Sho SHIMIZU

Refactor Resoruce/ResourceId sub-types

isTypeOf() and isSubTypeOf() of Resource is delegated to ResourceId

Change-Id: Ief39f4967b8fb43ec0cb35dbd44bd2a7dde93680
......@@ -57,8 +57,7 @@ public final class ContinuousResource implements Resource {
public boolean isTypeOf(Class<?> type) {
checkNotNull(type);
String typeName = (String) id.components().get(id.components().size() - 1);
return typeName.equals(type.getCanonicalName());
return id.isTypeOf(type);
}
/**
......@@ -74,13 +73,7 @@ public final class ContinuousResource implements Resource {
public boolean isSubTypeOf(Class<?> ancestor) {
checkNotNull(ancestor);
String typeName = (String) id.components().get(id.components().size() - 1);
boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName());
boolean foundInAncestor = id.components().subList(0, id.components().size()).stream()
.filter(x -> ancestor.isAssignableFrom(x.getClass()))
.findAny()
.isPresent();
return foundInAncestor || foundInLeaf;
return id.isSubTypeOf(ancestor);
}
/**
......
......@@ -52,6 +52,23 @@ public final class ContinuousResourceId extends ResourceId {
return name;
}
@Override
boolean isTypeOf(Class<?> type) {
String typeName = (String) lastComponent();
return typeName.equals(type.getCanonicalName());
}
@Override
boolean isSubTypeOf(Class<?> ancestor) {
String typeName = (String) lastComponent();
boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName());
boolean foundInAncestor = components.subList(0, components.size()).stream()
.filter(x -> ancestor.isAssignableFrom(x.getClass()))
.findAny()
.isPresent();
return foundInAncestor || foundInLeaf;
}
/**
* {@inheritDoc}
*
......@@ -86,6 +103,10 @@ public final class ContinuousResourceId extends ResourceId {
}
}
private Object lastComponent() {
return components.get(components.size() - 1);
}
@Override
public int hashCode() {
return components.hashCode();
......
......@@ -54,38 +54,21 @@ public final class DiscreteResource implements Resource {
public boolean isTypeOf(Class<?> type) {
checkNotNull(type);
if (isRoot()) {
return false;
}
return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass());
return id.isTypeOf(type);
}
@Override
public boolean isSubTypeOf(Class<?> ancestor) {
checkNotNull(ancestor);
return id.components().stream()
.filter(x -> ancestor.isAssignableFrom(x.getClass()))
.findAny()
.isPresent();
return id.isSubTypeOf(ancestor);
}
@Override
public <T> Optional<T> valueAs(Class<T> type) {
checkNotNull(type);
if (!isTypeOf(type)) {
return Optional.empty();
}
@SuppressWarnings("unchecked")
T value = (T) id.components().get(id.components().size() - 1);
return Optional.of(value);
}
private boolean isRoot() {
return id.equals(ResourceId.ROOT);
return id.lastComponentAs(type);
}
@Override
......
......@@ -46,11 +46,28 @@ public final class DiscreteResourceId extends ResourceId {
@Override
String simpleTypeName() {
if (components.isEmpty()) {
if (isRoot()) {
return "Root";
}
return components.get(components.size() - 1).getClass().getSimpleName();
return lastComponent().getClass().getSimpleName();
}
@Override
boolean isTypeOf(Class<?> type) {
if (isRoot()) {
return false;
}
return type.isAssignableFrom(lastComponent().getClass());
}
@Override
boolean isSubTypeOf(Class<?> ancestor) {
return components.stream()
.filter(x -> ancestor.isAssignableFrom(x.getClass()))
.findAny()
.isPresent();
}
@Override
......@@ -69,7 +86,7 @@ public final class DiscreteResourceId extends ResourceId {
@Override
public Optional<DiscreteResourceId> parent() {
if (components.size() == 0) {
if (isRoot()) {
return Optional.empty();
}
if (components.size() == 1) {
......@@ -79,6 +96,25 @@ public final class DiscreteResourceId extends ResourceId {
}
}
<T> Optional<T> lastComponentAs(Class<T> type) {
if (!isTypeOf(type)) {
return Optional.empty();
}
@SuppressWarnings("unchecked")
T value = (T) lastComponent();
return Optional.of(value);
}
private boolean isRoot() {
return components.isEmpty();
}
private Object lastComponent() {
return components.get(components.size() - 1);
}
@Override
public int hashCode() {
return components.hashCode();
......
......@@ -31,6 +31,12 @@ public abstract class ResourceId {
abstract String simpleTypeName();
// caller must pass a non-null value
abstract boolean isTypeOf(Class<?> type);
// caller must pass a non-null value
abstract boolean isSubTypeOf(Class<?> ancestor);
/**
* Returns the parent resource ID of this instance.
*
......