Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 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 | package org.onosproject.net.intent; |
| 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 19 | import com.google.common.hash.HashFunction; |
| 20 | import com.google.common.hash.Hashing; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | |
| 23 | import java.nio.charset.StandardCharsets; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | /** |
| 27 | * Key class for Intents. |
| 28 | */ |
| 29 | // TODO maybe pull this up to utils |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 30 | @Beta |
Simon Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 31 | public abstract class Key implements Comparable<Key> { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 32 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 33 | //TODO consider making this a HashCode object (worry about performance) |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 34 | private final long hash; |
| 35 | private static final HashFunction HASH_FN = Hashing.md5(); |
| 36 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 37 | protected Key(long hash) { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 38 | this.hash = hash; |
| 39 | } |
| 40 | |
| 41 | public long hash() { |
| 42 | return hash; |
| 43 | } |
| 44 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 45 | @Override |
| 46 | public int hashCode() { |
Sho SHIMIZU | 8711451 | 2015-05-08 14:31:00 -0700 | [diff] [blame] | 47 | return Long.hashCode(hash); |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 48 | } |
| 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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 63 | public static Key of(String key, ApplicationId appId) { |
| 64 | return new StringKey(key, appId); |
| 65 | } |
| 66 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 67 | /** |
| 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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 78 | public static Key of(long key, ApplicationId appId) { |
| 79 | return new LongKey(key, appId); |
| 80 | } |
| 81 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 82 | private static final class StringKey extends Key { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 83 | |
| 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'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 101 | // checkstyle requires this |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 102 | @Override |
| 103 | public int hashCode() { |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 104 | return super.hashCode(); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 105 | } |
| 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 Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 120 | |
| 121 | @Override |
| 122 | public int compareTo(Key o) { |
| 123 | StringKey sk = (StringKey) o; |
| 124 | return this.key.compareTo(sk.key); |
| 125 | } |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 128 | private static final class LongKey extends Key { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 129 | |
| 130 | private final ApplicationId appId; |
Ray Milkey | 5ad3966 | 2015-02-10 15:12:14 -0800 | [diff] [blame] | 131 | private final long key; |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 132 | |
| 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 Milkey | 3af1386 | 2015-02-10 15:23:13 -0800 | [diff] [blame] | 144 | return "0x" + Long.toHexString(key); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 147 | // checkstyle requires this |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 148 | @Override |
| 149 | public int hashCode() { |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 150 | return super.hashCode(); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 151 | } |
| 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'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 162 | return this.hash() == other.hash() && |
| 163 | this.key == other.key && |
| 164 | Objects.equals(this.appId, other.appId); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 165 | } |
Simon Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 166 | |
| 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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | |