tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
2 | |||||
3 | import java.net.URI; | ||||
4 | import java.util.Objects; | ||||
5 | |||||
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 6 | /** |
7 | * Immutable representation of a network element identity. | ||||
8 | */ | ||||
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 9 | public abstract class ElementId { |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 10 | |
11 | private final URI uri; | ||||
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 12 | private final String str; |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 13 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 14 | // Default constructor for serialization |
15 | protected ElementId() { | ||||
16 | this.uri = null; | ||||
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 17 | this.str = null; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 18 | } |
19 | |||||
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 20 | /** |
21 | * Creates an element identifier using the supplied URI. | ||||
22 | * | ||||
23 | * @param uri backing URI | ||||
24 | */ | ||||
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 25 | protected ElementId(URI uri) { |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 26 | this.uri = uri; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 27 | this.str = uri.toString(); |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 28 | } |
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 HIGUCHI | f5ba8bc | 2014-10-09 16:02:29 -0700 | [diff] [blame] | 41 | return Objects.hash(uri); |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 42 | } |
43 | |||||
44 | @Override | ||||
45 | public boolean equals(Object obj) { | ||||
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 46 | if (this == obj) { |
47 | return true; | ||||
48 | } | ||||
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 49 | if (obj instanceof ElementId) { |
50 | final ElementId that = (ElementId) obj; | ||||
51 | return this.getClass() == that.getClass() && | ||||
Yuta HIGUCHI | f5ba8bc | 2014-10-09 16:02:29 -0700 | [diff] [blame] | 52 | Objects.equals(this.uri, that.uri); |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 53 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 54 | return false; |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 55 | } |
56 | |||||
57 | @Override | ||||
58 | public String toString() { | ||||
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 59 | return str; |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 60 | } |
61 | |||||
62 | } |