Refactor Resoruce/ResourceId sub-types

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

Change-Id: Ief39f4967b8fb43ec0cb35dbd44bd2a7dde93680
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java
index f5a9f46..3625ce0 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java
@@ -57,8 +57,7 @@
     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 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);
     }
 
     /**
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResourceId.java b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResourceId.java
index 8a042c8..f372c59 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResourceId.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResourceId.java
@@ -52,6 +52,23 @@
         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 @@
         }
     }
 
+    private Object lastComponent() {
+        return components.get(components.size() - 1);
+    }
+
     @Override
     public int hashCode() {
         return components.hashCode();
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java
index 6504f6d..8bf0e0a 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java
@@ -54,38 +54,21 @@
     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
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResourceId.java b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResourceId.java
index 70e0119..2f52471 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResourceId.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResourceId.java
@@ -46,11 +46,28 @@
 
     @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 @@
 
     @Override
     public Optional<DiscreteResourceId> parent() {
-        if (components.size() == 0) {
+        if (isRoot()) {
             return Optional.empty();
         }
         if (components.size() == 1) {
@@ -79,6 +96,25 @@
         }
     }
 
+    <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();
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceId.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceId.java
index 002dc87..c4df116 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceId.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceId.java
@@ -31,6 +31,12 @@
 
     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.
      *