Define valueAs() to get the enclosed value and remove volume()

volume() is replaced by valueAs()

Change-Id: I3dbcbd6a0b8fcd28f0064272fe1fa6d7259e0a87
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 a55124e..00221ae 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,24 +57,10 @@
     }
 
     /**
-     * The user of this methods must receive the return value as Double or double.
-     * Otherwise, this methods throws an exception.
-     *
-     * @param <T> type of the return value
-     * @return the volume of this resource
-     */
-    @SuppressWarnings("unchecked")
-    @Override
-    public <T> T volume() {
-        return (T) Double.valueOf(value);
-    }
-
-    /**
      * Returns the value of the resource amount.
      *
      * @return the value of the resource amount
      */
-    // FIXME: overlapping a purpose with volume()
     public double value() {
         return value;
     }
@@ -93,6 +79,24 @@
         return foundInAncestor || foundInLeaf;
     }
 
+    /**
+     * {@inheritDoc}
+     *
+     * A user must specify Double.class or double.class to avoid an empty value.
+     */
+    @Override
+    public <T> Optional<T> valueAs(Class<T> type) {
+        checkNotNull(type);
+
+        if (type == Object.class || type == double.class || type == Double.class) {
+            @SuppressWarnings("unchecked")
+            T value = (T) Double.valueOf(this.value);
+            return Optional.of(value);
+        }
+
+        return Optional.empty();
+    }
+
     @Override
     public Object last() {
         if (id.components().isEmpty()) {
@@ -138,7 +142,7 @@
     public String toString() {
         return MoreObjects.toStringHelper(this)
                 .add("id", id)
-                .add("volume", value)
+                .add("value", value)
                 .toString();
     }
 }
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 6ce51d3..beb564a 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
@@ -56,20 +56,6 @@
         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.
-     *
-     * @param <T> type of the return value
-     * @return the volume of this resource
-     */
-    @SuppressWarnings("unchecked")
-    @Override
-    // TODO: consider receiving Class<T> as an argument. Which approach is convenient?
-    public <T> T volume() {
-        return (T) last();
-    }
-
     @Override
     public boolean isSubTypeOf(Class<?> ancestor) {
         checkNotNull(ancestor);
@@ -82,6 +68,19 @@
     }
 
     @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);
+    }
+
+    @Override
     public Object last() {
         if (id.components().isEmpty()) {
             return null;
@@ -133,7 +132,6 @@
     public String toString() {
         return MoreObjects.toStringHelper(this)
                 .add("id", id)
-                .add("volume", volume())
                 .toString();
     }
 }
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
index f2a0c73..5572592 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
@@ -64,13 +64,15 @@
     boolean isSubTypeOf(Class<?> ancestor);
 
     /**
-     * Returns the volume of this resource.
+     * Returns value interpreted as the specified type. If the specified type is
+     * incompatible with the underlying value, an empty instance is returned.
      *
-     * @param <T> type of return value
-     * @return the volume of this resource
+     * @param type class instance specifying the type of return value
+     * @param <T> type of the return value
+     * @return the value of this resource as the specified type. If type mismatches,
+     * returns an empty instance.
      */
-    // TODO: think about other naming possibilities. amount? quantity?
-    <T> T volume();
+    <T> Optional<T> valueAs(Class<T> type);
 
     /**
      * Returns the last component of this instance.
diff --git a/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java b/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java
index bc6fd29..b404d10 100644
--- a/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java
+++ b/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java
@@ -126,18 +126,25 @@
     }
 
     @Test
-    public void testVolumeOfDiscrete() {
+    public void testValueOfDiscrete() {
         Resource resource = Resources.discrete(D1).resource();
 
-        DeviceId volume = resource.volume();
-        assertThat(volume, is(D1));
+        Optional<DeviceId> volume = resource.valueAs(DeviceId.class);
+        assertThat(volume.get(), is(D1));
     }
 
     @Test
-    public void testVolumeOfContinuous() {
+    public void testValueOfRoot() {
+        Resource resource = Resource.ROOT;
+
+        assertThat(resource.valueAs(Object.class), is(Optional.empty()));
+    }
+
+    @Test
+    public void testValueOfContinuous() {
         Resource resource = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps());
 
-        double volume = resource.volume();
-        assertThat(volume, is(BW1.bps()));
+        Optional<Double> volume = resource.valueAs(double.class);
+        assertThat(volume.get(), is(BW1.bps()));
     }
 }