Skip to content

Commit aae97d6

Browse files
committed
import Guava primitives
1 parent 5f31cd1 commit aae97d6

39 files changed

+11000
-935
lines changed

NOTICE.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ components that this product depends on.
2626

2727
-------------------------------------------------------------------------------
2828

29-
This product contains a unsigned numerics support which is derived from Google Guava's 'UnsignedLong' and related types.
29+
This product contains a unsigned numerics support which is derived from Google Guava library (Version 33.4.8).
30+
Specifically, types from the 'com.google.common.primitives.*' package.
3031

3132
* LICENSE (Apache License 2.0):
3233
* https://www.apache.org/licenses/LICENSE-2.0
3334
* HOMEPAGE:
3435
* https://github.com/google/guava/
36+
* https://github.com/google/guava/blob/master/guava/src/com/google/common/primitives/
3537
* https://github.com/google/guava/blob/master/guava/src/com/google/common/primitives/UnsignedLong.java
3638
* https://github.com/google/guava/blob/master/guava/src/com/google/common/primitives/UnsignedInts.java

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ let package = Package(
210210
],
211211
targets: [
212212
.target(
213-
name: "Documentation",
214213
name: "SwiftJavaDocumentation",
215214
dependencies: [
216215
"JavaKit",

Samples/SwiftKitSampleApp/Sources/MySwiftLibrary/MySwiftClass.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ public class MySwiftClass {
6161
return Int.random(in: 1..<256)
6262
}
6363

64-
public func takeUnsignedByte(arg: UInt8) -> UInt8 {
64+
public func takeUnsignedInt(arg: UInt32) {
6565
p("\(UInt32.self) = \(arg)")
66-
return arg
6766
}
6867

69-
public func takeUnsignedInt(arg: UInt32) {
70-
p("\(UInt32.self) = \(arg)")
68+
public func takeUnsignedLong(arg: UInt64) {
69+
p("\(UInt64.self) = \(arg)")
7170
}
7271
}

Samples/SwiftKitSampleApp/src/test/java/com/example/swift/UnsignedTest.java renamed to Samples/SwiftKitSampleApp/src/test/java/com/example/swift/UnsignedNumbersTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,20 @@
1818
import org.swift.swiftkit.core.primitives.*;
1919
import org.swift.swiftkit.ffm.AllocatingSwiftArena;
2020

21-
import static org.junit.jupiter.api.Assertions.*;
22-
23-
public class UnsignedTest {
21+
public class UnsignedNumbersTest {
2422
@Test
25-
void take_unsigned_int32() {
23+
void take_uint32() {
2624
try (var arena = AllocatingSwiftArena.ofConfined()) {
2725
var c = MySwiftClass.init(1, 2, arena);
2826
c.takeUnsignedInt(UnsignedInteger.valueOf(128));
2927
}
3028
}
3129

3230
@Test
33-
void take_uint8() {
31+
void take_uint64() {
3432
try (var arena = AllocatingSwiftArena.ofConfined()) {
3533
var c = MySwiftClass.init(1, 2, arena);
36-
byte got = c.takeUnsignedByte(UnsignedByte.valueOf(200)); // FIXME: should return UnsignedByte
37-
assertEquals(UnsignedByte.representedByBitsOf(got).intValue(), got);
34+
c.takeUnsignedLong(UnsignedLong.MAX_VALUE);
3835
}
3936
}
4037
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (C) 2007 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
//===----------------------------------------------------------------------===//
16+
//
17+
// This source file is part of the Swift.org open source project
18+
//
19+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
20+
// Licensed under Apache License v2.0
21+
//
22+
// See LICENSE.txt for license information
23+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
24+
//
25+
// SPDX-License-Identifier: Apache-2.0
26+
//
27+
//===----------------------------------------------------------------------===//
28+
29+
package org.swift.swiftkit.core;
30+
31+
import org.swift.swiftkit.core.annotations.Nullable;
32+
33+
/**
34+
* Collection of convenience functions to check argument preconditions.
35+
* <p/>
36+
* Partially based on {@code com.google.common.base.Preconditions}.
37+
*/
38+
public final class Preconditions {
39+
private Preconditions() {
40+
}
41+
42+
public static void checkArgument(boolean expression) {
43+
if (!expression) {
44+
throw new IllegalArgumentException();
45+
}
46+
}
47+
48+
public static void checkArgument(boolean expression, @Nullable String format) {
49+
if (!expression) {
50+
throw new IllegalArgumentException(format);
51+
}
52+
}
53+
54+
public static void checkArgument(boolean expression, @Nullable String format,
55+
@Nullable Object arg1) {
56+
if (!expression) {
57+
throw new IllegalArgumentException(String.format(format, arg1));
58+
}
59+
}
60+
61+
public static void checkArgument(boolean expression, @Nullable String format,
62+
@Nullable Object arg1,
63+
@Nullable Object arg2) {
64+
if (!expression) {
65+
throw new IllegalArgumentException(String.format(format, arg1, arg2));
66+
}
67+
}
68+
69+
public static <T> T checkNotNull(@Nullable T reference) {
70+
if (reference == null) {
71+
throw new NullPointerException();
72+
}
73+
74+
return reference;
75+
}
76+
77+
public static <T> T checkNotNull(@Nullable T reference, @Nullable String message) {
78+
if (reference == null) {
79+
throw new NullPointerException(message);
80+
}
81+
82+
return reference;
83+
}
84+
85+
/*
86+
* All recent hotspots (as of 2009) *really* like to have the natural code
87+
*
88+
* if (guardExpression) {
89+
* throw new BadException(messageExpression);
90+
* }
91+
*
92+
* refactored so that messageExpression is moved to a separate String-returning method.
93+
*
94+
* if (guardExpression) {
95+
* throw new BadException(badMsg(...));
96+
* }
97+
*
98+
* The alternative natural refactorings into void or Exception-returning methods are much slower.
99+
* This is a big deal - we're talking factors of 2-8 in microbenchmarks, not just 10-20%. (This is
100+
* a hotspot optimizer bug, which should be fixed, but that's a separate, big project).
101+
*
102+
* The coding pattern above is heavily used in java.util, e.g. in ArrayList. There is a
103+
* RangeCheckMicroBenchmark in the JDK that was used to test this.
104+
*
105+
* But the methods in this class want to throw different exceptions, depending on the args, so it
106+
* appears that this pattern is not directly applicable. But we can use the ridiculous, devious
107+
* trick of throwing an exception in the middle of the construction of another exception. Hotspot
108+
* is fine with that.
109+
*/
110+
111+
/**
112+
* Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size
113+
* {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
114+
*
115+
* @param index a user-supplied index identifying an element of an array, list or string
116+
* @param size the size of that array, list or string
117+
* @return the value of {@code index}
118+
* @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
119+
* @throws IllegalArgumentException if {@code size} is negative
120+
*/
121+
public static int checkElementIndex(int index, int size) {
122+
return checkElementIndex(index, size, "index");
123+
}
124+
125+
/**
126+
* Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size
127+
* {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive.
128+
*
129+
* @param index a user-supplied index identifying an element of an array, list or string
130+
* @param size the size of that array, list or string
131+
* @param desc the text to use to describe this index in an error message
132+
* @return the value of {@code index}
133+
* @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
134+
* @throws IllegalArgumentException if {@code size} is negative
135+
*/
136+
public static int checkElementIndex(int index, int size, String desc) {
137+
// Carefully optimized for execution by hotspot (explanatory comment above)
138+
if (index < 0 || index >= size) {
139+
throw new IndexOutOfBoundsException(
140+
String.format("%s, index:%d, size:%d", desc, index, size));
141+
}
142+
return index;
143+
}
144+
145+
public static void checkPositionIndexes(int start, int end, int size) {
146+
// Carefully optimized for execution by hotspot (explanatory comment above)
147+
if (start < 0 || end < start || end > size) {
148+
throw new IndexOutOfBoundsException(
149+
String.format("Start index:%d, end index:%d, size: %d", start, end, size));
150+
}
151+
}
152+
153+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.swift.swiftkit.core.annotations;
2+
3+
4+
import java.lang.annotation.Documented;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.Target;
7+
8+
import static java.lang.annotation.ElementType.TYPE_USE;
9+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
10+
11+
// TODO: Consider depending on jspecify instead
12+
@Documented
13+
@Target(TYPE_USE)
14+
@Retention(RUNTIME)
15+
public @interface NonNull {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.swift.swiftkit.core.annotations;
2+
3+
4+
import static java.lang.annotation.ElementType.TYPE_USE;
5+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
6+
7+
import java.lang.annotation.Documented;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.Target;
10+
11+
// TODO: Consider depending on jspecify instead
12+
@Documented
13+
@Target(TYPE_USE)
14+
@Retention(RUNTIME)
15+
public @interface Nullable {}

0 commit comments

Comments
 (0)