Deprecate {String,UUID}Identifier 40/39740/4
authorRobert Varga <rovarga@cisco.com>
Thu, 2 Jun 2016 00:21:01 +0000 (02:21 +0200)
committerTony Tkacik <ttkacik@cisco.com>
Mon, 6 Jun 2016 10:00:55 +0000 (10:00 +0000)
These identifiers are easigy mixed up if we do not allow for subclassing. Deprecate
the classes and provide their abstract equivalents. This allows for specific subclasses
which do not produce clashes.

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

diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/AbstractIdentifier.java b/common/util/src/main/java/org/opendaylight/yangtools/util/AbstractIdentifier.java
new file mode 100644 (file)
index 0000000..dbe5ca6
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import org.opendaylight.yangtools.concepts.Identifier;
+
+/**
+ * An abstract {@link Identifier} backed by an immutable object. Subclasses have no control over {@link #hashCode()}
+ * and {@link #equals(Object)}, hence they should not add any fields.
+ *
+ * @author Robert Varga
+ *
+ * @param <T> Object type
+ */
+public abstract class AbstractIdentifier<T> implements Identifier {
+    private static final long serialVersionUID = 1L;
+
+    private final T value;
+
+    public AbstractIdentifier(final T value) {
+        this.value = Preconditions.checkNotNull(value);
+    }
+
+    public final T getValue() {
+        return value;
+    }
+
+    @Override
+    public final int hashCode() {
+        return value.hashCode();
+    }
+
+    @Override
+    public final boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null) {
+            return false;
+        }
+
+        return getClass().equals(o.getClass()) && value.equals(((AbstractIdentifier<?>)o).value);
+    }
+
+    @Override
+    public final String toString() {
+        return MoreObjects.toStringHelper(this).add("value", value).toString();
+    }
+}
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/AbstractStringIdentifier.java b/common/util/src/main/java/org/opendaylight/yangtools/util/AbstractStringIdentifier.java
new file mode 100644 (file)
index 0000000..c1f0c25
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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 java.util.UUID;
+import org.opendaylight.yangtools.concepts.Identifier;
+
+/**
+ * Utility {@link Identifier} backed by a {@link UUID}.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public abstract class AbstractStringIdentifier<T extends AbstractStringIdentifier<T>>
+        extends AbstractIdentifier<String> implements Comparable<T> {
+    private static final long serialVersionUID = 1L;
+
+    protected AbstractStringIdentifier(final String string) {
+        super(string);
+    }
+
+    @Override
+    public final int compareTo(final T o) {
+        return getValue().compareTo(o.getValue());
+    }
+}
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/AbstractUUIDIdentifier.java b/common/util/src/main/java/org/opendaylight/yangtools/util/AbstractUUIDIdentifier.java
new file mode 100644 (file)
index 0000000..6382eb0
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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 java.util.UUID;
+import org.opendaylight.yangtools.concepts.Identifier;
+
+/**
+ * Utility {@link Identifier} backed by a {@link UUID}.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public abstract class AbstractUUIDIdentifier<T extends AbstractUUIDIdentifier<T>> extends AbstractIdentifier<UUID>
+        implements Comparable<T> {
+    private static final long serialVersionUID = 1L;
+
+    protected AbstractUUIDIdentifier(final UUID uuid) {
+        super(uuid);
+    }
+
+    @Override
+    public final int compareTo(final T o) {
+        return getValue().compareTo(o.getValue());
+    }
+}
index 9b7ec5694fa872ee3df0a4a92df92c5583bfebbd..232f652d159d6994acb5f079ec1bde285bf9dc0f 100644 (file)
@@ -8,43 +8,29 @@
 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}.
+ *
+ * @deprecated Treats instantiations as equal, not providing safety against mixing instances from different modules.
+ *             Use a subclass of {@link AbstractStringIdentifier} instead.
  */
+@Deprecated
 @Beta
-public final class StringIdentifier implements Identifier, Comparable<StringIdentifier> {
+public final class StringIdentifier extends AbstractStringIdentifier<StringIdentifier> {
     private static final long serialVersionUID = 1L;
-    private final String string;
 
     public StringIdentifier(final String string) {
-        this.string = Preconditions.checkNotNull(string);
+        super(string);
     }
 
+    /**
+     * @deprecated use {@link #getValue()} instead.
+     * @return
+     */
+    @Deprecated
     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);
+        return getValue();
     }
 }
index b56b4ad98a05d07f3c18c10038f462e53e96cb96..2f448fff53b1995d52c1e889ed56766ea706f339 100644 (file)
@@ -8,44 +8,29 @@
 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}.
+ *
+ * @deprecated Treats instantiations as equal, not providing safety against mixing instances from different modules.
+ *             Use a subclass of {@link AbstractUUIDIdentifier} instead.
  */
+@Deprecated
 @Beta
-public final class UUIDIdentifier implements Identifier, Comparable<UUIDIdentifier> {
+public final class UUIDIdentifier extends AbstractUUIDIdentifier<UUIDIdentifier> {
     private static final long serialVersionUID = 1L;
-    private final UUID uuid;
 
     public UUIDIdentifier(final UUID uuid) {
-        this.uuid = Preconditions.checkNotNull(uuid);
+        super(uuid);
     }
 
+    /**
+     * @deprecated Use {@link #getValue()} instead.
+     */
+    @Deprecated
     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);
+        return getValue();
     }
 }