diff --git a/asyncapi-template/partials/SimDeviceSim.java b/asyncapi-template/partials/SimDeviceSim.java
index 5ea121b..6e6bdfb 100644
--- a/asyncapi-template/partials/SimDeviceSim.java
+++ b/asyncapi-template/partials/SimDeviceSim.java
@@ -18,7 +18,6 @@
import java.util.function.Predicate;
import org.team199.wpiws.Pair;
-import org.team199.wpiws.ScopedObject;
import org.team199.wpiws.StateDevice;
import org.team199.wpiws.connection.ConnectionProcessor;
import org.team199.wpiws.connection.WSValue;
@@ -168,21 +167,17 @@ private void set(String name, String value, boolean notifyRobot) {
* Registers a SimValueCallback to be called whenever a specified value for this device is created
* @param callback the callback function to call
* @param initialNotify if true
, calls the callback function with all currently initialized values
- * @return a ScopedObject which can be used to close the callback
+ * @return the callback object that can be used to cancel the callback
* @see #cancelValueCreatedCallback(StringCallback)
*/
- public ScopedObject registerValueCreatedCallback(StringCallback callback, boolean initialNotify) {
+ public StringCallback registerValueCreatedCallback(StringCallback callback, boolean initialNotify) {
getState().valueCreatedCallbacks.add(callback);
if(initialNotify) {
getState().existingValues.forEach(value -> callback.callback(value, get(value)));
}
- return new ScopedObject<>(callback, CANCEL_VALUE_CREATED_CALLBACK);
+ return callback;
}
- /**
- * A Consumer which calls {@link #cancelValueCreatedCallback(StringCallback)}
- */
- public final Consumer CANCEL_VALUE_CREATED_CALLBACK = this::cancelValueCreatedCallback;
/**
* Deregisters the given value created callback
* @param callback the callback to deregister
@@ -196,22 +191,18 @@ public void cancelValueCreatedCallback(StringCallback callback) {
* Registers a SimValueCallback to be called whenever the specified value of this device is changed
* @param callback the callback function to call
* @param initialNotify if true
, calls the callback function with the current value
- * @return a ScopedObject which can be used to close the callback
+ * @return a Pair of the form (value, callback) that can be used to cancel the callback
* @see #cancelValueChangedCallback(Pair)
*/
- public ScopedObject> registerValueChangedCallback(String value, StringCallback callback, boolean initialNotify) {
+ public Pair registerValueChangedCallback(String value, StringCallback callback, boolean initialNotify) {
Pair callbackPair = new Pair<>(value, callback);
getState().valueChangedCallbacks.add(callbackPair);
if(initialNotify) {
callback.callback(value, get(value));
}
- return new ScopedObject<>(callbackPair, CANCEL_VALUE_CHANGED_CALLBACK);
+ return callbackPair;
}
- /**
- * A Consumer which calls {@link #cancelValueChangedCallback(Pair)}
- */
- public final Consumer> CANCEL_VALUE_CHANGED_CALLBACK = this::cancelValueChangedCallback;
/**
* Deregisters the given value changed callback
* @param callback the callback to deregister
@@ -239,22 +230,18 @@ public String[] enumerateValues(String prefix) {
* Registers a SimDeviceCallback to be called whenever a new SimDeviceSim is created. A SimDeviceSim is determined to be created if a value on it has been set.
* @param callback the callback function to call
* @param initialNotify if true
, calls the callback function with all created SimDeviceSims
- * @return a ScopedObject which can be used to close the callback
+ * @return a Pair of the form (prefix, callback) that can be used to cancel the callback
* @see #cancelDeviceCreatedCallback(Pair)
*/
- public static ScopedObject> registerDeviceCreatedCallback(String prefix, SimDeviceCallback callback, boolean initialNotify) {
+ public static Pair registerDeviceCreatedCallback(String prefix, SimDeviceCallback callback, boolean initialNotify) {
Pair callbackPair = new Pair<>(prefix, callback);
DEVICE_CALLBACKS.add(callbackPair);
if(initialNotify) {
Arrays.stream(enumerateDevices(prefix)).forEach(callback::callback);
}
- return new ScopedObject<>(callbackPair, CANCEL_DEVICE_CREATED_CALLBACK);
+ return callbackPair;
}
- /**
- * A Consumer which calls {@link #cancelDeviceCreatedCallback(Pair)}
- */
- public static final Consumer> CANCEL_DEVICE_CREATED_CALLBACK = SimDeviceSim::cancelDeviceCreatedCallback;
/**
* Deregisters the given device created callback
* @param callback the callback to deregister
diff --git a/asyncapi-template/template/$$schema$$.java b/asyncapi-template/template/$$schema$$.java
index 5bf31b2..c3f9619 100644
--- a/asyncapi-template/template/$$schema$$.java
+++ b/asyncapi-template/template/$$schema$$.java
@@ -31,7 +31,6 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;
-import org.team199.wpiws.ScopedObject;
import org.team199.wpiws.StateDevice;
import org.team199.wpiws.connection.ConnectionProcessor;
import org.team199.wpiws.connection.WSValue;
@@ -80,22 +79,18 @@ public class {{ name }}Sim {
* Registers a BooleanCallback to be called whenever {{ a }} {{ name }}Sim device is initialized or uninitialized
* @param callback the callback function to call
* @param initialNotify if true
, calls the callback function with the device identifiers of all currently initialized {{ name }}Sims
- * @return a ScopedObject which can be used to close the callback
+ * @return the callback object that can be used to cancel the callback
* @see #cancelStaticInitializedCallback(BooleanCallback)
* @see #registerInitializedCallback(BooleanCallback, boolean)
*/
- public static ScopedObject registerStaticInitializedCallback(BooleanCallback callback, boolean initialNotify) {
+ public static BooleanCallback registerStaticInitializedCallback(BooleanCallback callback, boolean initialNotify) {
STATIC_INITIALIZED_CALLBACKS.add(callback);
if(initialNotify) {
INITIALIZED_DEVICES.forEach(device -> callback.callback(device, true));
}
- return new ScopedObject<>(callback, CANCEL_STATIC_INITIALIZED_CALLBACK);
+ return callback;
}
- /**
- * A Consumer which calls {@link #cancelStaticInitializedCallback(BooleanCallback)}
- */
- public static final Consumer CANCEL_STATIC_INITIALIZED_CALLBACK = {{ name }}Sim::cancelStaticInitializedCallback;
/**
* Deregisters the given static initialized callback
* @param callback the callback to deregister
@@ -109,22 +104,18 @@ public static void cancelStaticInitializedCallback(BooleanCallback callback) {
* Registers a BooleanCallback to be called whenever this device is initialized or uninitialized
* @param callback the callback function to call
* @param initialNotify if true
, calls the callback function with this device's current initialized state
- * @return a ScopedObject which can be used to close the callback
+ * @return the callback object that can be used to cancel the callback
* @see #cancelInitializedCallback(BooleanCallback)
* @see #registerStaticInitializedCallback(BooleanCallback, boolean)
*/
- public ScopedObject registerInitializedCallback(BooleanCallback callback, boolean initialNotify) {
+ public BooleanCallback registerInitializedCallback(BooleanCallback callback, boolean initialNotify) {
getState().INITIALIZED_CALLBACKS.add(callback);
if(initialNotify) {
callback.callback(id, getState().init);
}
- return new ScopedObject<>(callback, CANCEL_INITIALIZED_CALLBACK);
+ return callback;
}
- /**
- * A Consumer which calls {@link #cancelInitializedCallback(BooleanCallback)}
- */
- public final Consumer CANCEL_INITIALIZED_CALLBACK = this::cancelInitializedCallback;
/**
* Deregisters the given initialized callback
* @param callback the callback to deregister
@@ -196,21 +187,17 @@ protected State generateState() {
* Registers a {{ varInfo.ptype }}Callback to be called whenever the {{ varInfo.pnamel }} of this device is changed
* @param callback the callback function to call
* @param initialNotify if true
, calls the callback function with this device's current {{ varInfo.pnamel }} value
- * @return a ScopedObject which can be used to close the callback
- * @see #cancel{{ varInfo.pname }}Callback({{ varInfo.ptype }}Callback)
+ * @return the callback object that can be used to cancel the callback
+ * @see #cancel{{ varInfo.pname }}Callback({{ varInfo.callbackType }}Callback)
*/
- public{{ cstatic }}ScopedObject<{{ varInfo.ptype }}Callback> register{{ varInfo.pname }}Callback({{ varInfo.ptype }}Callback callback, boolean initialNotify) {
+ public{{ cstatic }}{{ varInfo.ptype }}Callback register{{ varInfo.pname }}Callback({{ varInfo.ptype }}Callback callback, boolean initialNotify) {
getState().{{ varInfo.pnameu }}_CALLBACKS.add(callback);
if(initialNotify) {
callback.callback({{ cid }}, getState().{{ varInfo.pnamel }});
}
- return new ScopedObject<>(callback, CANCEL_{{ varInfo.pnameu }}_CALLBACK);
+ return callback;
}
- /**
- * A Consumer which calls {@link #cancel{{ varInfo.pname }}Callback({{ varInfo.ptype }}Callback)}
- */
- public{{ cstatic }}final Consumer<{{ varInfo.ptype }}Callback> CANCEL_{{ varInfo.pnameu }}_CALLBACK = {{ cthis }}::cancel{{ varInfo.pname }}Callback;
/**
* Deregisters the given {{ varInfo.pnamel }} callback
* @param callback the callback to deregister
diff --git a/src/main/java/org/team199/wpiws/ScopedObject.java b/src/main/java/org/team199/wpiws/ScopedObject.java
deleted file mode 100644
index 5ba5ffe..0000000
--- a/src/main/java/org/team199/wpiws/ScopedObject.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package org.team199.wpiws;
-
-import java.util.function.Consumer;
-
-import org.team199.wpiws.interfaces.ExceptionConsumer;
-
-/**
- * Represents an object which has a destructor
- * @param T the object type held by this ScopedObject
- */
-public class ScopedObject implements AutoCloseable {
-
- private final T object;
- private final Consumer closer;
- private boolean isClosed;
-
- /**
- * Creates a ScopedObject from an AutoClosable and assigns its close method to {@link AutoCloseable#close()}
- * @param the type of the object
- * @param object the object
- * @return a ScopedObject with its closer assigned to {@link AutoCloseable#close()}
- */
- public static ScopedObject fromAutoClosable(T object) {
- return new ScopedObject(object, createCloser(T::close));
- }
-
- /**
- * Creates a new ScopedObject
- * @param object the Object to which to add a destructor
- * @param closer a Consumer which will be fed with the object when it (the Object) is destroyed
- */
- public ScopedObject(T object, Consumer closer) {
- this.object = object;
- this.closer = closer;
- this.isClosed = false;
- }
-
- /**
- * Retrieves the object associeated with this ScopedObject
- * @return the object associated with this ScopedObject
- */
- public T getObject() {
- return object;
- }
-
- /**
- * Closes the underlying object if it has not already done so
- */
- @Override
- public void close() {
- if(isClosed) {
- return;
- }
- try {
- isClosed = true;
- closer.accept(object);
- } catch(Exception e) {
- e.printStackTrace(System.err);
- }
- }
-
- public boolean isClosed() {
- return isClosed;
- }
-
- @Override
- protected void finalize() throws Throwable {
- close();
- super.finalize();
- }
-
- /**
- * Creates a Consumer which will not throw an Exception
- * @param the type of the object consumed by the consumer
- * @param closer the Consumer to wrap
- * @return a Consumer which will call the given Consumer and will not throw an exception
- */
- public static Consumer createCloser(ExceptionConsumer closer) {
- return object -> {
- try {
- closer.accept(object);
- } catch(Exception e) {
- e.printStackTrace(System.err);
- }
- };
- }
-
-}