blob: 18baafc8c1f05f90b8ef475833653d3c7fa148db [file] [log] [blame]
Brian O'Connor055fb972015-02-05 11:40:51 -08001/*
2 * Copyright 2015 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 */
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
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080031public abstract class 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 }
120 }
121
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800122 private static final class LongKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -0800123
124 private final ApplicationId appId;
Ray Milkey5ad39662015-02-10 15:12:14 -0800125 private final long key;
Brian O'Connor055fb972015-02-05 11:40:51 -0800126
127 private LongKey(long key, ApplicationId appId) {
128 super(HASH_FN.newHasher()
129 .putShort(appId.id())
130 .putLong(key)
131 .hash().asLong());
132 this.key = key;
133 this.appId = appId;
134 }
135
136 @Override
137 public String toString() {
Ray Milkey3af13862015-02-10 15:23:13 -0800138 return "0x" + Long.toHexString(key);
Brian O'Connor055fb972015-02-05 11:40:51 -0800139 }
140
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800141 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800142 @Override
143 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800144 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800145 }
146
147 @Override
148 public boolean equals(Object obj) {
149 if (this == obj) {
150 return true;
151 }
152 if (obj == null || getClass() != obj.getClass()) {
153 return false;
154 }
155 final LongKey other = (LongKey) obj;
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800156 return this.hash() == other.hash() &&
157 this.key == other.key &&
158 Objects.equals(this.appId, other.appId);
Brian O'Connor055fb972015-02-05 11:40:51 -0800159 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800160 }
161}
162
163