mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpiutil] Add Struct and Protobuf clone and immutable checks (#6686)
This commit is contained in:
@@ -97,6 +97,39 @@ public interface Protobuf<T, MessageType extends ProtoMessage<?>> {
|
||||
throw new UnsupportedOperationException("object does not support unpackInto");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not objects are immutable. Immutable objects must also be comparable using
|
||||
* the equals() method. Default implementation returns false.
|
||||
*
|
||||
* @return True if object is immutable
|
||||
*/
|
||||
default boolean isImmutable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not objects are cloneable using the clone() method. Clonable objects must
|
||||
* also be comparable using the equals() method. Default implementation returns false.
|
||||
*
|
||||
* @return True if object is clonable
|
||||
*/
|
||||
default boolean isCloneable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a (deep) clone of the object. May also return the object directly if the object is
|
||||
* immutable. Default implementation throws CloneNotSupportedException. Typically this should be
|
||||
* implemented by implementing clone() on the object itself, and calling it from here.
|
||||
*
|
||||
* @param obj object to clone
|
||||
* @return Clone of object (if immutable, may be same object)
|
||||
* @throws CloneNotSupportedException if clone not supported
|
||||
*/
|
||||
default T clone(T obj) throws CloneNotSupportedException {
|
||||
throw new CloneNotSupportedException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loops over all protobuf descriptors including nested/referenced descriptors.
|
||||
*
|
||||
|
||||
@@ -113,4 +113,37 @@ public interface Struct<T> {
|
||||
default void unpackInto(T out, ByteBuffer bb) {
|
||||
throw new UnsupportedOperationException("object does not support unpackInto");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not objects are immutable. Immutable objects must also be comparable using
|
||||
* the equals() method. Default implementation returns false.
|
||||
*
|
||||
* @return True if object is immutable
|
||||
*/
|
||||
default boolean isImmutable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not objects are cloneable using the clone() method. Clonable objects must
|
||||
* also be comparable using the equals() method. Default implementation returns false.
|
||||
*
|
||||
* @return True if object is clonable
|
||||
*/
|
||||
default boolean isCloneable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a (deep) clone of the object. May also return the object directly if the object is
|
||||
* immutable. Default implementation throws CloneNotSupportedException. Typically this should be
|
||||
* implemented by implementing clone() on the object itself, and calling it from here.
|
||||
*
|
||||
* @param obj object to clone
|
||||
* @return Clone of object (if immutable, may be same object)
|
||||
* @throws CloneNotSupportedException if clone not supported
|
||||
*/
|
||||
default T clone(T obj) throws CloneNotSupportedException {
|
||||
throw new CloneNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user