blob: b4dab2d213fd274f579fa298948c017f59f17892 [file] [log] [blame]
Brian O'Connor055fb972015-02-05 11:40:51 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Brian O'Connor055fb972015-02-05 11:40:51 -08003 *
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 */
16package org.onosproject.net.intent;
17
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Brian O'Connor055fb972015-02-05 11:40:51 -080019import com.google.common.hash.HashFunction;
20import com.google.common.hash.Hashing;
21import org.onosproject.core.ApplicationId;
22
23import java.nio.charset.StandardCharsets;
24import java.util.Objects;
25
26/**
27 * Key class for Intents.
28 */
29// TODO maybe pull this up to utils
Brian O'Connor9476fa12015-06-25 15:17:17 -040030@Beta
Simon Hunte2b6a2b2015-10-27 11:38:51 -070031public abstract class Key implements Comparable<Key> {
Brian O'Connor055fb972015-02-05 11:40:51 -080032
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080033 //TODO consider making this a HashCode object (worry about performance)
Brian O'Connor055fb972015-02-05 11:40:51 -080034 private final long hash;
35 private static final HashFunction HASH_FN = Hashing.md5();
36
Ray Milkey5b3717e2015-02-05 11:44:08 -080037 protected Key(long hash) {
Brian O'Connor055fb972015-02-05 11:40:51 -080038 this.hash = hash;
39 }
40
41 public long hash() {
42 return hash;
43 }
44
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080045 @Override
46 public int hashCode() {
Sho SHIMIZU87114512015-05-08 14:31:00 -070047 return Long.hashCode(hash);
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080048 }
49
50 @Override
51 public abstract boolean equals(Object obj);
52
53 /**
54 * Creates a key based on the provided string.
55 * <p>
56 * Note: Two keys with equal value, but different appId, are not equal.
57 * </p>
58 *
59 * @param key the provided string
60 * @param appId application id to associate with this key
61 * @return the key for the string
62 */
Brian O'Connor055fb972015-02-05 11:40:51 -080063 public static Key of(String key, ApplicationId appId) {
64 return new StringKey(key, appId);
65 }
66
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080067 /**
68 * Creates a key based on the provided long.
69 * <p>
70 * Note: Two keys with equal value, but different appId, are not equal.
71 * Also, "10" and 10L are different.
72 * </p>
73 *
74 * @param key the provided long
75 * @param appId application id to associate with this key
76 * @return the key for the long
77 */
Brian O'Connor055fb972015-02-05 11:40:51 -080078 public static Key of(long key, ApplicationId appId) {
79 return new LongKey(key, appId);
80 }
81
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080082 private static final class StringKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -080083
84 private final ApplicationId appId;
85 private final String key;
86
87 private StringKey(String key, ApplicationId appId) {
88 super(HASH_FN.newHasher()
89 .putShort(appId.id())
90 .putString(key, StandardCharsets.UTF_8)
91 .hash().asLong());
92 this.key = key;
93 this.appId = appId;
94 }
95
96 @Override
97 public String toString() {
98 return key;
99 }
100
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800101 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800102 @Override
103 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800104 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112 if (obj == null || getClass() != obj.getClass()) {
113 return false;
114 }
115 final StringKey other = (StringKey) obj;
116 return this.hash() == other.hash() &&
117 Objects.equals(this.appId, other.appId) &&
118 Objects.equals(this.key, other.key);
119 }
Simon Hunte2b6a2b2015-10-27 11:38:51 -0700120
121 @Override
122 public int compareTo(Key o) {
123 StringKey sk = (StringKey) o;
124 return this.key.compareTo(sk.key);
125 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800126 }
127
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800128 private static final class LongKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -0800129
130 private final ApplicationId appId;
Ray Milkey5ad39662015-02-10 15:12:14 -0800131 private final long key;
Brian O'Connor055fb972015-02-05 11:40:51 -0800132
133 private LongKey(long key, ApplicationId appId) {
134 super(HASH_FN.newHasher()
135 .putShort(appId.id())
136 .putLong(key)
137 .hash().asLong());
138 this.key = key;
139 this.appId = appId;
140 }
141
142 @Override
143 public String toString() {
Ray Milkey3af13862015-02-10 15:23:13 -0800144 return "0x" + Long.toHexString(key);
Brian O'Connor055fb972015-02-05 11:40:51 -0800145 }
146
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800147 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800148 @Override
149 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800150 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800151 }
152
153 @Override
154 public boolean equals(Object obj) {
155 if (this == obj) {
156 return true;
157 }
158 if (obj == null || getClass() != obj.getClass()) {
159 return false;
160 }
161 final LongKey other = (LongKey) obj;
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800162 return this.hash() == other.hash() &&
163 this.key == other.key &&
164 Objects.equals(this.appId, other.appId);
Brian O'Connor055fb972015-02-05 11:40:51 -0800165 }
Simon Hunte2b6a2b2015-10-27 11:38:51 -0700166
167 @Override
168 public int compareTo(Key o) {
169 Long myKey = key;
170 Long otherKey = ((LongKey) o).key;
171 return myKey.compareTo(otherKey);
172 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800173 }
174}
175
176