Skip to content

Commit 59a6d4a

Browse files
committed
Make Counters final with public functions
1 parent abe3af1 commit 59a6d4a

File tree

5 files changed

+86
-107
lines changed

5 files changed

+86
-107
lines changed

library/core/api/core.api

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,42 @@ public abstract interface class org/kotlincrypto/core/Copyable {
66
public abstract fun copy ()Ljava/lang/Object;
77
}
88

9-
public abstract class org/kotlincrypto/core/Counter {
9+
public abstract class org/kotlincrypto/core/Counter : org/kotlincrypto/core/Copyable, org/kotlincrypto/core/Resettable {
1010
public final fun equals (Ljava/lang/Object;)Z
1111
public final fun hashCode ()I
12-
protected abstract fun increment ()V
13-
protected abstract fun reset ()V
12+
public abstract fun increment ()V
1413
public final fun toString ()Ljava/lang/String;
1514
}
1615

17-
public abstract class org/kotlincrypto/core/Counter$Bit32 : org/kotlincrypto/core/Counter {
16+
public final class org/kotlincrypto/core/Counter$Bit32 : org/kotlincrypto/core/Counter {
1817
public static final field Companion Lorg/kotlincrypto/core/Counter$Bit32$Companion;
1918
public static final field MAX_INCREMENT I
2019
public final field incrementBy I
2120
public fun <init> (I)V
2221
public fun <init> (III)V
23-
public fun <init> (Lorg/kotlincrypto/core/Counter$Bit32;)V
22+
public synthetic fun copy ()Ljava/lang/Object;
23+
public fun copy ()Lorg/kotlincrypto/core/Counter$Bit32;
2424
public final fun hi ()I
25-
protected fun increment ()V
25+
public fun increment ()V
2626
public final fun lo ()I
27-
protected fun reset ()V
27+
public fun reset ()V
2828
}
2929

3030
public final class org/kotlincrypto/core/Counter$Bit32$Companion {
3131
}
3232

33-
public abstract class org/kotlincrypto/core/Counter$Bit64 : org/kotlincrypto/core/Counter {
33+
public final class org/kotlincrypto/core/Counter$Bit64 : org/kotlincrypto/core/Counter {
3434
public static final field Companion Lorg/kotlincrypto/core/Counter$Bit64$Companion;
3535
public static final field MAX_INCREMENT J
3636
public final field incrementBy J
3737
public fun <init> (J)V
3838
public fun <init> (JJJ)V
39-
public fun <init> (Lorg/kotlincrypto/core/Counter$Bit64;)V
39+
public synthetic fun copy ()Ljava/lang/Object;
40+
public fun copy ()Lorg/kotlincrypto/core/Counter$Bit64;
4041
public final fun hi ()J
41-
protected fun increment ()V
42+
public fun increment ()V
4243
public final fun lo ()J
43-
protected fun reset ()V
44+
public fun reset ()V
4445
}
4546

4647
public final class org/kotlincrypto/core/Counter$Bit64$Companion {

library/core/api/core.klib.api

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ abstract interface org.kotlincrypto.core/Updatable { // org.kotlincrypto.core/Up
3232
abstract fun update(kotlin/ByteArray, kotlin/Int, kotlin/Int) // org.kotlincrypto.core/Updatable.update|update(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
3333
}
3434

35-
sealed class org.kotlincrypto.core/Counter { // org.kotlincrypto.core/Counter|null[0]
35+
sealed class org.kotlincrypto.core/Counter : org.kotlincrypto.core/Copyable<org.kotlincrypto.core/Counter>, org.kotlincrypto.core/Resettable { // org.kotlincrypto.core/Counter|null[0]
3636
abstract fun increment() // org.kotlincrypto.core/Counter.increment|increment(){}[0]
37-
abstract fun reset() // org.kotlincrypto.core/Counter.reset|reset(){}[0]
3837
final fun equals(kotlin/Any?): kotlin/Boolean // org.kotlincrypto.core/Counter.equals|equals(kotlin.Any?){}[0]
3938
final fun hashCode(): kotlin/Int // org.kotlincrypto.core/Counter.hashCode|hashCode(){}[0]
4039
final fun toString(): kotlin/String // org.kotlincrypto.core/Counter.toString|toString(){}[0]
4140

42-
abstract class Bit32 : org.kotlincrypto.core/Counter { // org.kotlincrypto.core/Counter.Bit32|null[0]
41+
final class Bit32 : org.kotlincrypto.core/Counter { // org.kotlincrypto.core/Counter.Bit32|null[0]
4342
constructor <init>(kotlin/Int) // org.kotlincrypto.core/Counter.Bit32.<init>|<init>(kotlin.Int){}[0]
4443
constructor <init>(kotlin/Int, kotlin/Int, kotlin/Int) // org.kotlincrypto.core/Counter.Bit32.<init>|<init>(kotlin.Int;kotlin.Int;kotlin.Int){}[0]
45-
constructor <init>(org.kotlincrypto.core/Counter.Bit32) // org.kotlincrypto.core/Counter.Bit32.<init>|<init>(org.kotlincrypto.core.Counter.Bit32){}[0]
4644

4745
final val incrementBy // org.kotlincrypto.core/Counter.Bit32.incrementBy|{}incrementBy[0]
4846
final fun <get-incrementBy>(): kotlin/Int // org.kotlincrypto.core/Counter.Bit32.incrementBy.<get-incrementBy>|<get-incrementBy>(){}[0]
@@ -52,19 +50,19 @@ sealed class org.kotlincrypto.core/Counter { // org.kotlincrypto.core/Counter|nu
5250
final var lo // org.kotlincrypto.core/Counter.Bit32.lo|{}lo[0]
5351
final fun <get-lo>(): kotlin/Int // org.kotlincrypto.core/Counter.Bit32.lo.<get-lo>|<get-lo>(){}[0]
5452

55-
open fun increment() // org.kotlincrypto.core/Counter.Bit32.increment|increment(){}[0]
56-
open fun reset() // org.kotlincrypto.core/Counter.Bit32.reset|reset(){}[0]
53+
final fun copy(): org.kotlincrypto.core/Counter.Bit32 // org.kotlincrypto.core/Counter.Bit32.copy|copy(){}[0]
54+
final fun increment() // org.kotlincrypto.core/Counter.Bit32.increment|increment(){}[0]
55+
final fun reset() // org.kotlincrypto.core/Counter.Bit32.reset|reset(){}[0]
5756

5857
final object Companion { // org.kotlincrypto.core/Counter.Bit32.Companion|null[0]
5958
final const val MAX_INCREMENT // org.kotlincrypto.core/Counter.Bit32.Companion.MAX_INCREMENT|{}MAX_INCREMENT[0]
6059
final fun <get-MAX_INCREMENT>(): kotlin/Int // org.kotlincrypto.core/Counter.Bit32.Companion.MAX_INCREMENT.<get-MAX_INCREMENT>|<get-MAX_INCREMENT>(){}[0]
6160
}
6261
}
6362

64-
abstract class Bit64 : org.kotlincrypto.core/Counter { // org.kotlincrypto.core/Counter.Bit64|null[0]
63+
final class Bit64 : org.kotlincrypto.core/Counter { // org.kotlincrypto.core/Counter.Bit64|null[0]
6564
constructor <init>(kotlin/Long) // org.kotlincrypto.core/Counter.Bit64.<init>|<init>(kotlin.Long){}[0]
6665
constructor <init>(kotlin/Long, kotlin/Long, kotlin/Long) // org.kotlincrypto.core/Counter.Bit64.<init>|<init>(kotlin.Long;kotlin.Long;kotlin.Long){}[0]
67-
constructor <init>(org.kotlincrypto.core/Counter.Bit64) // org.kotlincrypto.core/Counter.Bit64.<init>|<init>(org.kotlincrypto.core.Counter.Bit64){}[0]
6866

6967
final val incrementBy // org.kotlincrypto.core/Counter.Bit64.incrementBy|{}incrementBy[0]
7068
final fun <get-incrementBy>(): kotlin/Long // org.kotlincrypto.core/Counter.Bit64.incrementBy.<get-incrementBy>|<get-incrementBy>(){}[0]
@@ -74,8 +72,9 @@ sealed class org.kotlincrypto.core/Counter { // org.kotlincrypto.core/Counter|nu
7472
final var lo // org.kotlincrypto.core/Counter.Bit64.lo|{}lo[0]
7573
final fun <get-lo>(): kotlin/Long // org.kotlincrypto.core/Counter.Bit64.lo.<get-lo>|<get-lo>(){}[0]
7674

77-
open fun increment() // org.kotlincrypto.core/Counter.Bit64.increment|increment(){}[0]
78-
open fun reset() // org.kotlincrypto.core/Counter.Bit64.reset|reset(){}[0]
75+
final fun copy(): org.kotlincrypto.core/Counter.Bit64 // org.kotlincrypto.core/Counter.Bit64.copy|copy(){}[0]
76+
final fun increment() // org.kotlincrypto.core/Counter.Bit64.increment|increment(){}[0]
77+
final fun reset() // org.kotlincrypto.core/Counter.Bit64.reset|reset(){}[0]
7978

8079
final object Companion { // org.kotlincrypto.core/Counter.Bit64.Companion|null[0]
8180
final const val MAX_INCREMENT // org.kotlincrypto.core/Counter.Bit64.Companion.MAX_INCREMENT|{}MAX_INCREMENT[0]

library/core/src/commonMain/kotlin/org/kotlincrypto/core/Counter.kt

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,28 @@ import kotlin.jvm.JvmName
2323
/**
2424
* Utility for counting things.
2525
* */
26-
public sealed class Counter private constructor() {
26+
public sealed class Counter private constructor(): Resettable, Copyable<Counter> {
27+
28+
/**
29+
* Increments the counter
30+
* */
31+
public abstract fun increment()
2732

2833
/**
2934
* A counter that utilizes 32-bit numbers providing a maximum count of 2^64.
35+
*
36+
* @see [Bit64]
3037
* */
31-
public abstract class Bit32: Counter {
38+
public class Bit32: Counter {
3239

3340
public companion object {
3441

3542
/**
3643
* The maximum value for which [Bit32.incrementBy] can be set to.
3744
*
38-
* 1024 * 1024 >> 1048576
45+
* 1024^2 >> 1048576
3946
* */
40-
public const val MAX_INCREMENT: Int = 1024 * 1024 // Never decrease, only increase.
47+
public const val MAX_INCREMENT: Int = 1048576 // Never decrease, only increase.
4148
}
4249

4350
/**
@@ -90,39 +97,40 @@ public sealed class Counter private constructor() {
9097
* */
9198
public constructor(incrementBy: Int): this(0, 0, incrementBy)
9299

93-
/**
94-
* Creates a clone of [other]
95-
* */
96-
public constructor(other: Bit32): super() {
97-
this.incrementBy = other.incrementBy
98-
this.lo = other.lo
99-
this.hi = other.hi
100-
}
100+
public override fun copy(): Bit32 = Bit32(this)
101101

102-
protected override fun increment() {
102+
public override fun increment() {
103103
lo += incrementBy
104104
if (lo == 0) hi++
105105
}
106106

107-
protected override fun reset() {
107+
public override fun reset() {
108108
lo = 0
109109
hi = 0
110110
}
111+
112+
private constructor(other: Bit32): super() {
113+
this.incrementBy = other.incrementBy
114+
this.lo = other.lo
115+
this.hi = other.hi
116+
}
111117
}
112118

113119
/**
114120
* A counter that utilizes 64-bit numbers providing a maximum count of 2^128.
121+
*
122+
* @see [Bit32]
115123
* */
116-
public abstract class Bit64: Counter {
124+
public class Bit64: Counter {
117125

118126
public companion object {
119127

120128
/**
121129
* The maximum value for which [Bit64.incrementBy] can be set to.
122130
*
123-
* @see [Bit32.MAX_INCREMENT]
131+
* 1024^4 >> 1099511627776
124132
* */
125-
public const val MAX_INCREMENT: Long = Bit32.MAX_INCREMENT.toLong()
133+
public const val MAX_INCREMENT: Long = 1099511627776L // Never decrease, only increase.
126134
}
127135

128136
/**
@@ -175,28 +183,24 @@ public sealed class Counter private constructor() {
175183
* */
176184
public constructor(incrementBy: Long): this(0, 0, incrementBy)
177185

178-
/**
179-
* Creates a clone of [other]
180-
* */
181-
public constructor(other: Bit64): super() {
182-
this.incrementBy = other.incrementBy
183-
this.lo = other.lo
184-
this.hi = other.hi
185-
}
186+
public override fun copy(): Bit64 = Bit64(this)
186187

187-
protected override fun increment() {
188+
public override fun increment() {
188189
lo += incrementBy
189190
if (lo == 0L) hi++
190191
}
191192

192-
protected override fun reset() {
193+
public override fun reset() {
193194
lo = 0L
194195
hi = 0L
195196
}
196-
}
197197

198-
protected abstract fun increment()
199-
protected abstract fun reset()
198+
private constructor(other: Bit64): super() {
199+
this.incrementBy = other.incrementBy
200+
this.lo = other.lo
201+
this.hi = other.hi
202+
}
203+
}
200204

201205
private val code = Any()
202206

library/core/src/commonTest/kotlin/org/kotlincrypto/core/CounterUnitTest.kt

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,50 @@ class CounterUnitTest {
2424
@Test
2525
fun givenIncrementBy_when0_thenThrowsException() {
2626
assertFailsWith<IllegalArgumentException> {
27-
TestBit32Counter(0)
27+
Counter.Bit32(0)
2828
}
2929
assertFailsWith<IllegalArgumentException> {
30-
TestBit64Counter(0)
30+
Counter.Bit64(0)
3131
}
3232
}
3333

3434
@Test
3535
fun givenIncrementBy_whenExceedsMaximum_thenThrowsException() {
3636
assertFailsWith<IllegalArgumentException> {
37-
TestBit32Counter(Counter.Bit32.MAX_INCREMENT + 8)
37+
Counter.Bit32(Counter.Bit32.MAX_INCREMENT + 8)
3838
}
3939
assertFailsWith<IllegalArgumentException> {
40-
TestBit64Counter(Counter.Bit64.MAX_INCREMENT + 8)
40+
Counter.Bit64(Counter.Bit64.MAX_INCREMENT + 8)
4141
}
4242
}
4343

4444
@Test
4545
fun givenIncrementBy_whenNotFactory8_thenThrowsException() {
4646
assertFailsWith<IllegalArgumentException> {
47-
TestBit32Counter(9)
47+
Counter.Bit32(9)
4848
}
4949
assertFailsWith<IllegalArgumentException> {
50-
TestBit64Counter(9)
50+
Counter.Bit64(9)
5151
}
5252
}
5353

5454
@Test
5555
fun givenLo_whenNotFactoryIncrementBy_thenThrowsException() {
5656
// Would throw if 24 was not a factor of 8...
57-
TestBit32Counter(24)
58-
TestBit64Counter(24)
57+
Counter.Bit32(24)
58+
Counter.Bit64(24)
5959

6060
assertFailsWith<IllegalArgumentException> {
61-
TestBit32Counter(8, 0, 24)
61+
Counter.Bit32(8, 0, 24)
6262
}
6363
assertFailsWith<IllegalArgumentException> {
64-
TestBit64Counter(16, 0, 24)
64+
Counter.Bit64(16, 0, 24)
6565
}
6666
}
6767

6868
@Test
6969
fun givenBit32_whenReset_thenIsZero() {
70-
val c = TestBit32Counter(8, 8, 8)
70+
val c = Counter.Bit32(8, 8, 8)
7171
assertEquals(8, c.lo)
7272
assertEquals(8, c.hi)
7373
assertEquals(8, c.incrementBy)
@@ -78,7 +78,7 @@ class CounterUnitTest {
7878

7979
@Test
8080
fun givenBit64_whenReset_thenIsZero() {
81-
val c = TestBit64Counter(8, 8, 8)
81+
val c = Counter.Bit64(8, 8, 8)
8282
assertEquals(8, c.lo)
8383
assertEquals(8, c.hi)
8484
assertEquals(8, c.incrementBy)
@@ -89,42 +89,51 @@ class CounterUnitTest {
8989

9090
@Test
9191
fun givenBit32_whenIncrement_thenIncrements() {
92-
var c = TestBit32Counter(-16, 0, 8)
92+
var c = Counter.Bit32(-16, 0, 8)
9393
c.increment()
9494
assertEquals(-8, c.lo)
9595
assertEquals(0, c.hi)
9696
c.increment()
9797
assertEquals(0, c.lo)
9898
assertEquals(1, c.hi)
9999

100-
c = TestBit32Counter(Int.MAX_VALUE - 7, 0, 8)
100+
c = Counter.Bit32(Int.MAX_VALUE - 7, 0, 8)
101101
c.increment()
102102
assertEquals(Int.MIN_VALUE, c.lo)
103103
assertEquals(0, c.hi)
104104
}
105105

106106
@Test
107107
fun givenBit64_whenIncrement_thenIncrements() {
108-
var c = TestBit64Counter(-16, 0, 8)
108+
var c = Counter.Bit64(-16, 0, 8)
109109
c.increment()
110110
assertEquals(-8, c.lo)
111111
assertEquals(0, c.hi)
112112
c.increment()
113113
assertEquals(0, c.lo)
114114
assertEquals(1, c.hi)
115115

116-
c = TestBit64Counter(Long.MAX_VALUE - 7, 0, 8)
116+
c = Counter.Bit64(Long.MAX_VALUE - 7, 0, 8)
117117
c.increment()
118118
assertEquals(Long.MIN_VALUE, c.lo)
119119
assertEquals(0, c.hi)
120120
}
121121

122122
@Test
123-
fun givenBit32_whenClone_thenCopiesValues() {
124-
val expected = TestBit32Counter(8, 8, 8)
125-
val actual = TestBit32Counter(expected)
126-
assertEquals(expected.incrementBy, actual.incrementBy)
127-
assertEquals(expected.lo, actual.lo)
128-
assertEquals(expected.hi, actual.hi)
123+
fun givenBit32_whenCopy_thenCopiesValues() {
124+
val expected = Counter.Bit32(8, 8, 8)
125+
val copy = expected.copy()
126+
assertEquals(expected.incrementBy, copy.incrementBy)
127+
assertEquals(expected.lo, copy.lo)
128+
assertEquals(expected.hi, copy.hi)
129+
}
130+
131+
@Test
132+
fun givenBit64_whenCopy_thenCopiesValues() {
133+
val expected = Counter.Bit64(8, 8, 8)
134+
val copy = expected.copy()
135+
assertEquals(expected.incrementBy, copy.incrementBy)
136+
assertEquals(expected.lo, copy.lo)
137+
assertEquals(expected.hi, copy.hi)
129138
}
130139
}

0 commit comments

Comments
 (0)