Skip to content

Commit d3f708b

Browse files
committed
java: export util/java from google3
1 parent f75e559 commit d3f708b

18 files changed

Lines changed: 1172 additions & 25 deletions

MODULE.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ JUNIT_JUPITER_VERSION = "5.9.2"
107107
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
108108
maven.install(
109109
artifacts = [
110-
"com.google.truth:truth:0.32",
110+
"com.google.errorprone:error_prone_type_annotations:2.46.0",
111+
"com.google.truth:truth:1.4.2",
112+
"com.google.truth.extensions:truth-proto-extension:1.4.2",
111113
"net.java.dev.jna:jna:5.14.0",
114+
"org.jspecify:jspecify:1.0.0",
112115
"org.junit.platform:junit-platform-launcher:%s" % JUNIT_PLATFORM_VERSION,
113116
"org.junit.platform:junit-platform-reporting:%s" % JUNIT_PLATFORM_VERSION,
114117
"org.junit.jupiter:junit-jupiter-api:%s" % JUNIT_JUPITER_VERSION,

cmake/java.cmake

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,19 @@ foreach(SUBPROJECT IN ITEMS
267267
init
268268
linear_solver
269269
constraint_solver
270-
routing
271-
sat
272-
util)
270+
routing)
273271
add_subdirectory(ortools/${SUBPROJECT}/java)
274272
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jni${SUBPROJECT})
275273
endforeach()
274+
# from ortools/linear_solver/java
276275
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jnimodelbuilder)
277-
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE ortools::util_java_jni_helper)
276+
add_subdirectory(ortools/sat/java)
277+
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jnisat)
278278
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jni_cp_model_proto)
279+
add_subdirectory(ortools/util/java)
280+
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE util_java_jni_helper)
281+
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jni_sorted_interval_list)
282+
target_link_libraries(jni${JAVA_ARTIFACT} PRIVATE jni_java_swig_solve_interrupter)
279283

280284
#################################
281285
## Java Native Maven Package ##
@@ -569,6 +573,13 @@ add_custom_target(java_deploy
569573
WORKING_DIRECTORY ${JAVA_PROJECT_DIR})
570574
add_dependencies(java_deploy java_package)
571575

576+
#################
577+
## Java Test ##
578+
#################
579+
if(BUILD_TESTING)
580+
add_subdirectory(ortools/javatests/com/google/ortools javatests/ortools)
581+
endif()
582+
572583
###############
573584
## Doc rules ##
574585
###############

ortools/java/com/google/ortools/BUILD.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cc_binary(
3030
"//ortools/linear_solver/java:modelbuilder_cc",
3131
"//ortools/sat/java:cp_model_proto_cc",
3232
"//ortools/sat/java:sat_cc",
33+
"//ortools/util/java:java_swig_solve_interrupter_swig_cc",
3334
"//ortools/util/java:sorted_interval_list_cc",
3435
"//ortools/util/java:wrappers_test_message_cc",
3536
],
@@ -44,15 +45,16 @@ cc_binary(
4445
}),
4546
visibility = ["//visibility:public"],
4647
deps = [
47-
"@rules_jni//jni:libjvm",
4848
"//ortools/algorithms/java:knapsacksolver_cc",
4949
"//ortools/graph/java:graph_cc",
5050
"//ortools/init/java:init_cc",
5151
"//ortools/linear_solver/java:modelbuilder_cc",
5252
"//ortools/sat/java:cp_model_proto_cc",
5353
"//ortools/sat/java:sat_cc",
54+
"//ortools/util/java:java_swig_solve_interrupter_swig_cc",
5455
"//ortools/util/java:sorted_interval_list_cc",
5556
"//ortools/util/java:wrappers_test_message_cc",
57+
"@rules_jni//jni:libjvm",
5658
],
5759
)
5860

@@ -65,15 +67,16 @@ cc_binary(
6567
}),
6668
visibility = ["//visibility:public"],
6769
deps = [
68-
"@rules_jni//jni:libjvm",
6970
"//ortools/algorithms/java:knapsacksolver_cc",
7071
"//ortools/graph/java:graph_cc",
7172
"//ortools/init/java:init_cc",
7273
"//ortools/linear_solver/java:modelbuilder_cc",
7374
"//ortools/sat/java:cp_model_proto_cc",
7475
"//ortools/sat/java:sat_cc",
76+
"//ortools/util/java:java_swig_solve_interrupter_swig_cc",
7577
"//ortools/util/java:sorted_interval_list_cc",
7678
"//ortools/util/java:wrappers_test_message_cc",
79+
"@rules_jni//jni:libjvm",
7780
],
7881
)
7982

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2010-2025 Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
load("@rules_java//java:java_library.bzl", "java_library")
15+
16+
java_library(
17+
name = "interruptable",
18+
srcs = ["Interruptable.java"],
19+
visibility = ["//visibility:public"],
20+
)
21+
22+
java_library(
23+
name = "solve_interrupter",
24+
srcs = ["SolveInterrupter.java"],
25+
visibility = ["//visibility:public"],
26+
deps = [
27+
":interruptable",
28+
"@maven//:com_google_errorprone_error_prone_annotations",
29+
],
30+
)
31+
32+
java_library(
33+
name = "proto_duration_conversion",
34+
srcs = ["ProtoDurationConversion.java"],
35+
visibility = [
36+
"//javatests/com/google/wrappers/util/operationsresearch:__subpackages__",
37+
"//ortools:__subpackages__",
38+
],
39+
deps = [
40+
"@maven//:com_google_protobuf_protobuf_java",
41+
"@maven//:com_google_protobuf_protobuf_java_util",
42+
],
43+
)
44+
45+
java_library(
46+
name = "scoped_swig_interrupter",
47+
srcs = ["ScopedSwigInterrupter.java"],
48+
visibility = ["//ortools:__subpackages__"],
49+
deps = [
50+
":interruptable",
51+
"//ortools/util/java:java_swig_solve_interrupter_swig", # buildcleaner: keep
52+
"@maven//:org_jspecify_jspecify",
53+
],
54+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2010-2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package com.google.ortools.util;
15+
16+
/**
17+
* Accepts notification to interrupt computation.
18+
*
19+
* @see SolveInterrupter
20+
*/
21+
public interface Interruptable {
22+
/** A notification that computation should stop as soon as possible. */
23+
void interrupt();
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2010-2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package com.google.ortools.util;
15+
16+
import com.google.protobuf.util.Durations;
17+
18+
/**
19+
* A utility class that provides conversions between {@link com.google.protobuf.Duration} and {@link
20+
* java.time.Duration}.
21+
*/
22+
public final class ProtoDurationConversion {
23+
private ProtoDurationConversion() {}
24+
25+
/**
26+
* Converts a {@link com.google.protobuf.Duration} to a {@link java.time.Duration}.
27+
*
28+
* @throws ArithmeticException if it does not fit in a {@link java.time.Duration}.
29+
*/
30+
public static java.time.Duration toJavaDuration(com.google.protobuf.Duration protoDuration) {
31+
// Note that java.time.Duration.ofSeconds() handles negative nanos seconds correctly.
32+
return java.time.Duration.ofSeconds(protoDuration.getSeconds(), protoDuration.getNanos());
33+
}
34+
35+
/**
36+
* Converts a {@link java.time.Duration} to a {@link com.google.protobuf.Duration}.
37+
*
38+
* @throws ArithmeticException if the given duration does not fit in {@link long} in nanoseconds.
39+
*/
40+
public static com.google.protobuf.Duration toProtoDuration(java.time.Duration duration) {
41+
// Note that here we don't support cases of durations that don't fit in `long` when using
42+
// nanoseconds.
43+
return Durations.fromNanos(duration.toNanos());
44+
}
45+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2010-2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package com.google.ortools.util;
15+
16+
import org.jspecify.annotations.Nullable;
17+
18+
/**
19+
* Wraps a JavaSwigSolveInterrupter in an AutoCloseable so SWIGed Cpp object can be cleaned up using
20+
* try-with-resources.
21+
*
22+
* <p>Note that the underlying JavaSwigSolveInterrupter is nullable.
23+
*
24+
* <p>This class is not thread-safe.
25+
*/
26+
public class ScopedSwigInterrupter implements AutoCloseable, Interruptable {
27+
// Use nullable instead of optional because the JniSolver.cppSolve() wants a nullable value.
28+
@Nullable JavaSwigSolveInterrupter swigInterrupter;
29+
30+
/** The underlying {@link JavaSwigSolveInterrupter} is created only when {@code isPresent.} */
31+
public ScopedSwigInterrupter(boolean isPresent) {
32+
swigInterrupter = isPresent ? new JavaSwigSolveInterrupter() : null;
33+
}
34+
35+
/** The underlying {@link JavaSwigSolveInterrupter} is created. */
36+
public ScopedSwigInterrupter() {
37+
this(true);
38+
}
39+
40+
/**
41+
* Returns the underlying SWIGed interrupter, which is non-null if and only if the input user
42+
* interrupter is non-null.
43+
*
44+
* <p>The returned object must not be used after calling {@link #close()}, doing so will cause a
45+
* crash or memory corruption in C++.
46+
*/
47+
public @Nullable JavaSwigSolveInterrupter getSwigInterrupter() {
48+
return swigInterrupter;
49+
}
50+
51+
@Override
52+
public void close() {
53+
if (swigInterrupter != null) {
54+
swigInterrupter.delete();
55+
swigInterrupter = null;
56+
}
57+
}
58+
59+
/**
60+
* Interrupts the underlying {@link JavaSwigSolveInterrupter}, has no effect if it is initialized
61+
* as null.
62+
*/
63+
@Override
64+
public void interrupt() {
65+
if (swigInterrupter != null) {
66+
swigInterrupter.interrupt();
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)