diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/ArmFeedforwardJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/ArmFeedforwardJNI.java index 22796cf246..b72c7539f7 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/jni/ArmFeedforwardJNI.java +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/ArmFeedforwardJNI.java @@ -4,39 +4,8 @@ package edu.wpi.first.math.jni; -import edu.wpi.first.util.RuntimeLoader; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - /** ArmFeedforward JNI. */ -public final class ArmFeedforwardJNI { - static boolean libraryLoaded = false; - - static { - if (Helper.getExtractOnStaticLoad()) { - try { - RuntimeLoader.loadLibrary("wpimathjni"); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(1); - } - libraryLoaded = true; - } - } - - /** - * Force load the library. - * - * @throws IOException If the library could not be loaded or found. - */ - public static synchronized void forceLoad() throws IOException { - if (libraryLoaded) { - return; - } - RuntimeLoader.loadLibrary("wpimathjni"); - libraryLoaded = true; - } - +public final class ArmFeedforwardJNI extends WPIMathJNI { /** * Obtain a feedforward voltage from a single jointed arm feedforward object. * @@ -62,32 +31,6 @@ public final class ArmFeedforwardJNI { double nextVelocity, double dt); - /** Sets whether JNI should be loaded in the static block. */ - public static class Helper { - private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); - - /** - * Returns true if the JNI should be loaded in the static block. - * - * @return True if the JNI should be loaded in the static block. - */ - public static boolean getExtractOnStaticLoad() { - return extractOnStaticLoad.get(); - } - - /** - * Sets whether the JNI should be loaded in the static block. - * - * @param load Whether the JNI should be loaded in the static block. - */ - public static void setExtractOnStaticLoad(boolean load) { - extractOnStaticLoad.set(load); - } - - /** Utility class. */ - private Helper() {} - } - /** Utility class. */ private ArmFeedforwardJNI() {} } diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/DAREJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/DAREJNI.java index 8dcdf0e72a..644250f611 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/jni/DAREJNI.java +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/DAREJNI.java @@ -4,39 +4,8 @@ package edu.wpi.first.math.jni; -import edu.wpi.first.util.RuntimeLoader; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - /** DARE JNI. */ -public final class DAREJNI { - static boolean libraryLoaded = false; - - static { - if (Helper.getExtractOnStaticLoad()) { - try { - RuntimeLoader.loadLibrary("wpimathjni"); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(1); - } - libraryLoaded = true; - } - } - - /** - * Force load the library. - * - * @throws IOException If the library could not be loaded or found. - */ - public static synchronized void forceLoad() throws IOException { - if (libraryLoaded) { - return; - } - RuntimeLoader.loadLibrary("wpimathjni"); - libraryLoaded = true; - } - +public final class DAREJNI extends WPIMathJNI { /** * Computes the unique stabilizing solution X to the discrete-time algebraic Riccati equation. * @@ -202,32 +171,6 @@ public final class DAREJNI { int inputs, double[] S); - /** Sets whether JNI should be loaded in the static block. */ - public static class Helper { - private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); - - /** - * Returns true if the JNI should be loaded in the static block. - * - * @return True if the JNI should be loaded in the static block. - */ - public static boolean getExtractOnStaticLoad() { - return extractOnStaticLoad.get(); - } - - /** - * Sets whether the JNI should be loaded in the static block. - * - * @param load Whether the JNI should be loaded in the static block. - */ - public static void setExtractOnStaticLoad(boolean load) { - extractOnStaticLoad.set(load); - } - - /** Utility class. */ - private Helper() {} - } - /** Utility class. */ private DAREJNI() {} } diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/EigenJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/EigenJNI.java index ba7f2478d1..1a673ed86c 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/jni/EigenJNI.java +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/EigenJNI.java @@ -4,39 +4,8 @@ package edu.wpi.first.math.jni; -import edu.wpi.first.util.RuntimeLoader; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - /** Eigen JNI. */ -public final class EigenJNI { - static boolean libraryLoaded = false; - - static { - if (Helper.getExtractOnStaticLoad()) { - try { - RuntimeLoader.loadLibrary("wpimathjni"); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(1); - } - libraryLoaded = true; - } - } - - /** - * Force load the library. - * - * @throws IOException If the library could not be loaded or found. - */ - public static synchronized void forceLoad() throws IOException { - if (libraryLoaded) { - return; - } - RuntimeLoader.loadLibrary("wpimathjni"); - libraryLoaded = true; - } - +public final class EigenJNI extends WPIMathJNI { /** * Computes the matrix exp. * @@ -83,32 +52,6 @@ public final class EigenJNI { public static native void solveFullPivHouseholderQr( double[] A, int Arows, int Acols, double[] B, int Brows, int Bcols, double[] dst); - /** Sets whether JNI should be loaded in the static block. */ - public static class Helper { - private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); - - /** - * Returns true if the JNI should be loaded in the static block. - * - * @return True if the JNI should be loaded in the static block. - */ - public static boolean getExtractOnStaticLoad() { - return extractOnStaticLoad.get(); - } - - /** - * Sets whether the JNI should be loaded in the static block. - * - * @param load Whether the JNI should be loaded in the static block. - */ - public static void setExtractOnStaticLoad(boolean load) { - extractOnStaticLoad.set(load); - } - - /** Utility class. */ - private Helper() {} - } - /** Utility class. */ private EigenJNI() {} } diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/Ellipse2dJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/Ellipse2dJNI.java index e9a5c87067..984ca50401 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/jni/Ellipse2dJNI.java +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/Ellipse2dJNI.java @@ -4,39 +4,8 @@ package edu.wpi.first.math.jni; -import edu.wpi.first.util.RuntimeLoader; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - /** Ellipse2d JNI. */ -public final class Ellipse2dJNI { - static boolean libraryLoaded = false; - - static { - if (Helper.getExtractOnStaticLoad()) { - try { - RuntimeLoader.loadLibrary("wpimathjni"); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(1); - } - libraryLoaded = true; - } - } - - /** - * Force load the library. - * - * @throws IOException If the library could not be loaded or found. - */ - public static synchronized void forceLoad() throws IOException { - if (libraryLoaded) { - return; - } - RuntimeLoader.loadLibrary("wpimathjni"); - libraryLoaded = true; - } - +public final class Ellipse2dJNI extends WPIMathJNI { /** * Returns the nearest point that is contained within the ellipse. * @@ -61,32 +30,6 @@ public final class Ellipse2dJNI { double pointY, double[] nearestPoint); - /** Sets whether JNI should be loaded in the static block. */ - public static class Helper { - private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); - - /** - * Returns true if the JNI should be loaded in the static block. - * - * @return True if the JNI should be loaded in the static block. - */ - public static boolean getExtractOnStaticLoad() { - return extractOnStaticLoad.get(); - } - - /** - * Sets whether the JNI should be loaded in the static block. - * - * @param load Whether the JNI should be loaded in the static block. - */ - public static void setExtractOnStaticLoad(boolean load) { - extractOnStaticLoad.set(load); - } - - /** Utility class. */ - private Helper() {} - } - /** Utility class. */ private Ellipse2dJNI() {} } diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/Pose3dJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/Pose3dJNI.java index a642eaa3a9..f32d58e0b1 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/jni/Pose3dJNI.java +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/Pose3dJNI.java @@ -4,39 +4,8 @@ package edu.wpi.first.math.jni; -import edu.wpi.first.util.RuntimeLoader; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - /** Pose3d JNI. */ -public final class Pose3dJNI { - static boolean libraryLoaded = false; - - static { - if (Helper.getExtractOnStaticLoad()) { - try { - RuntimeLoader.loadLibrary("wpimathjni"); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(1); - } - libraryLoaded = true; - } - } - - /** - * Force load the library. - * - * @throws IOException If the library could not be loaded or found. - */ - public static synchronized void forceLoad() throws IOException { - if (libraryLoaded) { - return; - } - RuntimeLoader.loadLibrary("wpimathjni"); - libraryLoaded = true; - } - +public final class Pose3dJNI extends WPIMathJNI { /** * Obtain a Pose3d from a (constant curvature) velocity. * @@ -109,32 +78,6 @@ public final class Pose3dJNI { double endQy, double endQz); - /** Sets whether JNI should be loaded in the static block. */ - public static class Helper { - private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); - - /** - * Returns true if the JNI should be loaded in the static block. - * - * @return True if the JNI should be loaded in the static block. - */ - public static boolean getExtractOnStaticLoad() { - return extractOnStaticLoad.get(); - } - - /** - * Sets whether the JNI should be loaded in the static block. - * - * @param load Whether the JNI should be loaded in the static block. - */ - public static void setExtractOnStaticLoad(boolean load) { - extractOnStaticLoad.set(load); - } - - /** Utility class. */ - private Helper() {} - } - /** Utility class. */ private Pose3dJNI() {} } diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/StateSpaceUtilJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/StateSpaceUtilJNI.java index 9ee02b6c4c..b590e4ce15 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/jni/StateSpaceUtilJNI.java +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/StateSpaceUtilJNI.java @@ -4,39 +4,8 @@ package edu.wpi.first.math.jni; -import edu.wpi.first.util.RuntimeLoader; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - /** StateSpaceUtil JNI. */ -public final class StateSpaceUtilJNI { - static boolean libraryLoaded = false; - - static { - if (Helper.getExtractOnStaticLoad()) { - try { - RuntimeLoader.loadLibrary("wpimathjni"); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(1); - } - libraryLoaded = true; - } - } - - /** - * Force load the library. - * - * @throws IOException If the library could not be loaded or found. - */ - public static synchronized void forceLoad() throws IOException { - if (libraryLoaded) { - return; - } - RuntimeLoader.loadLibrary("wpimathjni"); - libraryLoaded = true; - } - +public final class StateSpaceUtilJNI extends WPIMathJNI { /** * Returns true if (A, B) is a stabilizable pair. * @@ -52,32 +21,6 @@ public final class StateSpaceUtilJNI { */ public static native boolean isStabilizable(int states, int inputs, double[] A, double[] B); - /** Sets whether JNI should be loaded in the static block. */ - public static class Helper { - private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); - - /** - * Returns true if the JNI should be loaded in the static block. - * - * @return True if the JNI should be loaded in the static block. - */ - public static boolean getExtractOnStaticLoad() { - return extractOnStaticLoad.get(); - } - - /** - * Sets whether the JNI should be loaded in the static block. - * - * @param load Whether the JNI should be loaded in the static block. - */ - public static void setExtractOnStaticLoad(boolean load) { - extractOnStaticLoad.set(load); - } - - /** Utility class. */ - private Helper() {} - } - /** Utility class. */ private StateSpaceUtilJNI() {} } diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/TrajectoryUtilJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/TrajectoryUtilJNI.java index 935b0c958b..7b27240cb3 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/jni/TrajectoryUtilJNI.java +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/TrajectoryUtilJNI.java @@ -4,39 +4,10 @@ package edu.wpi.first.math.jni; -import edu.wpi.first.util.RuntimeLoader; import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; /** TrajectoryUtil JNI. */ -public final class TrajectoryUtilJNI { - static boolean libraryLoaded = false; - - static { - if (Helper.getExtractOnStaticLoad()) { - try { - RuntimeLoader.loadLibrary("wpimathjni"); - } catch (Exception ex) { - ex.printStackTrace(); - System.exit(1); - } - libraryLoaded = true; - } - } - - /** - * Force load the library. - * - * @throws IOException If the library could not be loaded or found. - */ - public static synchronized void forceLoad() throws IOException { - if (libraryLoaded) { - return; - } - RuntimeLoader.loadLibrary("wpimathjni"); - libraryLoaded = true; - } - +public final class TrajectoryUtilJNI extends WPIMathJNI { /** * Loads a Pathweaver JSON. * @@ -71,32 +42,6 @@ public final class TrajectoryUtilJNI { */ public static native String serializeTrajectory(double[] elements); - /** Sets whether JNI should be loaded in the static block. */ - public static class Helper { - private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); - - /** - * Returns true if the JNI should be loaded in the static block. - * - * @return True if the JNI should be loaded in the static block. - */ - public static boolean getExtractOnStaticLoad() { - return extractOnStaticLoad.get(); - } - - /** - * Sets whether the JNI should be loaded in the static block. - * - * @param load Whether the JNI should be loaded in the static block. - */ - public static void setExtractOnStaticLoad(boolean load) { - extractOnStaticLoad.set(load); - } - - /** Utility class. */ - private Helper() {} - } - /** Utility class. */ private TrajectoryUtilJNI() {} } diff --git a/wpimath/src/main/java/edu/wpi/first/math/jni/WPIMathJNI.java b/wpimath/src/main/java/edu/wpi/first/math/jni/WPIMathJNI.java new file mode 100644 index 0000000000..8b16e7a189 --- /dev/null +++ b/wpimath/src/main/java/edu/wpi/first/math/jni/WPIMathJNI.java @@ -0,0 +1,68 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.math.jni; + +import edu.wpi.first.util.RuntimeLoader; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; + +/** Base class for all WPIMath JNI wrappers. */ +public class WPIMathJNI { + private static boolean libraryLoaded = false; + + /** Sets whether JNI should be loaded in the static block. */ + public static class Helper { + private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); + + /** + * Returns true if the JNI should be loaded in the static block. + * + * @return True if the JNI should be loaded in the static block. + */ + public static boolean getExtractOnStaticLoad() { + return extractOnStaticLoad.get(); + } + + /** + * Sets whether the JNI should be loaded in the static block. + * + * @param load Whether the JNI should be loaded in the static block. + */ + public static void setExtractOnStaticLoad(boolean load) { + extractOnStaticLoad.set(load); + } + + /** Utility class. */ + private Helper() {} + } + + static { + if (Helper.getExtractOnStaticLoad()) { + try { + RuntimeLoader.loadLibrary("wpimathjni"); + } catch (Exception ex) { + ex.printStackTrace(); + System.exit(1); + } + libraryLoaded = true; + } + } + + /** + * Force load the library. + * + * @throws IOException if the library load failed + */ + public static synchronized void forceLoad() throws IOException { + if (libraryLoaded) { + return; + } + RuntimeLoader.loadLibrary("wpimathjni"); + libraryLoaded = true; + } + + /** Utility class. */ + protected WPIMathJNI() {} +}