Skip to content

Commit

Permalink
Merge pull request #39 from DeepBlueRobotics/remove-scopedobject
Browse files Browse the repository at this point in the history
Delete ScopedObject to Resolve #35
  • Loading branch information
brettle committed May 27, 2024
2 parents a702186 + 9b3aac8 commit 2c3de83
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 133 deletions.
31 changes: 9 additions & 22 deletions asyncapi-template/partials/SimDeviceSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>true</code>, 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<StringCallback> 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<StringCallback> CANCEL_VALUE_CREATED_CALLBACK = this::cancelValueCreatedCallback;
/**
* Deregisters the given value created callback
* @param callback the callback to deregister
Expand All @@ -197,22 +192,18 @@ public void cancelValueCreatedCallback(StringCallback callback) {
* @param value the value to watch for changes
* @param callback the callback function to call
* @param initialNotify if <code>true</code>, 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<Pair<String, StringCallback>> registerValueChangedCallback(String value, StringCallback callback, boolean initialNotify) {
public Pair<String, StringCallback> registerValueChangedCallback(String value, StringCallback callback, boolean initialNotify) {
Pair<String, StringCallback> 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<Pair<String, StringCallback>> CANCEL_VALUE_CHANGED_CALLBACK = this::cancelValueChangedCallback;
/**
* Deregisters the given value changed callback
* @param callback the callback to deregister
Expand Down Expand Up @@ -241,22 +232,18 @@ public String[] enumerateValues(String prefix) {
* @param prefix the prefix to search for
* @param callback the callback function to call
* @param initialNotify if <code>true</code>, 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<Pair<String, SimDeviceCallback>> registerDeviceCreatedCallback(String prefix, SimDeviceCallback callback, boolean initialNotify) {
public static Pair<String, SimDeviceCallback> registerDeviceCreatedCallback(String prefix, SimDeviceCallback callback, boolean initialNotify) {
Pair<String, SimDeviceCallback> 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<Pair<String, SimDeviceCallback>> CANCEL_DEVICE_CREATED_CALLBACK = SimDeviceSim::cancelDeviceCreatedCallback;
/**
* Deregisters the given device created callback
* @param callback the callback to deregister
Expand Down
33 changes: 10 additions & 23 deletions asyncapi-template/template/$$schema$$.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>true</code>, 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<BooleanCallback> 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<BooleanCallback> CANCEL_STATIC_INITIALIZED_CALLBACK = {{ name }}Sim::cancelStaticInitializedCallback;
/**
* Deregisters the given static initialized callback
* @param callback the callback to deregister
Expand All @@ -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 <code>true</code>, 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<BooleanCallback> 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<BooleanCallback> CANCEL_INITIALIZED_CALLBACK = this::cancelInitializedCallback;
/**
* Deregisters the given initialized callback
* @param callback the callback to deregister
Expand Down Expand Up @@ -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 <code>true</code>, 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
Expand Down
88 changes: 0 additions & 88 deletions src/main/java/org/team199/wpiws/ScopedObject.java

This file was deleted.

0 comments on commit 2c3de83

Please sign in to comment.