Skip to content

Commit d349948

Browse files
committed
wip
1 parent 7bec685 commit d349948

File tree

6 files changed

+202
-4
lines changed

6 files changed

+202
-4
lines changed

SwiftKitCore/build.gradle

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,24 @@ repositories {
2525

2626
java {
2727
toolchain {
28-
languageVersion.set(JavaLanguageVersion.of(17))
28+
languageVersion.set(JavaLanguageVersion.of(1))
2929
}
3030
// Support Android 6+ (Java 7)
31-
sourceCompatibility = JavaVersion.VERSION_1_7
32-
targetCompatibility = JavaVersion.VERSION_1_7
31+
sourceCompatibility = JavaVersion.VERSION_17
32+
targetCompatibility = JavaVersion.VERSION_17
3333
}
3434

3535
dependencies {
36-
testImplementation 'junit:junit:4.13.2'
36+
testImplementation(platform("org.junit:junit-bom:5.10.0"))
37+
testImplementation("org.junit.jupiter:junit-jupiter")
38+
}
39+
40+
testing {
41+
suites {
42+
test {
43+
useJUnitJupiter('5.10.3')
44+
}
45+
}
3746
}
3847

3948
tasks.test {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package org.swift.swiftkit.core.primitives;
16+
17+
public class UnsignedByte {
18+
19+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package org.swift.swiftkit.core.primitives;
16+
17+
import java.math.BigInteger;
18+
import java.util.Objects;
19+
20+
public final class UnsignedInteger extends Number implements Comparable<UnsignedInteger> {
21+
22+
public final static UnsignedInteger ZERO = representedByBitsOf(0);
23+
public final static UnsignedInteger MAX_VALUE = representedByBitsOf(-1);
24+
public final static long MASK = 0xffffffffL;
25+
26+
final int value;
27+
28+
private UnsignedInteger(int bits) {
29+
this.value = bits;
30+
}
31+
32+
/**
33+
* Accept a signed Java @{code int} value, and interpret it as-if it was an unsigned value.
34+
* In other words, do not interpret the negative bit as "negative", but as part of the unsigned integers value.
35+
*
36+
* @param value bit value to store in this unsigned integer
37+
* @return unsigned integer representation of the passed in value
38+
*/
39+
public static UnsignedInteger representedByBitsOf(int value) {
40+
return new UnsignedInteger(value);
41+
}
42+
43+
public static UnsignedInteger valueOf(long value) throws UnsignedOverflowException {
44+
if ((value & UnsignedInteger.MASK) != value) {
45+
throw new UnsignedOverflowException(String.valueOf(value), UnsignedInteger.class);
46+
}
47+
return representedByBitsOf((int) value);
48+
}
49+
50+
@Override
51+
public int compareTo(UnsignedInteger o) {
52+
Objects.requireNonNull(o);
53+
return Long.compare(this.value + Long.MIN_VALUE, o.value + Long.MIN_VALUE);
54+
}
55+
56+
/**
57+
* Warning, this value is based on the exact bytes interpreted as a signed integer.
58+
*/
59+
@Override
60+
public int intValue() {
61+
return value;
62+
}
63+
64+
@Override
65+
public long longValue() {
66+
return 0;
67+
}
68+
69+
@Override
70+
public float floatValue() {
71+
return longValue(); // rely on standard decimal -> floating point conversion
72+
}
73+
74+
@Override
75+
public double doubleValue() {
76+
return longValue(); // rely on standard decimal -> floating point conversion
77+
}
78+
79+
public BigInteger bigIntegerValue() {
80+
return BigInteger.valueOf(value);
81+
}
82+
83+
@Override
84+
public boolean equals(Object o) {
85+
if (this == o) return true;
86+
if (o == null || getClass() != o.getClass()) return false;
87+
UnsignedInteger that = (UnsignedInteger) o;
88+
return value == that.value;
89+
}
90+
91+
@Override
92+
public int hashCode() {
93+
return value;
94+
}
95+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package org.swift.swiftkit.core.primitives;
16+
17+
import org.swift.swiftkit.core.SwiftValue;
18+
19+
/**
20+
* The abstract base class for all unsigned numeric wrapper types offered by SwiftKit.
21+
*/
22+
public abstract class UnsignedNumber extends Number {
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.swift.swiftkit.core.primitives;
2+
3+
public class UnsignedOverflowException extends RuntimeException {
4+
public UnsignedOverflowException(String value, Class<?> clazz) {
5+
super(String.format("Value '%s' cannot be represented as %s as it would overflow!", value, clazz.getName()));
6+
}
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package org.swift.swiftkit.core.primitives;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
public class UnsignedIntegerTest {
22+
@Test
23+
public void simpleIntegerValues() {
24+
assertEquals(UnsignedInteger.representedByBitsOf(12).intValue(), 12);
25+
// signed "max" easily fits in an unsigned integer
26+
assertEquals(UnsignedInteger.representedByBitsOf(Integer.MAX_VALUE).intValue(), Integer.MAX_VALUE);
27+
}
28+
29+
@Test
30+
public void maxUnsignedIntegerValue() {
31+
assertEquals(UnsignedInteger.representedByBitsOf(Integer.MAX_VALUE).intValue(), Integer.MAX_VALUE);
32+
}
33+
34+
@Test
35+
public void outOfRangeLongValue() {
36+
var exception = assertThrows(Exception.class, () -> UnsignedInteger.valueOf(2^42).intValue());
37+
assertTrue(exception.getMessage() instanceof UnsignedOverflowException);
38+
}
39+
40+
@Test
41+
public void valueRoundTrip() {
42+
int input = 129;
43+
assertEquals(UnsignedInteger.representedByBitsOf(input).intValue(), input);
44+
}
45+
}

0 commit comments

Comments
 (0)