blob: 6e73a5bc7d8eb074b06d87b489b752ef2ad7da87 [file] [log] [blame]
Aihua Guo1ce2dd12016-08-12 23:37:44 -04001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16
17package org.onosproject.tetopology.management.api;
18
19import java.net.URI;
20import java.util.Objects;
21
22/**
23 * Representation of an key identifier in URI.
24 *
25 */
26public class KeyId {
27 /**
28 * Represents either no uri, or an unspecified uri.
29 */
30 public static final KeyId NONE = keyId("none:none");
31
32 private final URI uri;
33 private final String str;
34
35 // Public construction is prohibited
36 private KeyId(URI uri) {
37 this.uri = uri;
38 //this.str = uri.toString().toLowerCase();
39 this.str = uri.toString();
40 }
41
42
43 /**
44 * Default constructor for serialization of KeyId.
45 */
46 protected KeyId() {
47 this.uri = null;
48 this.str = null;
49 }
50
51 /**
52 * Returns the backing URI.
53 *
54 * @return backing URI
55 */
56 public URI uri() {
57 return uri;
58 }
59
60 @Override
61 public int hashCode() {
62 return str.hashCode();
63 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (this == obj) {
68 return true;
69 }
70 if (obj instanceof KeyId) {
71 KeyId that = (KeyId) obj;
72 return this.getClass() == that.getClass() &&
73 Objects.equals(this.str, that.str);
74 }
75 return false;
76 }
77
78 @Override
79 public String toString() {
80 return str;
81 }
82
83 /**
84 * Creates a uri id using the supplied URI.
85 *
86 * @param uri URI
87 * @return UriId
88 */
89 public static KeyId keyId(URI uri) {
90 return new KeyId(uri);
91 }
92
93 /**
94 * Creates a uri id using the supplied URI string.
95 *
96 * @param string URI string
97 * @return UriId
98 */
99 public static KeyId keyId(String string) {
100 return keyId(URI.create(string));
101 }
102
103}