Committed by
Gerrit Code Review
Define valueAs() to get the enclosed value and remove volume()
volume() is replaced by valueAs() Change-Id: I3dbcbd6a0b8fcd28f0064272fe1fa6d7259e0a87
Showing
9 changed files
with
67 additions
and
57 deletions
... | @@ -115,7 +115,7 @@ public class AllocationsCommand extends AbstractShellCommand { | ... | @@ -115,7 +115,7 @@ public class AllocationsCommand extends AbstractShellCommand { |
115 | 115 | ||
116 | for (ResourceAllocation a : allocations) { | 116 | for (ResourceAllocation a : allocations) { |
117 | print("%s%s allocated by %s", Strings.repeat(" ", level + 1), | 117 | print("%s%s allocated by %s", Strings.repeat(" ", level + 1), |
118 | - a.resource().last(), asVerboseString(a.consumer())); | 118 | + a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumer())); |
119 | } | 119 | } |
120 | } | 120 | } |
121 | } | 121 | } | ... | ... |
... | @@ -128,7 +128,7 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -128,7 +128,7 @@ public class ResourcesCommand extends AbstractShellCommand { |
128 | } else { | 128 | } else { |
129 | String resourceName = resource.last().getClass().getSimpleName(); | 129 | String resourceName = resource.last().getClass().getSimpleName(); |
130 | 130 | ||
131 | - String toString = String.valueOf(resource.last()); | 131 | + String toString = String.valueOf(resource.valueAs(Object.class).orElse("")); |
132 | if (toString.startsWith(resourceName)) { | 132 | if (toString.startsWith(resourceName)) { |
133 | print("%s%s", Strings.repeat(" ", level), | 133 | print("%s%s", Strings.repeat(" ", level), |
134 | toString); | 134 | toString); |
... | @@ -178,14 +178,13 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -178,14 +178,13 @@ public class ResourcesCommand extends AbstractShellCommand { |
178 | 178 | ||
179 | // aggregate into RangeSet | 179 | // aggregate into RangeSet |
180 | e.getValue().stream() | 180 | e.getValue().stream() |
181 | - .map(Resource::last) | ||
182 | .map(res -> { | 181 | .map(res -> { |
183 | - if (res instanceof VlanId) { | 182 | + if (res.isTypeOf(VlanId.class)) { |
184 | - return (long) ((VlanId) res).toShort(); | 183 | + return (long) res.valueAs(VlanId.class).get().toShort(); |
185 | - } else if (res instanceof MplsLabel) { | 184 | + } else if (res.isTypeOf(MplsLabel.class)) { |
186 | - return (long) ((MplsLabel) res).toInt(); | 185 | + return (long) res.valueAs(MplsLabel.class).get().toInt(); |
187 | - } else if (res instanceof TributarySlot) { | 186 | + } else if (res.isTypeOf(TributarySlot.class)) { |
188 | - return ((TributarySlot) res).index(); | 187 | + return res.valueAs(TributarySlot.class).get().index(); |
189 | } | 188 | } |
190 | // TODO support Lambda (OchSignal types) | 189 | // TODO support Lambda (OchSignal types) |
191 | return 0L; | 190 | return 0L; | ... | ... |
... | @@ -57,24 +57,10 @@ public final class ContinuousResource implements Resource { | ... | @@ -57,24 +57,10 @@ public final class ContinuousResource implements Resource { |
57 | } | 57 | } |
58 | 58 | ||
59 | /** | 59 | /** |
60 | - * The user of this methods must receive the return value as Double or double. | ||
61 | - * Otherwise, this methods throws an exception. | ||
62 | - * | ||
63 | - * @param <T> type of the return value | ||
64 | - * @return the volume of this resource | ||
65 | - */ | ||
66 | - @SuppressWarnings("unchecked") | ||
67 | - @Override | ||
68 | - public <T> T volume() { | ||
69 | - return (T) Double.valueOf(value); | ||
70 | - } | ||
71 | - | ||
72 | - /** | ||
73 | * Returns the value of the resource amount. | 60 | * Returns the value of the resource amount. |
74 | * | 61 | * |
75 | * @return the value of the resource amount | 62 | * @return the value of the resource amount |
76 | */ | 63 | */ |
77 | - // FIXME: overlapping a purpose with volume() | ||
78 | public double value() { | 64 | public double value() { |
79 | return value; | 65 | return value; |
80 | } | 66 | } |
... | @@ -93,6 +79,24 @@ public final class ContinuousResource implements Resource { | ... | @@ -93,6 +79,24 @@ public final class ContinuousResource implements Resource { |
93 | return foundInAncestor || foundInLeaf; | 79 | return foundInAncestor || foundInLeaf; |
94 | } | 80 | } |
95 | 81 | ||
82 | + /** | ||
83 | + * {@inheritDoc} | ||
84 | + * | ||
85 | + * A user must specify Double.class or double.class to avoid an empty value. | ||
86 | + */ | ||
87 | + @Override | ||
88 | + public <T> Optional<T> valueAs(Class<T> type) { | ||
89 | + checkNotNull(type); | ||
90 | + | ||
91 | + if (type == Object.class || type == double.class || type == Double.class) { | ||
92 | + @SuppressWarnings("unchecked") | ||
93 | + T value = (T) Double.valueOf(this.value); | ||
94 | + return Optional.of(value); | ||
95 | + } | ||
96 | + | ||
97 | + return Optional.empty(); | ||
98 | + } | ||
99 | + | ||
96 | @Override | 100 | @Override |
97 | public Object last() { | 101 | public Object last() { |
98 | if (id.components().isEmpty()) { | 102 | if (id.components().isEmpty()) { |
... | @@ -138,7 +142,7 @@ public final class ContinuousResource implements Resource { | ... | @@ -138,7 +142,7 @@ public final class ContinuousResource implements Resource { |
138 | public String toString() { | 142 | public String toString() { |
139 | return MoreObjects.toStringHelper(this) | 143 | return MoreObjects.toStringHelper(this) |
140 | .add("id", id) | 144 | .add("id", id) |
141 | - .add("volume", value) | 145 | + .add("value", value) |
142 | .toString(); | 146 | .toString(); |
143 | } | 147 | } |
144 | } | 148 | } | ... | ... |
... | @@ -56,20 +56,6 @@ public final class DiscreteResource implements Resource { | ... | @@ -56,20 +56,6 @@ public final class DiscreteResource implements Resource { |
56 | return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass()); | 56 | return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass()); |
57 | } | 57 | } |
58 | 58 | ||
59 | - /** | ||
60 | - * The user of this methods must receive the return value as the correct type. | ||
61 | - * Otherwise, this methods throws an exception. | ||
62 | - * | ||
63 | - * @param <T> type of the return value | ||
64 | - * @return the volume of this resource | ||
65 | - */ | ||
66 | - @SuppressWarnings("unchecked") | ||
67 | - @Override | ||
68 | - // TODO: consider receiving Class<T> as an argument. Which approach is convenient? | ||
69 | - public <T> T volume() { | ||
70 | - return (T) last(); | ||
71 | - } | ||
72 | - | ||
73 | @Override | 59 | @Override |
74 | public boolean isSubTypeOf(Class<?> ancestor) { | 60 | public boolean isSubTypeOf(Class<?> ancestor) { |
75 | checkNotNull(ancestor); | 61 | checkNotNull(ancestor); |
... | @@ -82,6 +68,19 @@ public final class DiscreteResource implements Resource { | ... | @@ -82,6 +68,19 @@ public final class DiscreteResource implements Resource { |
82 | } | 68 | } |
83 | 69 | ||
84 | @Override | 70 | @Override |
71 | + public <T> Optional<T> valueAs(Class<T> type) { | ||
72 | + checkNotNull(type); | ||
73 | + | ||
74 | + if (!isTypeOf(type)) { | ||
75 | + return Optional.empty(); | ||
76 | + } | ||
77 | + | ||
78 | + @SuppressWarnings("unchecked") | ||
79 | + T value = (T) id.components().get(id.components().size() - 1); | ||
80 | + return Optional.of(value); | ||
81 | + } | ||
82 | + | ||
83 | + @Override | ||
85 | public Object last() { | 84 | public Object last() { |
86 | if (id.components().isEmpty()) { | 85 | if (id.components().isEmpty()) { |
87 | return null; | 86 | return null; |
... | @@ -133,7 +132,6 @@ public final class DiscreteResource implements Resource { | ... | @@ -133,7 +132,6 @@ public final class DiscreteResource implements Resource { |
133 | public String toString() { | 132 | public String toString() { |
134 | return MoreObjects.toStringHelper(this) | 133 | return MoreObjects.toStringHelper(this) |
135 | .add("id", id) | 134 | .add("id", id) |
136 | - .add("volume", volume()) | ||
137 | .toString(); | 135 | .toString(); |
138 | } | 136 | } |
139 | } | 137 | } | ... | ... |
... | @@ -64,13 +64,15 @@ public interface Resource { | ... | @@ -64,13 +64,15 @@ public interface Resource { |
64 | boolean isSubTypeOf(Class<?> ancestor); | 64 | boolean isSubTypeOf(Class<?> ancestor); |
65 | 65 | ||
66 | /** | 66 | /** |
67 | - * Returns the volume of this resource. | 67 | + * Returns value interpreted as the specified type. If the specified type is |
68 | + * incompatible with the underlying value, an empty instance is returned. | ||
68 | * | 69 | * |
69 | - * @param <T> type of return value | 70 | + * @param type class instance specifying the type of return value |
70 | - * @return the volume of this resource | 71 | + * @param <T> type of the return value |
72 | + * @return the value of this resource as the specified type. If type mismatches, | ||
73 | + * returns an empty instance. | ||
71 | */ | 74 | */ |
72 | - // TODO: think about other naming possibilities. amount? quantity? | 75 | + <T> Optional<T> valueAs(Class<T> type); |
73 | - <T> T volume(); | ||
74 | 76 | ||
75 | /** | 77 | /** |
76 | * Returns the last component of this instance. | 78 | * Returns the last component of this instance. | ... | ... |
... | @@ -126,18 +126,25 @@ public class ResourceTest { | ... | @@ -126,18 +126,25 @@ public class ResourceTest { |
126 | } | 126 | } |
127 | 127 | ||
128 | @Test | 128 | @Test |
129 | - public void testVolumeOfDiscrete() { | 129 | + public void testValueOfDiscrete() { |
130 | Resource resource = Resources.discrete(D1).resource(); | 130 | Resource resource = Resources.discrete(D1).resource(); |
131 | 131 | ||
132 | - DeviceId volume = resource.volume(); | 132 | + Optional<DeviceId> volume = resource.valueAs(DeviceId.class); |
133 | - assertThat(volume, is(D1)); | 133 | + assertThat(volume.get(), is(D1)); |
134 | } | 134 | } |
135 | 135 | ||
136 | @Test | 136 | @Test |
137 | - public void testVolumeOfContinuous() { | 137 | + public void testValueOfRoot() { |
138 | + Resource resource = Resource.ROOT; | ||
139 | + | ||
140 | + assertThat(resource.valueAs(Object.class), is(Optional.empty())); | ||
141 | + } | ||
142 | + | ||
143 | + @Test | ||
144 | + public void testValueOfContinuous() { | ||
138 | Resource resource = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()); | 145 | Resource resource = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()); |
139 | 146 | ||
140 | - double volume = resource.volume(); | 147 | + Optional<Double> volume = resource.valueAs(double.class); |
141 | - assertThat(volume, is(BW1.bps())); | 148 | + assertThat(volume.get(), is(BW1.bps())); |
142 | } | 149 | } |
143 | } | 150 | } | ... | ... |
... | @@ -27,6 +27,7 @@ import org.onlab.packet.EthType; | ... | @@ -27,6 +27,7 @@ import org.onlab.packet.EthType; |
27 | import org.onlab.packet.Ethernet; | 27 | import org.onlab.packet.Ethernet; |
28 | import org.onlab.packet.MplsLabel; | 28 | import org.onlab.packet.MplsLabel; |
29 | import org.onlab.packet.VlanId; | 29 | import org.onlab.packet.VlanId; |
30 | +import org.onlab.util.Tools; | ||
30 | import org.onosproject.core.ApplicationId; | 31 | import org.onosproject.core.ApplicationId; |
31 | import org.onosproject.core.CoreService; | 32 | import org.onosproject.core.CoreService; |
32 | import org.onosproject.net.ConnectPoint; | 33 | import org.onosproject.net.ConnectPoint; |
... | @@ -158,8 +159,7 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { | ... | @@ -158,8 +159,7 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { |
158 | 159 | ||
159 | private Set<MplsLabel> findMplsLabel(ConnectPoint cp) { | 160 | private Set<MplsLabel> findMplsLabel(ConnectPoint cp) { |
160 | return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream() | 161 | return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream() |
161 | - .filter(x -> x.last() instanceof MplsLabel) | 162 | + .flatMap(x -> Tools.stream(x.valueAs(MplsLabel.class))) |
162 | - .map(x -> (MplsLabel) x.last()) | ||
163 | .collect(Collectors.toSet()); | 163 | .collect(Collectors.toSet()); |
164 | } | 164 | } |
165 | 165 | ... | ... |
... | @@ -17,7 +17,6 @@ package org.onosproject.net.intent.impl.compiler; | ... | @@ -17,7 +17,6 @@ package org.onosproject.net.intent.impl.compiler; |
17 | 17 | ||
18 | import com.google.common.collect.ImmutableList; | 18 | import com.google.common.collect.ImmutableList; |
19 | import com.google.common.collect.ImmutableSet; | 19 | import com.google.common.collect.ImmutableSet; |
20 | -import com.google.common.collect.Iterables; | ||
21 | import com.google.common.collect.Sets; | 20 | import com.google.common.collect.Sets; |
22 | import org.apache.felix.scr.annotations.Activate; | 21 | import org.apache.felix.scr.annotations.Activate; |
23 | import org.apache.felix.scr.annotations.Component; | 22 | import org.apache.felix.scr.annotations.Component; |
... | @@ -25,6 +24,7 @@ import org.apache.felix.scr.annotations.Deactivate; | ... | @@ -25,6 +24,7 @@ import org.apache.felix.scr.annotations.Deactivate; |
25 | import org.apache.felix.scr.annotations.Reference; | 24 | import org.apache.felix.scr.annotations.Reference; |
26 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
27 | import org.onlab.util.Frequency; | 26 | import org.onlab.util.Frequency; |
27 | +import org.onlab.util.Tools; | ||
28 | import org.onosproject.net.AnnotationKeys; | 28 | import org.onosproject.net.AnnotationKeys; |
29 | import org.onosproject.net.ChannelSpacing; | 29 | import org.onosproject.net.ChannelSpacing; |
30 | import org.onosproject.net.ConnectPoint; | 30 | import org.onosproject.net.ConnectPoint; |
... | @@ -218,8 +218,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical | ... | @@ -218,8 +218,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical |
218 | Resources.discrete(x.dst().deviceId(), x.dst().port()).id() | 218 | Resources.discrete(x.dst().deviceId(), x.dst().port()).id() |
219 | )) | 219 | )) |
220 | .map(resourceService::getAvailableResources) | 220 | .map(resourceService::getAvailableResources) |
221 | - .map(x -> Iterables.filter(x, r -> r.last() instanceof OchSignal)) | 221 | + .map(x -> x.stream() |
222 | - .map(x -> Iterables.transform(x, r -> (OchSignal) r.last())) | 222 | + .flatMap(r -> Tools.stream(r.valueAs(OchSignal.class))).collect(Collectors.toList())) |
223 | .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x)) | 223 | .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x)) |
224 | .reduce(Sets::intersection) | 224 | .reduce(Sets::intersection) |
225 | .orElse(Collections.emptySet()); | 225 | .orElse(Collections.emptySet()); | ... | ... |
... | @@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Deactivate; | ... | @@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Deactivate; |
23 | import org.apache.felix.scr.annotations.Reference; | 23 | import org.apache.felix.scr.annotations.Reference; |
24 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
25 | import org.onlab.packet.VlanId; | 25 | import org.onlab.packet.VlanId; |
26 | +import org.onlab.util.Tools; | ||
26 | import org.onosproject.core.ApplicationId; | 27 | import org.onosproject.core.ApplicationId; |
27 | import org.onosproject.core.CoreService; | 28 | import org.onosproject.core.CoreService; |
28 | import org.onosproject.net.ConnectPoint; | 29 | import org.onosproject.net.ConnectPoint; |
... | @@ -286,8 +287,7 @@ public class PathIntentCompiler implements IntentCompiler<PathIntent> { | ... | @@ -286,8 +287,7 @@ public class PathIntentCompiler implements IntentCompiler<PathIntent> { |
286 | 287 | ||
287 | private Set<VlanId> findVlanId(ConnectPoint cp) { | 288 | private Set<VlanId> findVlanId(ConnectPoint cp) { |
288 | return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream() | 289 | return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream() |
289 | - .filter(x -> x.last() instanceof VlanId) | 290 | + .flatMap(x -> Tools.stream(x.valueAs(VlanId.class))) |
290 | - .map(x -> (VlanId) x.last()) | ||
291 | .collect(Collectors.toSet()); | 291 | .collect(Collectors.toSet()); |
292 | } | 292 | } |
293 | 293 | ... | ... |
-
Please register or login to post a comment