X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FUuidIdentified.java;fp=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FUuidIdentified.java;h=77cab371e89e36ee7df02c1f1dd29513971d5c54;hb=369abbec35158f11bcca3651c1c3f2f7d6652226;hp=0000000000000000000000000000000000000000;hpb=40b08908d2519f46bce6d1bc3364224100c59c28;p=lgpl%2Fargeo-commons.git
diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidIdentified.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidIdentified.java
new file mode 100644
index 000000000..77cab371e
--- /dev/null
+++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidIdentified.java
@@ -0,0 +1,57 @@
+package org.argeo.api.uuid;
+
+import java.util.UUID;
+
+/**
+ * An object identified by a {@link UUID}. Typically used to fasten indexing and
+ * comparisons of objects or records. THe method to implement is {@link #uuid()}
+ * so that any record with an uuid
field can easily be enriched
+ * with this interface.
+ */
+public interface UuidIdentified {
+ /** The UUID identifier. */
+ UUID uuid();
+
+ /** The UUID identifier, for compatibility with beans accessors. */
+ default UUID getUuid() {
+ return uuid();
+ }
+
+ /**
+ * Helper to implement the equals method of an {@link UuidIdentified}.
+ *
+ *
+ * @Override + * public boolean equals(Object o) { + * return UuidIdentified.equals(this, o); + * } + *+ */ + static boolean equals(UuidIdentified uuidIdentified, Object o) { + assert uuidIdentified != null; + if (o == null) + return false; + if (uuidIdentified == o) + return true; + if (o instanceof UuidIdentified u) + return uuidIdentified.uuid().equals(u.uuid()); + else + return false; + } + + /** + * Helper to implement the hash code method of an {@link UuidIdentified}.
+ * @Override + * public int hashCode() { + * return UuidIdentified.hashCode(this); + * } + *+ */ + static int hashCode(UuidIdentified uuidIdentified) { + assert uuidIdentified != null; + return uuidIdentified.getUuid().hashCode(); + } + +}