Committed by
Gerrit Code Review
Rename ResourcePath.Key to ResourceId for naming consistency
Change-Id: Idb34624f897b0fd5745f26758a007c171f4cd774
Showing
5 changed files
with
141 additions
and
114 deletions
1 | +/* | ||
2 | + * Copyright 2016 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.newresource; | ||
17 | + | ||
18 | +import com.google.common.annotations.Beta; | ||
19 | +import com.google.common.base.MoreObjects; | ||
20 | +import com.google.common.collect.ImmutableList; | ||
21 | +import org.onosproject.net.DeviceId; | ||
22 | +import org.onosproject.net.PortNumber; | ||
23 | + | ||
24 | +import java.util.Objects; | ||
25 | + | ||
26 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
27 | + | ||
28 | +/** | ||
29 | + * Represents identifier of resource. | ||
30 | + * This class is exposed to public, but intended to use only in ResourceStore implementations. | ||
31 | + */ | ||
32 | +@Beta | ||
33 | +public final class ResourceId { | ||
34 | + static final ResourceId ROOT = new ResourceId(); | ||
35 | + | ||
36 | + final ImmutableList<Object> components; | ||
37 | + | ||
38 | + static ResourceId of(DeviceId device, Object... components) { | ||
39 | + return new ResourceId(ImmutableList.builder() | ||
40 | + .add(device) | ||
41 | + .add(components) | ||
42 | + .build()); | ||
43 | + } | ||
44 | + | ||
45 | + static ResourceId of(DeviceId device, PortNumber port, Object... components) { | ||
46 | + return new ResourceId(ImmutableList.builder() | ||
47 | + .add(device) | ||
48 | + .add(port) | ||
49 | + .add(components) | ||
50 | + .build()); | ||
51 | + } | ||
52 | + | ||
53 | + private ResourceId(ImmutableList<Object> components) { | ||
54 | + this.components = checkNotNull(components); | ||
55 | + } | ||
56 | + | ||
57 | + // for serializer | ||
58 | + private ResourceId() { | ||
59 | + this.components = ImmutableList.of(); | ||
60 | + } | ||
61 | + | ||
62 | + // IndexOutOfBoundsException is raised when the instance is equal to ROOT | ||
63 | + ResourceId parent() { | ||
64 | + if (components.size() == 1) { | ||
65 | + return ROOT; | ||
66 | + } else { | ||
67 | + return new ResourceId(components.subList(0, components.size() - 1)); | ||
68 | + } | ||
69 | + } | ||
70 | + | ||
71 | + ResourceId child(Object child) { | ||
72 | + return new ResourceId(ImmutableList.builder() | ||
73 | + .add(components) | ||
74 | + .add(child) | ||
75 | + .build()); | ||
76 | + } | ||
77 | + | ||
78 | + @Override | ||
79 | + public int hashCode() { | ||
80 | + return components.hashCode(); | ||
81 | + } | ||
82 | + | ||
83 | + @Override | ||
84 | + public boolean equals(Object obj) { | ||
85 | + if (this == obj) { | ||
86 | + return true; | ||
87 | + } | ||
88 | + if (!(obj instanceof ResourceId)) { | ||
89 | + return false; | ||
90 | + } | ||
91 | + | ||
92 | + ResourceId other = (ResourceId) obj; | ||
93 | + return Objects.equals(this.components, other.components); | ||
94 | + } | ||
95 | + | ||
96 | + @Override | ||
97 | + public String toString() { | ||
98 | + return MoreObjects.toStringHelper(this) | ||
99 | + .add("components", components) | ||
100 | + .toString(); | ||
101 | + } | ||
102 | +} |
... | @@ -17,7 +17,6 @@ package org.onosproject.net.newresource; | ... | @@ -17,7 +17,6 @@ package org.onosproject.net.newresource; |
17 | 17 | ||
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | import com.google.common.base.MoreObjects; | 19 | import com.google.common.base.MoreObjects; |
20 | -import com.google.common.collect.ImmutableList; | ||
21 | import org.onosproject.net.DeviceId; | 20 | import org.onosproject.net.DeviceId; |
22 | import org.onosproject.net.PortNumber; | 21 | import org.onosproject.net.PortNumber; |
23 | 22 | ||
... | @@ -49,12 +48,12 @@ import static com.google.common.base.Preconditions.checkState; | ... | @@ -49,12 +48,12 @@ import static com.google.common.base.Preconditions.checkState; |
49 | public abstract class ResourcePath { | 48 | public abstract class ResourcePath { |
50 | 49 | ||
51 | private final Discrete parent; | 50 | private final Discrete parent; |
52 | - private final Key key; | 51 | + private final ResourceId id; |
53 | 52 | ||
54 | public static final Discrete ROOT = new Discrete(); | 53 | public static final Discrete ROOT = new Discrete(); |
55 | 54 | ||
56 | public static ResourcePath discrete(DeviceId device) { | 55 | public static ResourcePath discrete(DeviceId device) { |
57 | - return new Discrete(Key.of(device)); | 56 | + return new Discrete(ResourceId.of(device)); |
58 | } | 57 | } |
59 | 58 | ||
60 | /** | 59 | /** |
... | @@ -65,7 +64,7 @@ public abstract class ResourcePath { | ... | @@ -65,7 +64,7 @@ public abstract class ResourcePath { |
65 | * @return resource path instance | 64 | * @return resource path instance |
66 | */ | 65 | */ |
67 | public static ResourcePath discrete(DeviceId device, Object... components) { | 66 | public static ResourcePath discrete(DeviceId device, Object... components) { |
68 | - return new Discrete(Key.of(device, components)); | 67 | + return new Discrete(ResourceId.of(device, components)); |
69 | } | 68 | } |
70 | 69 | ||
71 | /** | 70 | /** |
... | @@ -77,7 +76,7 @@ public abstract class ResourcePath { | ... | @@ -77,7 +76,7 @@ public abstract class ResourcePath { |
77 | * @return resource path instance | 76 | * @return resource path instance |
78 | */ | 77 | */ |
79 | public static ResourcePath discrete(DeviceId device, PortNumber port, Object... components) { | 78 | public static ResourcePath discrete(DeviceId device, PortNumber port, Object... components) { |
80 | - return new Discrete(Key.of(device, port, components)); | 79 | + return new Discrete(ResourceId.of(device, port, components)); |
81 | } | 80 | } |
82 | 81 | ||
83 | /** | 82 | /** |
... | @@ -92,7 +91,7 @@ public abstract class ResourcePath { | ... | @@ -92,7 +91,7 @@ public abstract class ResourcePath { |
92 | checkArgument(components.length > 0, | 91 | checkArgument(components.length > 0, |
93 | "Length of components must be greater thant 0, but " + components.length); | 92 | "Length of components must be greater thant 0, but " + components.length); |
94 | 93 | ||
95 | - return new Continuous(Key.of(device, components), value); | 94 | + return new Continuous(ResourceId.of(device, components), value); |
96 | } | 95 | } |
97 | 96 | ||
98 | /** | 97 | /** |
... | @@ -105,29 +104,29 @@ public abstract class ResourcePath { | ... | @@ -105,29 +104,29 @@ public abstract class ResourcePath { |
105 | * @return resource path instance | 104 | * @return resource path instance |
106 | */ | 105 | */ |
107 | public static ResourcePath continuous(double value, DeviceId device, PortNumber port, Object... components) { | 106 | public static ResourcePath continuous(double value, DeviceId device, PortNumber port, Object... components) { |
108 | - return new Continuous(Key.of(device, port, components), value); | 107 | + return new Continuous(ResourceId.of(device, port, components), value); |
109 | } | 108 | } |
110 | 109 | ||
111 | /** | 110 | /** |
112 | * Creates an resource path from the specified key. | 111 | * Creates an resource path from the specified key. |
113 | * | 112 | * |
114 | - * @param key key of the path | 113 | + * @param id key of the path |
115 | */ | 114 | */ |
116 | - protected ResourcePath(Key key) { | 115 | + protected ResourcePath(ResourceId id) { |
117 | - checkNotNull(key); | 116 | + checkNotNull(id); |
118 | 117 | ||
119 | - this.key = key; | 118 | + this.id = id; |
120 | - if (key.components.size() == 1) { | 119 | + if (id.components.size() == 1) { |
121 | this.parent = ROOT; | 120 | this.parent = ROOT; |
122 | } else { | 121 | } else { |
123 | - this.parent = new Discrete(key.parent()); | 122 | + this.parent = new Discrete(id.parent()); |
124 | } | 123 | } |
125 | } | 124 | } |
126 | 125 | ||
127 | // for serialization | 126 | // for serialization |
128 | private ResourcePath() { | 127 | private ResourcePath() { |
129 | this.parent = null; | 128 | this.parent = null; |
130 | - this.key = Key.ROOT; | 129 | + this.id = ResourceId.ROOT; |
131 | } | 130 | } |
132 | 131 | ||
133 | /** | 132 | /** |
... | @@ -136,7 +135,7 @@ public abstract class ResourcePath { | ... | @@ -136,7 +135,7 @@ public abstract class ResourcePath { |
136 | * @return the components of this resource path | 135 | * @return the components of this resource path |
137 | */ | 136 | */ |
138 | public List<Object> components() { | 137 | public List<Object> components() { |
139 | - return key.components; | 138 | + return id.components; |
140 | } | 139 | } |
141 | 140 | ||
142 | /** | 141 | /** |
... | @@ -160,7 +159,7 @@ public abstract class ResourcePath { | ... | @@ -160,7 +159,7 @@ public abstract class ResourcePath { |
160 | public ResourcePath child(Object child) { | 159 | public ResourcePath child(Object child) { |
161 | checkState(this instanceof Discrete); | 160 | checkState(this instanceof Discrete); |
162 | 161 | ||
163 | - return new Discrete(key().child(child)); | 162 | + return new Discrete(id().child(child)); |
164 | } | 163 | } |
165 | 164 | ||
166 | /** | 165 | /** |
... | @@ -174,7 +173,7 @@ public abstract class ResourcePath { | ... | @@ -174,7 +173,7 @@ public abstract class ResourcePath { |
174 | public ResourcePath child(Object child, double value) { | 173 | public ResourcePath child(Object child, double value) { |
175 | checkState(this instanceof Discrete); | 174 | checkState(this instanceof Discrete); |
176 | 175 | ||
177 | - return new Continuous(key.child(child), value); | 176 | + return new Continuous(id.child(child), value); |
178 | } | 177 | } |
179 | 178 | ||
180 | /** | 179 | /** |
... | @@ -184,10 +183,10 @@ public abstract class ResourcePath { | ... | @@ -184,10 +183,10 @@ public abstract class ResourcePath { |
184 | * The return value is equal to the last object of {@code components()}. | 183 | * The return value is equal to the last object of {@code components()}. |
185 | */ | 184 | */ |
186 | public Object last() { | 185 | public Object last() { |
187 | - if (key.components.isEmpty()) { | 186 | + if (id.components.isEmpty()) { |
188 | return null; | 187 | return null; |
189 | } | 188 | } |
190 | - return key.components.get(key.components.size() - 1); | 189 | + return id.components.get(id.components.size() - 1); |
191 | } | 190 | } |
192 | 191 | ||
193 | /** | 192 | /** |
... | @@ -195,13 +194,13 @@ public abstract class ResourcePath { | ... | @@ -195,13 +194,13 @@ public abstract class ResourcePath { |
195 | * | 194 | * |
196 | * @return the key of this resource path | 195 | * @return the key of this resource path |
197 | */ | 196 | */ |
198 | - public Key key() { | 197 | + public ResourceId id() { |
199 | - return key; | 198 | + return id; |
200 | } | 199 | } |
201 | 200 | ||
202 | @Override | 201 | @Override |
203 | public int hashCode() { | 202 | public int hashCode() { |
204 | - return key.hashCode(); | 203 | + return id.hashCode(); |
205 | } | 204 | } |
206 | 205 | ||
207 | @Override | 206 | @Override |
... | @@ -213,13 +212,13 @@ public abstract class ResourcePath { | ... | @@ -213,13 +212,13 @@ public abstract class ResourcePath { |
213 | return false; | 212 | return false; |
214 | } | 213 | } |
215 | final ResourcePath that = (ResourcePath) obj; | 214 | final ResourcePath that = (ResourcePath) obj; |
216 | - return Objects.equals(this.key, that.key); | 215 | + return Objects.equals(this.id, that.id); |
217 | } | 216 | } |
218 | 217 | ||
219 | @Override | 218 | @Override |
220 | public String toString() { | 219 | public String toString() { |
221 | return MoreObjects.toStringHelper(this) | 220 | return MoreObjects.toStringHelper(this) |
222 | - .add("key", key) | 221 | + .add("key", id) |
223 | .toString(); | 222 | .toString(); |
224 | } | 223 | } |
225 | 224 | ||
... | @@ -237,7 +236,7 @@ public abstract class ResourcePath { | ... | @@ -237,7 +236,7 @@ public abstract class ResourcePath { |
237 | super(); | 236 | super(); |
238 | } | 237 | } |
239 | 238 | ||
240 | - private Discrete(Key key) { | 239 | + private Discrete(ResourceId key) { |
241 | super(key); | 240 | super(key); |
242 | } | 241 | } |
243 | } | 242 | } |
... | @@ -253,14 +252,14 @@ public abstract class ResourcePath { | ... | @@ -253,14 +252,14 @@ public abstract class ResourcePath { |
253 | public static final class Continuous extends ResourcePath { | 252 | public static final class Continuous extends ResourcePath { |
254 | private final double value; | 253 | private final double value; |
255 | 254 | ||
256 | - private Continuous(Key key, double value) { | 255 | + private Continuous(ResourceId key, double value) { |
257 | super(key); | 256 | super(key); |
258 | this.value = value; | 257 | this.value = value; |
259 | } | 258 | } |
260 | 259 | ||
261 | @Override | 260 | @Override |
262 | public int hashCode() { | 261 | public int hashCode() { |
263 | - return Objects.hash(this.key(), this.value); | 262 | + return Objects.hash(this.id(), this.value); |
264 | } | 263 | } |
265 | 264 | ||
266 | @Override | 265 | @Override |
... | @@ -278,7 +277,7 @@ public abstract class ResourcePath { | ... | @@ -278,7 +277,7 @@ public abstract class ResourcePath { |
278 | } | 277 | } |
279 | 278 | ||
280 | final Continuous other = (Continuous) obj; | 279 | final Continuous other = (Continuous) obj; |
281 | - return Objects.equals(this.key(), other.key()); | 280 | + return Objects.equals(this.id(), other.id()); |
282 | } | 281 | } |
283 | 282 | ||
284 | /** | 283 | /** |
... | @@ -291,79 +290,4 @@ public abstract class ResourcePath { | ... | @@ -291,79 +290,4 @@ public abstract class ResourcePath { |
291 | } | 290 | } |
292 | } | 291 | } |
293 | 292 | ||
294 | - /** | ||
295 | - * Represents key of resource path used as a key in ResourceStore. | ||
296 | - * This class is exposed to public, but intended to use only in ResourceStore implementations. | ||
297 | - */ | ||
298 | - @Beta | ||
299 | - public static final class Key { | ||
300 | - private static final Key ROOT = new Key(); | ||
301 | - | ||
302 | - private final ImmutableList<Object> components; | ||
303 | - | ||
304 | - private static Key of(DeviceId device, Object... components) { | ||
305 | - return new Key(ImmutableList.builder() | ||
306 | - .add(device) | ||
307 | - .add(components) | ||
308 | - .build()); | ||
309 | - } | ||
310 | - | ||
311 | - private static Key of(DeviceId device, PortNumber port, Object... components) { | ||
312 | - return new Key(ImmutableList.builder() | ||
313 | - .add(device) | ||
314 | - .add(port) | ||
315 | - .add(components) | ||
316 | - .build()); | ||
317 | - } | ||
318 | - | ||
319 | - private Key(ImmutableList<Object> components) { | ||
320 | - this.components = checkNotNull(components); | ||
321 | - } | ||
322 | - | ||
323 | - // for serializer | ||
324 | - private Key() { | ||
325 | - this.components = ImmutableList.of(); | ||
326 | - } | ||
327 | - | ||
328 | - // IndexOutOfBoundsException is raised when the instance is equal to ROOT | ||
329 | - private Key parent() { | ||
330 | - if (components.size() == 1) { | ||
331 | - return ROOT; | ||
332 | - } else { | ||
333 | - return new Key(components.subList(0, components.size() - 1)); | ||
334 | - } | ||
335 | - } | ||
336 | - | ||
337 | - private Key child(Object child) { | ||
338 | - return new Key(ImmutableList.builder() | ||
339 | - .add(components) | ||
340 | - .add(child) | ||
341 | - .build()); | ||
342 | - } | ||
343 | - | ||
344 | - @Override | ||
345 | - public int hashCode() { | ||
346 | - return components.hashCode(); | ||
347 | - } | ||
348 | - | ||
349 | - @Override | ||
350 | - public boolean equals(Object obj) { | ||
351 | - if (this == obj) { | ||
352 | - return true; | ||
353 | - } | ||
354 | - if (!(obj instanceof Key)) { | ||
355 | - return false; | ||
356 | - } | ||
357 | - | ||
358 | - Key other = (Key) obj; | ||
359 | - return Objects.equals(this.components, other.components); | ||
360 | - } | ||
361 | - | ||
362 | - @Override | ||
363 | - public String toString() { | ||
364 | - return MoreObjects.toStringHelper(this) | ||
365 | - .add("components", components) | ||
366 | - .toString(); | ||
367 | - } | ||
368 | - } | ||
369 | } | 293 | } | ... | ... |
... | @@ -60,18 +60,18 @@ public class ResourcePathTest { | ... | @@ -60,18 +60,18 @@ public class ResourcePathTest { |
60 | } | 60 | } |
61 | 61 | ||
62 | @Test | 62 | @Test |
63 | - public void testKeyEquality() { | 63 | + public void testIdEquality() { |
64 | - ResourcePath.Key key1 = ResourcePath.discrete(D1, P1, VLAN1).key(); | 64 | + ResourceId id1 = ResourcePath.discrete(D1, P1, VLAN1).id(); |
65 | - ResourcePath.Key sameAsKey1 = ResourcePath.discrete(D1, P1, VLAN1).key(); | 65 | + ResourceId sameAsId1 = ResourcePath.discrete(D1, P1, VLAN1).id(); |
66 | - ResourcePath.Key key2 = ResourcePath.discrete(D2, P1, VLAN1).key(); | 66 | + ResourceId id2 = ResourcePath.discrete(D2, P1, VLAN1).id(); |
67 | - ResourcePath.Key key3 = ResourcePath.continuous(BW1.bps(), D1, P1, BW1).key(); | 67 | + ResourceId id3 = ResourcePath.continuous(BW1.bps(), D1, P1, BW1).id(); |
68 | // intentionally set a different value | 68 | // intentionally set a different value |
69 | - ResourcePath.Key sameAsKey3 = ResourcePath.continuous(BW2.bps(), D1, P1, BW1).key(); | 69 | + ResourceId sameAsId3 = ResourcePath.continuous(BW2.bps(), D1, P1, BW1).id(); |
70 | 70 | ||
71 | new EqualsTester() | 71 | new EqualsTester() |
72 | - .addEqualityGroup(key1, sameAsKey1) | 72 | + .addEqualityGroup(id1, sameAsId1) |
73 | - .addEqualityGroup(key2) | 73 | + .addEqualityGroup(id2) |
74 | - .addEqualityGroup(key3, sameAsKey3); | 74 | + .addEqualityGroup(id3, sameAsId3); |
75 | } | 75 | } |
76 | 76 | ||
77 | @Test | 77 | @Test | ... | ... |
... | @@ -172,6 +172,7 @@ import org.onosproject.net.intent.constraint.WaypointConstraint; | ... | @@ -172,6 +172,7 @@ import org.onosproject.net.intent.constraint.WaypointConstraint; |
172 | import org.onosproject.net.link.DefaultLinkDescription; | 172 | import org.onosproject.net.link.DefaultLinkDescription; |
173 | import org.onosproject.net.meter.MeterId; | 173 | import org.onosproject.net.meter.MeterId; |
174 | import org.onosproject.net.newresource.ResourceAllocation; | 174 | import org.onosproject.net.newresource.ResourceAllocation; |
175 | +import org.onosproject.net.newresource.ResourceId; | ||
175 | import org.onosproject.net.newresource.ResourcePath; | 176 | import org.onosproject.net.newresource.ResourcePath; |
176 | import org.onosproject.net.packet.DefaultOutboundPacket; | 177 | import org.onosproject.net.packet.DefaultOutboundPacket; |
177 | import org.onosproject.net.packet.DefaultPacketRequest; | 178 | import org.onosproject.net.packet.DefaultPacketRequest; |
... | @@ -430,7 +431,7 @@ public final class KryoNamespaces { | ... | @@ -430,7 +431,7 @@ public final class KryoNamespaces { |
430 | ResourcePath.class, | 431 | ResourcePath.class, |
431 | ResourcePath.Discrete.class, | 432 | ResourcePath.Discrete.class, |
432 | ResourcePath.Continuous.class, | 433 | ResourcePath.Continuous.class, |
433 | - ResourcePath.Key.class, | 434 | + ResourceId.class, |
434 | ResourceAllocation.class, | 435 | ResourceAllocation.class, |
435 | // Constraints | 436 | // Constraints |
436 | LambdaConstraint.class, | 437 | LambdaConstraint.class, | ... | ... |
... | @@ -379,7 +379,7 @@ public class KryoSerializerTest { | ... | @@ -379,7 +379,7 @@ public class KryoSerializerTest { |
379 | 379 | ||
380 | @Test | 380 | @Test |
381 | public void testResourceKey() { | 381 | public void testResourceKey() { |
382 | - testSerializedEquals(ResourcePath.discrete(DID1, P1).key()); | 382 | + testSerializedEquals(ResourcePath.discrete(DID1, P1).id()); |
383 | } | 383 | } |
384 | 384 | ||
385 | @Test | 385 | @Test | ... | ... |
-
Please register or login to post a comment