[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

@@ -44,4 +44,9 @@ public class Pose2dProto implements Protobuf<Pose2d, ProtobufPose2d> {
Translation2d.proto.pack(msg.getMutableTranslation(), value.getTranslation());
Rotation2d.proto.pack(msg.getMutableRotation(), value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -44,4 +44,9 @@ public class Pose3dProto implements Protobuf<Pose3d, ProtobufPose3d> {
Translation3d.proto.pack(msg.getMutableTranslation(), value.getTranslation());
Rotation3d.proto.pack(msg.getMutableRotation(), value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -37,4 +37,9 @@ public class QuaternionProto implements Protobuf<Quaternion, ProtobufQuaternion>
msg.setY(value.getY());
msg.setZ(value.getZ());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -34,4 +34,9 @@ public class Rotation2dProto implements Protobuf<Rotation2d, ProtobufRotation2d>
public void pack(ProtobufRotation2d msg, Rotation2d value) {
msg.setValue(value.getRadians());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -40,4 +40,9 @@ public class Rotation3dProto implements Protobuf<Rotation3d, ProtobufRotation3d>
public void pack(ProtobufRotation3d msg, Rotation3d value) {
Quaternion.proto.pack(msg.getMutableQ(), value.getQuaternion());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -44,4 +44,9 @@ public class Transform2dProto implements Protobuf<Transform2d, ProtobufTransform
Translation2d.proto.pack(msg.getMutableTranslation(), value.getTranslation());
Rotation2d.proto.pack(msg.getMutableRotation(), value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -44,4 +44,9 @@ public class Transform3dProto implements Protobuf<Transform3d, ProtobufTransform
Translation3d.proto.pack(msg.getMutableTranslation(), value.getTranslation());
Rotation3d.proto.pack(msg.getMutableRotation(), value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -35,4 +35,9 @@ public class Translation2dProto implements Protobuf<Translation2d, ProtobufTrans
msg.setX(value.getX());
msg.setY(value.getY());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -36,4 +36,9 @@ public class Translation3dProto implements Protobuf<Translation3d, ProtobufTrans
msg.setY(value.getY());
msg.setZ(value.getZ());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -48,4 +48,9 @@ public class Pose2dStruct implements Struct<Pose2d> {
Translation2d.struct.pack(bb, value.getTranslation());
Rotation2d.struct.pack(bb, value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -48,4 +48,9 @@ public class Pose3dStruct implements Struct<Pose3d> {
Translation3d.struct.pack(bb, value.getTranslation());
Rotation3d.struct.pack(bb, value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -45,4 +45,9 @@ public class QuaternionStruct implements Struct<Quaternion> {
bb.putDouble(value.getY());
bb.putDouble(value.getZ());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -39,4 +39,9 @@ public class Rotation2dStruct implements Struct<Rotation2d> {
public void pack(ByteBuffer bb, Rotation2d value) {
bb.putDouble(value.getRadians());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -45,4 +45,9 @@ public class Rotation3dStruct implements Struct<Rotation3d> {
public void pack(ByteBuffer bb, Rotation3d value) {
Quaternion.struct.pack(bb, value.getQuaternion());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -48,4 +48,9 @@ public class Transform2dStruct implements Struct<Transform2d> {
Translation2d.struct.pack(bb, value.getTranslation());
Rotation2d.struct.pack(bb, value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -48,4 +48,9 @@ public class Transform3dStruct implements Struct<Transform3d> {
Translation3d.struct.pack(bb, value.getTranslation());
Rotation3d.struct.pack(bb, value.getRotation());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -41,4 +41,9 @@ public class Translation2dStruct implements Struct<Translation2d> {
bb.putDouble(value.getX());
bb.putDouble(value.getY());
}
@Override
public boolean isImmutable() {
return true;
}
}

View File

@@ -43,4 +43,9 @@ public class Translation3dStruct implements Struct<Translation3d> {
bb.putDouble(value.getY());
bb.putDouble(value.getZ());
}
@Override
public boolean isImmutable() {
return true;
}
}

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