Committed by
Gerrit Code Review
Refactor ResourcePath internal implementation
Change-Id: Idb3aa467b4d0e8181adf0d1766812a038b5408ac
Showing
7 changed files
with
54 additions
and
41 deletions
... | @@ -20,6 +20,7 @@ import com.google.common.base.MoreObjects; | ... | @@ -20,6 +20,7 @@ import com.google.common.base.MoreObjects; |
20 | import com.google.common.collect.ImmutableList; | 20 | import com.google.common.collect.ImmutableList; |
21 | 21 | ||
22 | import java.util.Arrays; | 22 | import java.util.Arrays; |
23 | +import java.util.LinkedList; | ||
23 | import java.util.List; | 24 | import java.util.List; |
24 | import java.util.Objects; | 25 | import java.util.Objects; |
25 | import java.util.Optional; | 26 | import java.util.Optional; |
... | @@ -39,18 +40,11 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -39,18 +40,11 @@ import static com.google.common.base.Preconditions.checkNotNull; |
39 | @Beta | 40 | @Beta |
40 | public final class ResourcePath { | 41 | public final class ResourcePath { |
41 | 42 | ||
42 | - private final List<Object> resources; | 43 | + private final ResourcePath parent; |
44 | + private final Object last; | ||
43 | 45 | ||
44 | public static final ResourcePath ROOT = new ResourcePath(ImmutableList.of()); | 46 | public static final ResourcePath ROOT = new ResourcePath(ImmutableList.of()); |
45 | 47 | ||
46 | - public static ResourcePath child(ResourcePath parent, Object child) { | ||
47 | - ImmutableList<Object> components = ImmutableList.builder() | ||
48 | - .addAll(parent.components()) | ||
49 | - .add(child) | ||
50 | - .build(); | ||
51 | - return new ResourcePath(components); | ||
52 | - } | ||
53 | - | ||
54 | /** | 48 | /** |
55 | * Creates an resource path from the specified components. | 49 | * Creates an resource path from the specified components. |
56 | * | 50 | * |
... | @@ -67,13 +61,32 @@ public final class ResourcePath { | ... | @@ -67,13 +61,32 @@ public final class ResourcePath { |
67 | */ | 61 | */ |
68 | public ResourcePath(List<Object> components) { | 62 | public ResourcePath(List<Object> components) { |
69 | checkNotNull(components); | 63 | checkNotNull(components); |
64 | + if (components.isEmpty()) { | ||
65 | + this.parent = null; | ||
66 | + this.last = null; | ||
67 | + return; | ||
68 | + } | ||
69 | + | ||
70 | + LinkedList<Object> children = new LinkedList<>(components); | ||
71 | + this.last = children.pollLast(); | ||
72 | + this.parent = new ResourcePath(children); | ||
73 | + } | ||
70 | 74 | ||
71 | - this.resources = ImmutableList.copyOf(components); | 75 | + /** |
76 | + * Creates an resource path from the specified parent and child. | ||
77 | + * | ||
78 | + * @param parent the parent of this resource | ||
79 | + * @param last a child of the parent | ||
80 | + */ | ||
81 | + public ResourcePath(ResourcePath parent, Object last) { | ||
82 | + this.parent = checkNotNull(parent); | ||
83 | + this.last = checkNotNull(last); | ||
72 | } | 84 | } |
73 | 85 | ||
74 | // for serialization | 86 | // for serialization |
75 | private ResourcePath() { | 87 | private ResourcePath() { |
76 | - this.resources = null; | 88 | + this.parent = null; |
89 | + this.last = null; | ||
77 | } | 90 | } |
78 | 91 | ||
79 | /** | 92 | /** |
... | @@ -82,7 +95,15 @@ public final class ResourcePath { | ... | @@ -82,7 +95,15 @@ public final class ResourcePath { |
82 | * @return the components of this resource path | 95 | * @return the components of this resource path |
83 | */ | 96 | */ |
84 | public List<Object> components() { | 97 | public List<Object> components() { |
85 | - return resources; | 98 | + LinkedList<Object> components = new LinkedList<>(); |
99 | + | ||
100 | + ResourcePath parentPath = parent; | ||
101 | + while (parentPath != null) { | ||
102 | + components.addFirst(last); | ||
103 | + parentPath = parent.parent; | ||
104 | + } | ||
105 | + | ||
106 | + return components; | ||
86 | } | 107 | } |
87 | 108 | ||
88 | /** | 109 | /** |
... | @@ -93,20 +114,11 @@ public final class ResourcePath { | ... | @@ -93,20 +114,11 @@ public final class ResourcePath { |
93 | * If there is no parent, empty instance will be returned. | 114 | * If there is no parent, empty instance will be returned. |
94 | */ | 115 | */ |
95 | public Optional<ResourcePath> parent() { | 116 | public Optional<ResourcePath> parent() { |
96 | - if (!isRoot()) { | 117 | + return Optional.ofNullable(parent); |
97 | - return Optional.of(new ResourcePath(resources.subList(0, resources.size() - 1))); | ||
98 | - } | ||
99 | - | ||
100 | - return Optional.empty(); | ||
101 | } | 118 | } |
102 | 119 | ||
103 | - /** | 120 | + public ResourcePath child(Object child) { |
104 | - * Returns true if the path represents root. | 121 | + return new ResourcePath(this, child); |
105 | - * | ||
106 | - * @return true if the path represents root, false otherwise. | ||
107 | - */ | ||
108 | - public boolean isRoot() { | ||
109 | - return resources.size() == 0; | ||
110 | } | 122 | } |
111 | 123 | ||
112 | /** | 124 | /** |
... | @@ -115,14 +127,13 @@ public final class ResourcePath { | ... | @@ -115,14 +127,13 @@ public final class ResourcePath { |
115 | * @return the last component of this instance. | 127 | * @return the last component of this instance. |
116 | * The return value is equal to the last object of {@code components()}. | 128 | * The return value is equal to the last object of {@code components()}. |
117 | */ | 129 | */ |
118 | - public Object lastComponent() { | 130 | + public Object last() { |
119 | - int last = resources.size() - 1; | 131 | + return last; |
120 | - return resources.get(last); | ||
121 | } | 132 | } |
122 | 133 | ||
123 | @Override | 134 | @Override |
124 | public int hashCode() { | 135 | public int hashCode() { |
125 | - return resources.hashCode(); | 136 | + return Objects.hash(this.parent, this.last); |
126 | } | 137 | } |
127 | 138 | ||
128 | @Override | 139 | @Override |
... | @@ -134,13 +145,15 @@ public final class ResourcePath { | ... | @@ -134,13 +145,15 @@ public final class ResourcePath { |
134 | return false; | 145 | return false; |
135 | } | 146 | } |
136 | final ResourcePath that = (ResourcePath) obj; | 147 | final ResourcePath that = (ResourcePath) obj; |
137 | - return Objects.equals(this.resources, that.resources); | 148 | + return Objects.equals(this.parent, that.parent) |
149 | + && Objects.equals(this.last, that.last); | ||
138 | } | 150 | } |
139 | 151 | ||
140 | @Override | 152 | @Override |
141 | public String toString() { | 153 | public String toString() { |
142 | return MoreObjects.toStringHelper(this) | 154 | return MoreObjects.toStringHelper(this) |
143 | - .add("resources", resources) | 155 | + .add("parent", parent) |
156 | + .add("last", last) | ||
144 | .toString(); | 157 | .toString(); |
145 | } | 158 | } |
146 | } | 159 | } | ... | ... |
... | @@ -76,7 +76,7 @@ public class ResourcePathTest { | ... | @@ -76,7 +76,7 @@ public class ResourcePathTest { |
76 | LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1); | 76 | LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1); |
77 | ResourcePath path = new ResourcePath(linkKey); | 77 | ResourcePath path = new ResourcePath(linkKey); |
78 | 78 | ||
79 | - LinkKey child = (LinkKey) path.lastComponent(); | 79 | + LinkKey child = (LinkKey) path.last(); |
80 | assertThat(child, is(linkKey)); | 80 | assertThat(child, is(linkKey)); |
81 | } | 81 | } |
82 | } | 82 | } | ... | ... |
... | @@ -146,8 +146,8 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { | ... | @@ -146,8 +146,8 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { |
146 | 146 | ||
147 | private Optional<MplsLabel> findMplsLabel(LinkKey link) { | 147 | private Optional<MplsLabel> findMplsLabel(LinkKey link) { |
148 | return resourceService.getAvailableResources(new ResourcePath(link)).stream() | 148 | return resourceService.getAvailableResources(new ResourcePath(link)).stream() |
149 | - .filter(x -> x.lastComponent() instanceof MplsLabel) | 149 | + .filter(x -> x.last() instanceof MplsLabel) |
150 | - .map(x -> (MplsLabel) x.lastComponent()) | 150 | + .map(x -> (MplsLabel) x.last()) |
151 | .findFirst(); | 151 | .findFirst(); |
152 | } | 152 | } |
153 | 153 | ... | ... |
... | @@ -183,7 +183,7 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical | ... | @@ -183,7 +183,7 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical |
183 | IndexedLambda minLambda = findFirstLambda(lambdas); | 183 | IndexedLambda minLambda = findFirstLambda(lambdas); |
184 | List<ResourcePath> lambdaResources = path.links().stream() | 184 | List<ResourcePath> lambdaResources = path.links().stream() |
185 | .map(x -> new ResourcePath(linkKey(x.src(), x.dst()))) | 185 | .map(x -> new ResourcePath(linkKey(x.src(), x.dst()))) |
186 | - .map(x -> ResourcePath.child(x, minLambda)) | 186 | + .map(x -> x.child(minLambda)) |
187 | .collect(Collectors.toList()); | 187 | .collect(Collectors.toList()); |
188 | 188 | ||
189 | List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), lambdaResources); | 189 | List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), lambdaResources); |
... | @@ -198,8 +198,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical | ... | @@ -198,8 +198,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical |
198 | return links.stream() | 198 | return links.stream() |
199 | .map(x -> new ResourcePath(linkKey(x.src(), x.dst()))) | 199 | .map(x -> new ResourcePath(linkKey(x.src(), x.dst()))) |
200 | .map(resourceService::getAvailableResources) | 200 | .map(resourceService::getAvailableResources) |
201 | - .map(x -> Iterables.filter(x, r -> r.lastComponent() instanceof IndexedLambda)) | 201 | + .map(x -> Iterables.filter(x, r -> r.last() instanceof IndexedLambda)) |
202 | - .map(x -> Iterables.transform(x, r -> (IndexedLambda) r.lastComponent())) | 202 | + .map(x -> Iterables.transform(x, r -> (IndexedLambda) r.last())) |
203 | .map(x -> (Set<IndexedLambda>) ImmutableSet.copyOf(x)) | 203 | .map(x -> (Set<IndexedLambda>) ImmutableSet.copyOf(x)) |
204 | .reduce(Sets::intersection) | 204 | .reduce(Sets::intersection) |
205 | .orElse(Collections.emptySet()); | 205 | .orElse(Collections.emptySet()); | ... | ... |
... | @@ -169,7 +169,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent | ... | @@ -169,7 +169,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent |
169 | checkNotNull(children); | 169 | checkNotNull(children); |
170 | checkArgument(!children.isEmpty()); | 170 | checkArgument(!children.isEmpty()); |
171 | 171 | ||
172 | - List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); | 172 | + List<ResourcePath> resources = Lists.transform(children, parent::child); |
173 | return store.register(resources); | 173 | return store.register(resources); |
174 | } | 174 | } |
175 | 175 | ||
... | @@ -179,7 +179,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent | ... | @@ -179,7 +179,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent |
179 | checkNotNull(children); | 179 | checkNotNull(children); |
180 | checkArgument(!children.isEmpty()); | 180 | checkArgument(!children.isEmpty()); |
181 | 181 | ||
182 | - List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x)); | 182 | + List<ResourcePath> resources = Lists.transform(children, parent::child); |
183 | return store.unregister(resources); | 183 | return store.unregister(resources); |
184 | } | 184 | } |
185 | 185 | ... | ... |
... | @@ -90,7 +90,7 @@ class MockResourceService implements ResourceService { | ... | @@ -90,7 +90,7 @@ class MockResourceService implements ResourceService { |
90 | 90 | ||
91 | @Override | 91 | @Override |
92 | public Collection<ResourcePath> getAvailableResources(ResourcePath parent) { | 92 | public Collection<ResourcePath> getAvailableResources(ResourcePath parent) { |
93 | - ResourcePath resource = ResourcePath.child(parent, MplsLabel.mplsLabel(10)); | 93 | + ResourcePath resource = parent.child(MplsLabel.mplsLabel(10)); |
94 | return ImmutableList.of(resource); | 94 | return ImmutableList.of(resource); |
95 | } | 95 | } |
96 | 96 | ... | ... |
... | @@ -264,7 +264,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -264,7 +264,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
264 | } | 264 | } |
265 | 265 | ||
266 | return children.value().stream() | 266 | return children.value().stream() |
267 | - .filter(x -> x.lastComponent().getClass().equals(cls)) | 267 | + .filter(x -> x.last().getClass().equals(cls)) |
268 | .filter(consumerMap::containsKey) | 268 | .filter(consumerMap::containsKey) |
269 | .collect(Collectors.toList()); | 269 | .collect(Collectors.toList()); |
270 | } | 270 | } |
... | @@ -344,7 +344,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -344,7 +344,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
344 | */ | 344 | */ |
345 | private boolean isRegistered(TransactionalMap<ResourcePath, List<ResourcePath>> map, ResourcePath resource) { | 345 | private boolean isRegistered(TransactionalMap<ResourcePath, List<ResourcePath>> map, ResourcePath resource) { |
346 | // root is always regarded to be registered | 346 | // root is always regarded to be registered |
347 | - if (resource.isRoot()) { | 347 | + if (!resource.parent().isPresent()) { |
348 | return true; | 348 | return true; |
349 | } | 349 | } |
350 | 350 | ... | ... |
-
Please register or login to post a comment