Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 17 | |
| 18 | import java.net.URI; |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 19 | import java.util.Objects; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 22 | * Immutable representation of a device identity. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 23 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 24 | public final class DeviceId extends ElementId { |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 25 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Represents either no device, or an unspecified device. |
| 28 | */ |
| 29 | public static final DeviceId NONE = deviceId("none:none"); |
| 30 | |
| 31 | private final URI uri; |
| 32 | private final String str; |
| 33 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 34 | // Public construction is prohibited |
| 35 | private DeviceId(URI uri) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 36 | this.uri = uri; |
Thomas Vachuska | 2292567 | 2014-11-11 17:57:53 -0800 | [diff] [blame] | 37 | this.str = uri.toString().toLowerCase(); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
| 41 | // Default constructor for serialization |
| 42 | protected DeviceId() { |
| 43 | this.uri = null; |
| 44 | this.str = null; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 45 | } |
| 46 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 47 | /** |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 48 | * Creates a device id using the supplied URI. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 49 | * |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 50 | * @param uri device URI |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 51 | * @return DeviceId |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 52 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 53 | public static DeviceId deviceId(URI uri) { |
| 54 | return new DeviceId(uri); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a device id using the supplied URI string. |
| 59 | * |
| 60 | * @param string device URI string |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 61 | * @return DeviceId |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 62 | */ |
| 63 | public static DeviceId deviceId(String string) { |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 64 | return deviceId(URI.create(string)); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 65 | } |
| 66 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Returns the backing URI. |
| 69 | * |
| 70 | * @return backing URI |
| 71 | */ |
| 72 | public URI uri() { |
| 73 | return uri; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 78 | return str.hashCode(); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean equals(Object obj) { |
| 83 | if (this == obj) { |
| 84 | return true; |
| 85 | } |
| 86 | if (obj instanceof DeviceId) { |
| 87 | final DeviceId that = (DeviceId) obj; |
| 88 | return this.getClass() == that.getClass() && |
| 89 | Objects.equals(this.str, that.str); |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String toString() { |
| 96 | return str; |
| 97 | } |
| 98 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 99 | } |