blob: d985be862580e52df98d2ec107772577d6460274 [file] [log] [blame]
tomb36046e2014-08-27 00:22:24 -07001package org.onlab.onos.net;
2
3import java.net.URI;
4import java.util.Objects;
5
tomb36046e2014-08-27 00:22:24 -07006/**
7 * Immutable representation of a network element identity.
8 */
tomca20e0c2014-09-03 23:22:24 -07009public abstract class ElementId {
tomb36046e2014-08-27 00:22:24 -070010
11 private final URI uri;
tom1679e182014-10-09 13:50:45 -070012 private final String str;
tomb36046e2014-08-27 00:22:24 -070013
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070014 // Default constructor for serialization
15 protected ElementId() {
16 this.uri = null;
tom1679e182014-10-09 13:50:45 -070017 this.str = null;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070018 }
19
tomb36046e2014-08-27 00:22:24 -070020 /**
21 * Creates an element identifier using the supplied URI.
22 *
23 * @param uri backing URI
24 */
tomca20e0c2014-09-03 23:22:24 -070025 protected ElementId(URI uri) {
tomb36046e2014-08-27 00:22:24 -070026 this.uri = uri;
tom1679e182014-10-09 13:50:45 -070027 this.str = uri.toString();
tomb36046e2014-08-27 00:22:24 -070028 }
29
30 /**
31 * Returns the backing URI.
32 *
33 * @return backing URI
34 */
35 public URI uri() {
36 return uri;
37 }
38
39 @Override
40 public int hashCode() {
Yuta HIGUCHIf5ba8bc2014-10-09 16:02:29 -070041 return Objects.hash(uri);
tomb36046e2014-08-27 00:22:24 -070042 }
43
44 @Override
45 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070046 if (this == obj) {
47 return true;
48 }
tomca20e0c2014-09-03 23:22:24 -070049 if (obj instanceof ElementId) {
50 final ElementId that = (ElementId) obj;
51 return this.getClass() == that.getClass() &&
Yuta HIGUCHIf5ba8bc2014-10-09 16:02:29 -070052 Objects.equals(this.uri, that.uri);
tomb36046e2014-08-27 00:22:24 -070053 }
tomca20e0c2014-09-03 23:22:24 -070054 return false;
tomb36046e2014-08-27 00:22:24 -070055 }
56
57 @Override
58 public String toString() {
tom1679e182014-10-09 13:50:45 -070059 return str;
tomb36046e2014-08-27 00:22:24 -070060 }
61
62}