Committed by
Gerrit Code Review
Add a method to check the type of resource
Change-Id: If4b44688c0a93a43dfa9a4c2cb77c52599159751
Showing
6 changed files
with
47 additions
and
2 deletions
... | @@ -24,6 +24,7 @@ import java.util.ArrayList; | ... | @@ -24,6 +24,7 @@ import java.util.ArrayList; |
24 | import java.util.Arrays; | 24 | import java.util.Arrays; |
25 | import java.util.Collections; | 25 | import java.util.Collections; |
26 | 26 | ||
27 | +import com.google.common.collect.Iterables; | ||
27 | import org.apache.karaf.shell.commands.Argument; | 28 | import org.apache.karaf.shell.commands.Argument; |
28 | import org.apache.karaf.shell.commands.Command; | 29 | import org.apache.karaf.shell.commands.Command; |
29 | import org.apache.karaf.shell.commands.Option; | 30 | import org.apache.karaf.shell.commands.Option; |
... | @@ -158,7 +159,7 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -158,7 +159,7 @@ public class ResourcesCommand extends AbstractShellCommand { |
158 | if (r instanceof ContinuousResource) { | 159 | if (r instanceof ContinuousResource) { |
159 | // non-aggregatable terminal node | 160 | // non-aggregatable terminal node |
160 | nonAggregatable.add(r); | 161 | nonAggregatable.add(r); |
161 | - } else if (aggregatableTypes.contains(r.last().getClass())) { | 162 | + } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) { |
162 | // aggregatable & terminal node | 163 | // aggregatable & terminal node |
163 | String className = r.last().getClass().getSimpleName(); | 164 | String className = r.last().getClass().getSimpleName(); |
164 | aggregatables.put(className, r); | 165 | aggregatables.put(className, r); | ... | ... |
... | @@ -48,6 +48,14 @@ public final class ContinuousResource implements Resource { | ... | @@ -48,6 +48,14 @@ public final class ContinuousResource implements Resource { |
48 | return id; | 48 | return id; |
49 | } | 49 | } |
50 | 50 | ||
51 | + @Override | ||
52 | + public boolean isTypeOf(Class<?> type) { | ||
53 | + checkNotNull(type); | ||
54 | + | ||
55 | + String typeName = (String) id.components().get(id.components().size() - 1); | ||
56 | + return typeName.equals(type.getCanonicalName()); | ||
57 | + } | ||
58 | + | ||
51 | /** | 59 | /** |
52 | * The user of this methods must receive the return value as Double or double. | 60 | * The user of this methods must receive the return value as Double or double. |
53 | * Otherwise, this methods throws an exception. | 61 | * Otherwise, this methods throws an exception. | ... | ... |
... | @@ -45,6 +45,17 @@ public final class DiscreteResource implements Resource { | ... | @@ -45,6 +45,17 @@ public final class DiscreteResource implements Resource { |
45 | return id; | 45 | return id; |
46 | } | 46 | } |
47 | 47 | ||
48 | + @Override | ||
49 | + public boolean isTypeOf(Class<?> type) { | ||
50 | + checkNotNull(type); | ||
51 | + | ||
52 | + if (isRoot()) { | ||
53 | + return false; | ||
54 | + } | ||
55 | + | ||
56 | + return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass()); | ||
57 | + } | ||
58 | + | ||
48 | /** | 59 | /** |
49 | * The user of this methods must receive the return value as the correct type. | 60 | * The user of this methods must receive the return value as the correct type. |
50 | * Otherwise, this methods throws an exception. | 61 | * Otherwise, this methods throws an exception. |
... | @@ -78,6 +89,10 @@ public final class DiscreteResource implements Resource { | ... | @@ -78,6 +89,10 @@ public final class DiscreteResource implements Resource { |
78 | return id.components().get(id.components().size() - 1); | 89 | return id.components().get(id.components().size() - 1); |
79 | } | 90 | } |
80 | 91 | ||
92 | + private boolean isRoot() { | ||
93 | + return id.equals(ResourceId.ROOT); | ||
94 | + } | ||
95 | + | ||
81 | @Override | 96 | @Override |
82 | public DiscreteResource child(Object child) { | 97 | public DiscreteResource child(Object child) { |
83 | checkArgument(!(child instanceof Class<?>)); | 98 | checkArgument(!(child instanceof Class<?>)); | ... | ... |
... | @@ -48,6 +48,14 @@ public interface Resource { | ... | @@ -48,6 +48,14 @@ public interface Resource { |
48 | ResourceId id(); | 48 | ResourceId id(); |
49 | 49 | ||
50 | /** | 50 | /** |
51 | + * Checks if the type of this instance is the specified type. | ||
52 | + * | ||
53 | + * @param type type of resource to be checked | ||
54 | + * @return true if this resource is the type of the specified type. Otherwise, false. | ||
55 | + */ | ||
56 | + boolean isTypeOf(Class<?> type); | ||
57 | + | ||
58 | + /** | ||
51 | * Checks if the type of this instance is the sub-type of the specified type. | 59 | * Checks if the type of this instance is the sub-type of the specified type. |
52 | * | 60 | * |
53 | * @param ancestor type of resource to be checked. | 61 | * @param ancestor type of resource to be checked. | ... | ... |
... | @@ -90,6 +90,19 @@ public class ResourceTest { | ... | @@ -90,6 +90,19 @@ public class ResourceTest { |
90 | } | 90 | } |
91 | 91 | ||
92 | @Test | 92 | @Test |
93 | + public void testTypeOf() { | ||
94 | + DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource(); | ||
95 | + assertThat(discrete.isTypeOf(DeviceId.class), is(false)); | ||
96 | + assertThat(discrete.isTypeOf(PortNumber.class), is(false)); | ||
97 | + assertThat(discrete.isTypeOf(VlanId.class), is(true)); | ||
98 | + | ||
99 | + ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()); | ||
100 | + assertThat(continuous.isTypeOf(DeviceId.class), is(false)); | ||
101 | + assertThat(continuous.isTypeOf(PortNumber.class), is(false)); | ||
102 | + assertThat(continuous.isTypeOf(Bandwidth.class), is(true)); | ||
103 | + } | ||
104 | + | ||
105 | + @Test | ||
93 | public void testSubTypeOf() { | 106 | public void testSubTypeOf() { |
94 | DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource(); | 107 | DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource(); |
95 | assertThat(discrete.isSubTypeOf(DeviceId.class), is(true)); | 108 | assertThat(discrete.isSubTypeOf(DeviceId.class), is(true)); | ... | ... |
... | @@ -429,7 +429,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -429,7 +429,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
429 | } | 429 | } |
430 | 430 | ||
431 | Stream<DiscreteResource> discrete = children.value().stream() | 431 | Stream<DiscreteResource> discrete = children.value().stream() |
432 | - .filter(x -> x.last().getClass().equals(cls)) | 432 | + .filter(x -> x.isTypeOf(cls)) |
433 | .filter(x -> x instanceof DiscreteResource) | 433 | .filter(x -> x instanceof DiscreteResource) |
434 | .map(x -> ((DiscreteResource) x)) | 434 | .map(x -> ((DiscreteResource) x)) |
435 | .filter(x -> discreteConsumers.containsKey(x.id())); | 435 | .filter(x -> discreteConsumers.containsKey(x.id())); | ... | ... |
-
Please register or login to post a comment