blob: dc9ef36de1fe40287e0f8704c69a99e2a1d530c7 [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
18import com.google.common.hash.HashFunction;
19import com.google.common.hash.Hashing;
20import org.onosproject.core.ApplicationId;
21
22import java.nio.charset.StandardCharsets;
23import java.util.Objects;
24
25/**
26 * Key class for Intents.
27 */
28// TODO maybe pull this up to utils
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080029public abstract class Key {
Brian O'Connor055fb972015-02-05 11:40:51 -080030
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080031 //TODO consider making this a HashCode object (worry about performance)
Brian O'Connor055fb972015-02-05 11:40:51 -080032 private final long hash;
33 private static final HashFunction HASH_FN = Hashing.md5();
34
Ray Milkey5b3717e2015-02-05 11:44:08 -080035 protected Key(long hash) {
Brian O'Connor055fb972015-02-05 11:40:51 -080036 this.hash = hash;
37 }
38
39 public long hash() {
40 return hash;
41 }
42
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080043 @Override
44 public int hashCode() {
45 return (int) (hash() ^ (hash() >>> 32));
46 }
47
48 @Override
49 public abstract boolean equals(Object obj);
50
51 /**
52 * Creates a key based on the provided string.
53 * <p>
54 * Note: Two keys with equal value, but different appId, are not equal.
55 * </p>
56 *
57 * @param key the provided string
58 * @param appId application id to associate with this key
59 * @return the key for the string
60 */
Brian O'Connor055fb972015-02-05 11:40:51 -080061 public static Key of(String key, ApplicationId appId) {
62 return new StringKey(key, appId);
63 }
64
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080065 /**
66 * Creates a key based on the provided long.
67 * <p>
68 * Note: Two keys with equal value, but different appId, are not equal.
69 * Also, "10" and 10L are different.
70 * </p>
71 *
72 * @param key the provided long
73 * @param appId application id to associate with this key
74 * @return the key for the long
75 */
Brian O'Connor055fb972015-02-05 11:40:51 -080076 public static Key of(long key, ApplicationId appId) {
77 return new LongKey(key, appId);
78 }
79
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080080 private static final class StringKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -080081
82 private final ApplicationId appId;
83 private final String key;
84
85 private StringKey(String key, ApplicationId appId) {
86 super(HASH_FN.newHasher()
87 .putShort(appId.id())
88 .putString(key, StandardCharsets.UTF_8)
89 .hash().asLong());
90 this.key = key;
91 this.appId = appId;
92 }
93
94 @Override
95 public String toString() {
96 return key;
97 }
98
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080099 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800100 @Override
101 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800102 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) {
108 return true;
109 }
110 if (obj == null || getClass() != obj.getClass()) {
111 return false;
112 }
113 final StringKey other = (StringKey) obj;
114 return this.hash() == other.hash() &&
115 Objects.equals(this.appId, other.appId) &&
116 Objects.equals(this.key, other.key);
117 }
118 }
119
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800120 private static final class LongKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -0800121
122 private final ApplicationId appId;
Ray Milkey5ad39662015-02-10 15:12:14 -0800123 private final long key;
Brian O'Connor055fb972015-02-05 11:40:51 -0800124
125 private LongKey(long key, ApplicationId appId) {
126 super(HASH_FN.newHasher()
127 .putShort(appId.id())
128 .putLong(key)
129 .hash().asLong());
130 this.key = key;
131 this.appId = appId;
132 }
133
134 @Override
135 public String toString() {
Ray Milkey3af13862015-02-10 15:23:13 -0800136 return "0x" + Long.toHexString(key);
Brian O'Connor055fb972015-02-05 11:40:51 -0800137 }
138
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800139 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800140 @Override
141 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800142 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800143 }
144
145 @Override
146 public boolean equals(Object obj) {
147 if (this == obj) {
148 return true;
149 }
150 if (obj == null || getClass() != obj.getClass()) {
151 return false;
152 }
153 final LongKey other = (LongKey) obj;
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800154 return this.hash() == other.hash() &&
155 this.key == other.key &&
156 Objects.equals(this.appId, other.appId);
Brian O'Connor055fb972015-02-05 11:40:51 -0800157 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800158 }
159}
160
161