[wpiutil] Add Struct and Protobuf clone and immutable checks (#6686)

This commit is contained in:
Peter Johnson
2024-06-01 11:58:53 -07:00
committed by GitHub
parent ae8e4289b9
commit 7c8a36f3eb
20 changed files with 156 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -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();
}
}