commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1633693 - in /commons/sandbox/beanutils2/trunk/src: changes/ main/java/org/apache/commons/beanutils2/ test/java/org/apache/commons/beanutils2/
Date Wed, 22 Oct 2014 19:37:26 GMT
Author: britter
Date: Wed Oct 22 19:37:25 2014
New Revision: 1633693

URL: http://svn.apache.org/r1633693
Log:
SANDBOX-482: Add short cut methods for accessing property values

Modified:
    commons/sandbox/beanutils2/trunk/src/changes/changes.xml
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/IndexedPropertyGetterAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MapBeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MappedPropertyGetterAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetIndexedPropertyTestCase.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetMappedPropertyTestCase.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java

Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/changes/changes.xml?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Wed Oct 22 19:37:25 2014
@@ -23,6 +23,9 @@
   </properties>
   <body>
   <release version="2.0" date="TBA" description="Redesign of beanutils with a fluent API">
+    <action dev="britter" type="add" issue="SANDBOX-482">
+      Add short cut methods for accessing property values
+    </action>
     <action dev="britter" type="add" issue="SANDBOX-478">
       BeanUtils and Maps
     </action>

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessor.java
Wed Oct 22 19:37:25 2014
@@ -40,6 +40,16 @@ public interface BeanAccessor<B> {
     BeanAccessor<?> get(String propertyName);
 
     /**
+     * Gets the value of the property with name {@code propertyName} from the bean wrapped
by this BeanAccessor. This
+     * is a shortcut for calling {@link #get(String)} followed by {@link #get()}.
+     *
+     * @param <V> the type of the property
+     * @param propertyName the name of the property to get the value from. Must not be {@code
null}!
+     * @return a {@code BeanAccessor} wrapping the properties value.
+     */
+    <V> V getValue(String propertyName);
+
+    /**
      * Selects the indexed property with name {@code propertyName} from the bean wrapped
by this {@code BeanAccessor}. A
      * {@link IndexedPropertyGetterAccessor} will be returned for specifying the index to
return the value of.
      *

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
Wed Oct 22 19:37:25 2014
@@ -49,6 +49,11 @@ final class DefaultBeanAccessor<B>
      * {@inheritDoc}
      */
     public BeanAccessor<?> get(String propertyName) {
+        Object value = getValue(propertyName);
+        return new DefaultBeanAccessor<Object>(value);
+    }
+
+    public <V> V getValue(String propertyName) {
         checkNotNull(propertyName, "Parameter 'propertyName' must not be null!");
         Method readMethod = properties.getReadPropertyMethod(propertyName);
 
@@ -60,7 +65,7 @@ final class DefaultBeanAccessor<B>
         } catch (InvocationTargetException e) {
             throw new PropertyGetterInvocationException(propertyName, readMethod.getName(),
bean.getClass(), e);
         }
-        return new DefaultBeanAccessor<Object>(newBean);
+        return (V) newBean;
     }
 
     /**

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
Wed Oct 22 19:37:25 2014
@@ -43,15 +43,22 @@ final class DefaultIndexedPropertyGetter
      * {@inheritDoc}
      */
     public BeanAccessor<?> at(int index) {
-        checkArgument(index >= 0, "Indexed property '%s' in bean of type %s cannot be
get from a negative index %s",
-                propertyName, bean.getClass().getName(), index);
-        Object indexedValue = invokeGetter(index);
+        Object indexedValue = valueAt(index);
         if (indexedValue == null) {
             return new NullBeanAccessor<Object>(bean.getClass().getName(), indexedReadMethod.getName());
         }
         return BeanAccessorFactory.createBeanAccessor(indexedValue);
     }
 
+    public <V> V valueAt(int index) {
+        checkArgument(index >= 0, "Indexed property '%s' in bean of type %s cannot be
get from a negative index %s",
+                propertyName, bean.getClass().getName(), index);
+        // V is determined by the user
+        @SuppressWarnings("unchecked")
+        V indexedValue = (V) invokeGetter(index);
+        return indexedValue;
+    }
+
     private Object invokeGetter(int index) {
         try {
             return indexedReadMethod.invoke(bean, index);

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
Wed Oct 22 19:37:25 2014
@@ -48,10 +48,20 @@ class DefaultMappedPropertyGetterAccesso
      * {@inheritDoc}
      */
     public BeanAccessor<?> of(String key) {
+        Object mappedValue = valueOf(key);
+        return wrapInAccessor(mappedValue);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public <V> V valueOf(String key) {
         checkNotNull(key, "Can not get mapped property '%s' in bean of type '%s' for null!",
propertyName,
                 bean.getClass().getName());
-        Object mappedValue = invokeGetter(key);
-        return wrapInAccessor(mappedValue);
+        // V is determined by the user
+        @SuppressWarnings("unchecked")
+        V mappedValue = (V) invokeGetter(key);
+        return mappedValue;
     }
 
     private Object invokeGetter(String key) {

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/IndexedPropertyGetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/IndexedPropertyGetterAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/IndexedPropertyGetterAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/IndexedPropertyGetterAccessor.java
Wed Oct 22 19:37:25 2014
@@ -23,4 +23,6 @@ public interface IndexedPropertyGetterAc
 
     BeanAccessor<?> at(int index);
 
+    <V> V valueAt(int index);
+
 }

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MapBeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MapBeanAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MapBeanAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MapBeanAccessor.java
Wed Oct 22 19:37:25 2014
@@ -46,6 +46,16 @@ final class MapBeanAccessor<B> implement
         throw new NoSuchPropertyException(propertyName, map.getClass(), null);
     }
 
+    public <V> V getValue(String propertyName) {
+        checkNotNull(propertyName, "Parameter 'propertyName' must not be null!");
+        if (map.containsKey(propertyName)) {
+            Object value = map.get(propertyName);
+            return (V) value;
+        }
+        // TODO better default message doesn't fit for map access
+        throw new NoSuchPropertyException(propertyName, map.getClass(), null);
+    }
+
     public IndexedPropertyGetterAccessor<?> getIndexed(String propertyName) {
         throw new NoSuchPropertyException(propertyName, map.getClass(), null);
     }

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MappedPropertyGetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MappedPropertyGetterAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MappedPropertyGetterAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/MappedPropertyGetterAccessor.java
Wed Oct 22 19:37:25 2014
@@ -23,4 +23,5 @@ public interface MappedPropertyGetterAcc
 
     BeanAccessor<?> of(String key);
 
+    <V> V valueOf(String key);
 }

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java
Wed Oct 22 19:37:25 2014
@@ -43,6 +43,13 @@ final class NullBeanAccessor<B>
     /**
      * {@inheritDoc}
      */
+    public <V> V getValue(String propertyName) {
+        throw new NullPointerException(errorMessage);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     public IndexedPropertyGetterAccessor<?> getIndexed(String propertyName) {
         throw new NullPointerException(errorMessage);
     }

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetIndexedPropertyTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetIndexedPropertyTestCase.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetIndexedPropertyTestCase.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetIndexedPropertyTestCase.java
Wed Oct 22 19:37:25 2014
@@ -69,6 +69,19 @@ public class GetIndexedPropertyTestCase 
         assertEquals(expected, accessor.get());
     }
 
+    @Test(expected = IllegalArgumentException.class)
+    public void getIndexedValueNegative() {
+        on(testBean).getIndexed("intIndexed").valueAt(-1);
+    }
+
+    @Test
+    public void getIndexedValue() throws Exception {
+        int expected = testBean.getIntIndexed(0);
+        Integer value = on(testBean).getIndexed("intIndexed").valueAt(0);
+        assertNotNull(value);
+        assertEquals(expected, value.intValue());
+    }
+
     @Test
     public void getIndexedForNullIndex() throws Exception {
         testBean.setStringIndexed(0, null);

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetMappedPropertyTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetMappedPropertyTestCase.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetMappedPropertyTestCase.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetMappedPropertyTestCase.java
Wed Oct 22 19:37:25 2014
@@ -89,6 +89,24 @@ public class GetMappedPropertyTestCase {
         assertEquals("First Value", result.get());
     }
 
+    @Test
+    public void getMappedValueUnknownKey() throws Exception {
+        Object result = on(testBean).getMapped("mappedProperty").valueOf("unknownKey");
+        assertNull(result);
+    }
+
+    @Test
+    public void getMappedValueMappedProperty() throws Exception {
+        String result = on(testBean).getMapped("mappedProperty").valueOf("First Key");
+        assertEquals("First Value", result);
+    }
+
+    @Test
+    public void getMappedValueMappedIntProperty() throws Exception {
+        Integer result = on(testBean).getMapped("mappedIntProperty").valueOf("One");
+        assertEquals(Integer.valueOf(1), result);
+    }
+
     @Test(expected = PropertyGetterInvocationException.class)
     public void getExceptionMapped() {
         on(exceptionBean).getMapped("exceptionMapped").of("A Key");

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
Wed Oct 22 19:37:25 2014
@@ -20,6 +20,7 @@
 package org.apache.commons.beanutils2;
 
 import static org.apache.commons.beanutils2.BeanUtils.on;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -96,4 +97,13 @@ public final class GetPropertyTestCase {
         on(exceptionBean).get("exceptionProperty");
     }
 
+    @Test(expected = NullPointerException.class)
+    public void getValueNull() throws Exception {
+        on(bean).getValue(null);
+    }
+
+    @Test
+    public void getValueSimpleBoolean() throws Exception {
+        assertEquals(bean.getBooleanProperty(), on(bean).getValue("booleanProperty"));
+    }
 }

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java?rev=1633693&r1=1633692&r2=1633693&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java
Wed Oct 22 19:37:25 2014
@@ -71,6 +71,16 @@ public class MapBeanTestCase {
         assertEquals(on(map).get("integerProperty").get(), Integer.valueOf(15));
     }
 
+    @Test(expected = NoSuchPropertyException.class)
+    public void missingPropertyGetValueThrowsException() throws Exception {
+        on(map).getValue("nonExistingProperty");
+    }
+
+    @Test
+    public void getValueReturnsIntegerProperty() throws Exception {
+        assertEquals(on(map).getValue("integerProperty"), Integer.valueOf(15));
+    }
+
     @Test
     public void returnsNestedMap() throws Exception {
         assertEquals(Boolean.TRUE, on(map).get("nestedMap").get("booleanProperty").get());



Mime
View raw message