blob: 7007f75e27bbbbb0f72dc6b70bd9489e1355e0e5 [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
29// TODO need to make this classes kryo serializable
30public class Key {
31
32 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
43 public static Key of(String key, ApplicationId appId) {
44 return new StringKey(key, appId);
45 }
46
47 public static Key of(long key, ApplicationId appId) {
48 return new LongKey(key, appId);
49 }
50
Ray Milkey5b3717e2015-02-05 11:44:08 -080051 private static final class StringKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -080052
53 private final ApplicationId appId;
54 private final String key;
55
56 private StringKey(String key, ApplicationId appId) {
57 super(HASH_FN.newHasher()
58 .putShort(appId.id())
59 .putString(key, StandardCharsets.UTF_8)
60 .hash().asLong());
61 this.key = key;
62 this.appId = appId;
63 }
64
65 @Override
66 public String toString() {
67 return key;
68 }
69
70 @Override
71 public int hashCode() {
72 return Objects.hash(key);
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (this == obj) {
78 return true;
79 }
80 if (obj == null || getClass() != obj.getClass()) {
81 return false;
82 }
83 final StringKey other = (StringKey) obj;
84 return this.hash() == other.hash() &&
85 Objects.equals(this.appId, other.appId) &&
86 Objects.equals(this.key, other.key);
87 }
88 }
89
Ray Milkey5b3717e2015-02-05 11:44:08 -080090 private static final class LongKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -080091
92 private final ApplicationId appId;
Ray Milkey5ad39662015-02-10 15:12:14 -080093 private final long key;
Brian O'Connor055fb972015-02-05 11:40:51 -080094
95 private LongKey(long key, ApplicationId appId) {
96 super(HASH_FN.newHasher()
97 .putShort(appId.id())
98 .putLong(key)
99 .hash().asLong());
100 this.key = key;
101 this.appId = appId;
102 }
103
104 @Override
105 public String toString() {
106 return Long.toString(key);
107 }
108
109 @Override
110 public int hashCode() {
111 return Objects.hash(key);
112 }
113
114 @Override
115 public boolean equals(Object obj) {
116 if (this == obj) {
117 return true;
118 }
119 if (obj == null || getClass() != obj.getClass()) {
120 return false;
121 }
122 final LongKey other = (LongKey) obj;
123 return Objects.equals(this.appId, other.appId) &&
124 this.key == other.key;
125 }
126
127 }
128}
129
130