BUG-5626: introduce utility identifiers 21/38621/2
authorRobert Varga <rovarga@cisco.com>
Tue, 10 May 2016 09:48:48 +0000 (11:48 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 10 May 2016 10:19:23 +0000 (12:19 +0200)
Wrapping String and UUID into an Identifier is useful for bridging worlds
with and without Identifiers. Introduce dedicated wrappers for these two
common cases.

Change-Id: Ib020c34512a826c23099601e3a57d558aa324976
Signed-off-by: Robert Varga <rovarga@cisco.com>
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Identifier.java
common/util/src/main/java/org/opendaylight/yangtools/util/StringIdentifier.java [new file with mode: 0644]
common/util/src/main/java/org/opendaylight/yangtools/util/UUIDIdentifier.java [new file with mode: 0644]

index 148865b850cec84120517d8a6451e66072b6d441..96696d7b656dd2cd2f8540bc6b0d1270af0dfabf 100644 (file)
@@ -14,8 +14,22 @@ import java.io.Serializable;
  * identify concepts -- such as names, addresses, classes, etc. We do not
  * require too much, just that the identifiers are serializable (and this
  * transferable).
+ *
+ * Implementations are expected to implement hashCode() and equals() methods
+ * in a way, which ensures that objects before and after serialization are
+ * considered equal.
+ *
+ * Implementations are advised to use the Externalizable Proxy pattern to
+ * allow future evolution of their serialization format.
  */
 public interface Identifier extends Serializable, Immutable {
+    @Override
+    boolean equals(Object o);
+
+    @Override
+    int hashCode();
 
+    @Override
+    String toString();
 }
 
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/StringIdentifier.java b/common/util/src/main/java/org/opendaylight/yangtools/util/StringIdentifier.java
new file mode 100644 (file)
index 0000000..9b7ec56
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import org.opendaylight.yangtools.concepts.Identifier;
+
+/**
+ * Utility {@link Identifier} backed by a {@link String}.
+ */
+@Beta
+public final class StringIdentifier implements Identifier, Comparable<StringIdentifier> {
+    private static final long serialVersionUID = 1L;
+    private final String string;
+
+    public StringIdentifier(final String string) {
+        this.string = Preconditions.checkNotNull(string);
+    }
+
+    public String getString() {
+        return string;
+    }
+
+    @Override
+    public int hashCode() {
+        return string.hashCode();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        return this == o || (o instanceof StringIdentifier && string.equals(((StringIdentifier)o).string));
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(StringIdentifier.class).add("string", string).toString();
+    }
+
+    @Override
+    public int compareTo(final StringIdentifier o) {
+        return string.compareTo(o.string);
+    }
+}
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/UUIDIdentifier.java b/common/util/src/main/java/org/opendaylight/yangtools/util/UUIDIdentifier.java
new file mode 100644 (file)
index 0000000..b56b4ad
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import java.util.UUID;
+import org.opendaylight.yangtools.concepts.Identifier;
+
+/**
+ * Utility {@link Identifier} backed by a {@link UUID}.
+ */
+@Beta
+public final class UUIDIdentifier implements Identifier, Comparable<UUIDIdentifier> {
+    private static final long serialVersionUID = 1L;
+    private final UUID uuid;
+
+    public UUIDIdentifier(final UUID uuid) {
+        this.uuid = Preconditions.checkNotNull(uuid);
+    }
+
+    public UUID getUuid() {
+        return uuid;
+    }
+
+    @Override
+    public int hashCode() {
+        return uuid.hashCode();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        return this == o || (o instanceof UUIDIdentifier && uuid.equals(((UUIDIdentifier)o).uuid));
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(UUIDIdentifier.class).add("uuid", uuid).toString();
+    }
+
+    @Override
+    public int compareTo(final UUIDIdentifier o) {
+        return uuid.compareTo(o.uuid);
+    }
+}