Sho SHIMIZU
Committed by Gerrit Code Review

Add a method to check the type of resource

Change-Id: If4b44688c0a93a43dfa9a4c2cb77c52599159751
......@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import com.google.common.collect.Iterables;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
......@@ -158,7 +159,7 @@ public class ResourcesCommand extends AbstractShellCommand {
if (r instanceof ContinuousResource) {
// non-aggregatable terminal node
nonAggregatable.add(r);
} else if (aggregatableTypes.contains(r.last().getClass())) {
} else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
// aggregatable & terminal node
String className = r.last().getClass().getSimpleName();
aggregatables.put(className, r);
......
......@@ -48,6 +48,14 @@ public final class ContinuousResource implements Resource {
return id;
}
@Override
public boolean isTypeOf(Class<?> type) {
checkNotNull(type);
String typeName = (String) id.components().get(id.components().size() - 1);
return typeName.equals(type.getCanonicalName());
}
/**
* The user of this methods must receive the return value as Double or double.
* Otherwise, this methods throws an exception.
......
......@@ -45,6 +45,17 @@ public final class DiscreteResource implements Resource {
return id;
}
@Override
public boolean isTypeOf(Class<?> type) {
checkNotNull(type);
if (isRoot()) {
return false;
}
return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass());
}
/**
* The user of this methods must receive the return value as the correct type.
* Otherwise, this methods throws an exception.
......@@ -78,6 +89,10 @@ public final class DiscreteResource implements Resource {
return id.components().get(id.components().size() - 1);
}
private boolean isRoot() {
return id.equals(ResourceId.ROOT);
}
@Override
public DiscreteResource child(Object child) {
checkArgument(!(child instanceof Class<?>));
......
......@@ -48,6 +48,14 @@ public interface Resource {
ResourceId id();
/**
* Checks if the type of this instance is the specified type.
*
* @param type type of resource to be checked
* @return true if this resource is the type of the specified type. Otherwise, false.
*/
boolean isTypeOf(Class<?> type);
/**
* Checks if the type of this instance is the sub-type of the specified type.
*
* @param ancestor type of resource to be checked.
......
......@@ -90,6 +90,19 @@ public class ResourceTest {
}
@Test
public void testTypeOf() {
DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
assertThat(discrete.isTypeOf(DeviceId.class), is(false));
assertThat(discrete.isTypeOf(PortNumber.class), is(false));
assertThat(discrete.isTypeOf(VlanId.class), is(true));
ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps());
assertThat(continuous.isTypeOf(DeviceId.class), is(false));
assertThat(continuous.isTypeOf(PortNumber.class), is(false));
assertThat(continuous.isTypeOf(Bandwidth.class), is(true));
}
@Test
public void testSubTypeOf() {
DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
assertThat(discrete.isSubTypeOf(DeviceId.class), is(true));
......
......@@ -429,7 +429,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
}
Stream<DiscreteResource> discrete = children.value().stream()
.filter(x -> x.last().getClass().equals(cls))
.filter(x -> x.isTypeOf(cls))
.filter(x -> x instanceof DiscreteResource)
.map(x -> ((DiscreteResource) x))
.filter(x -> discreteConsumers.containsKey(x.id()));
......