tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
| 2 | |
| 3 | import java.net.URI; |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 4 | import java.util.Objects; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 5 | |
| 6 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 7 | * Immutable representation of a device identity. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 8 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 9 | public final class DeviceId extends ElementId { |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 10 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Represents either no device, or an unspecified device. |
| 13 | */ |
| 14 | public static final DeviceId NONE = deviceId("none:none"); |
| 15 | |
| 16 | private final URI uri; |
| 17 | private final String str; |
| 18 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 19 | // Public construction is prohibited |
| 20 | private DeviceId(URI uri) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 21 | this.uri = uri; |
| 22 | this.str = uri.toString(); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | // Default constructor for serialization |
| 27 | protected DeviceId() { |
| 28 | this.uri = null; |
| 29 | this.str = null; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 30 | } |
| 31 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 32 | /** |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 33 | * Creates a device id using the supplied URI. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 34 | * |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 35 | * @param uri device URI |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 36 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 37 | public static DeviceId deviceId(URI uri) { |
| 38 | return new DeviceId(uri); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Creates a device id using the supplied URI string. |
| 43 | * |
| 44 | * @param string device URI string |
| 45 | */ |
| 46 | public static DeviceId deviceId(String string) { |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 47 | return deviceId(URI.create(string)); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 48 | } |
| 49 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Returns the backing URI. |
| 52 | * |
| 53 | * @return backing URI |
| 54 | */ |
| 55 | public URI uri() { |
| 56 | return uri; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int hashCode() { |
| 61 | return Objects.hash(str); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean equals(Object obj) { |
| 66 | if (this == obj) { |
| 67 | return true; |
| 68 | } |
| 69 | if (obj instanceof DeviceId) { |
| 70 | final DeviceId that = (DeviceId) obj; |
| 71 | return this.getClass() == that.getClass() && |
| 72 | Objects.equals(this.str, that.str); |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public String toString() { |
| 79 | return str; |
| 80 | } |
| 81 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 82 | } |