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>
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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());
+ }
+}
--- /dev/null
+/*
+ * 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());
+ }
+}
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();
}
}
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();
}
}