Adding Key class for intents
Change-Id: Ib6e586bcc938e062de0b1d6c92e3b885334a4769
Showing
1 changed file
with
130 additions
and
0 deletions
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.net.intent; | ||
| 17 | + | ||
| 18 | +import com.google.common.hash.HashFunction; | ||
| 19 | +import com.google.common.hash.Hashing; | ||
| 20 | +import org.onosproject.core.ApplicationId; | ||
| 21 | + | ||
| 22 | +import java.nio.charset.StandardCharsets; | ||
| 23 | +import 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 | ||
| 30 | +public class Key { | ||
| 31 | + | ||
| 32 | + private final long hash; | ||
| 33 | + private static final HashFunction HASH_FN = Hashing.md5(); | ||
| 34 | + | ||
| 35 | + private Key(long hash) { | ||
| 36 | + 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 | + | ||
| 51 | + private final static class StringKey extends Key { | ||
| 52 | + | ||
| 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 | + | ||
| 90 | + private final static class LongKey extends Key { | ||
| 91 | + | ||
| 92 | + private final ApplicationId appId; | ||
| 93 | + private static long key; | ||
| 94 | + | ||
| 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 | + |
-
Please register or login to post a comment