(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / adapters / SetterConfiguration.java
index 649e008fae1f7f1ff0f17eff1b0c3923cd906db9..f81c4a4bc405837d893f5e021635a3b3bd1e0137 100644 (file)
@@ -100,21 +100,40 @@ public class SetterConfiguration {
         * Removes a setter from the set of methods.
         * 
         * @param aName
-        *            Name of the setter to remove (without the "set" prefix).
+        *            Name of the setter to remove.
         * @return Reference to the current object to allow call chaining.
         */
        public SetterConfiguration remove(String aName) {
-               final String name = createSetterName(aName);
-               Map<Method, ParameterValues> setters = new HashMap<Method, ParameterValues>();
                for (Method method : _setters.keySet()) {
-                       if (method.getName().equals(name)) {
+                       if (method.getName().equals(aName)) {
                                _setters.remove(method);
                                return this;
                        }
                }
                throw new IllegalArgumentException(
-                               "No setter configured by the name of '" + aName + "'");
+                               "No method configured by the name of '" + aName + "'");
        }
+       
+       /**
+        * Removes the method from the set of methods. 
+        * @param aMethod Method to remove. 
+        * @return
+        */
+       public SetterConfiguration remove(Method aMethod) { 
+           if ( !aMethod.getDeclaringClass().isAssignableFrom(_class) ) { 
+               throw new RuntimeException("Method " + aMethod + " not found in class " + _class + " or its superclasses");
+           }
+           for (Method method : _setters.keySet()) {
+            if (method.equals(aMethod)) {
+                _setters.remove(method);
+                return this;
+            }
+        }
+        throw new IllegalArgumentException(
+                "Method '" + aMethod + "' was not configured. ");
+       }
+       
+       
 
        /**
         * Creates the name of a setter based on the name of the setter without the
@@ -124,31 +143,30 @@ public class SetterConfiguration {
         *            Setter name.
         * @return Setter name.
         */
-       private String createSetterName(String aName) {
+       private String createxSetterName(String aName) {
                return "set" + aName.substring(0, 1).toUpperCase() + aName.substring(1);
        }
 
        /**
         * Adds a given setter name to the setters.
         * 
-        * @param aName
+        * @param aName Name of a setter method.
         * @return Reference to the current object to allow call chaining.
         */
-       public SetterConfiguration add(String aName) {
-               final String name = createSetterName(aName);
+       public SetterConfiguration add(final String aName) {
                int oldlen = _setters.size();
                List<Method> methods = new ArrayList<Method>();
                CollectionFilter.filter(getAllSetters(_class, _publicOnly), methods,
                                new Condition<Method>() {
                                        @Override
                                        public boolean matches(Method aObject) {
-                                               return aObject.getName().equals(name);
+                                               return aObject.getName().equals(aName);
                                        }
 
                                });
                if (methods.size() == 0 ) {
-                       throw new IllegalArgumentException("No setter found for '" + aName
-                                       + "' in " + _class.getName());
+                       throw new IllegalArgumentException("Method '" + aName
+                                       + "' not found in " + _class.getName());
                }
                // TODO is it possible to get more than one setter here in case the subclass overrides
                // the baseclass method? 
@@ -220,8 +238,9 @@ public class SetterConfiguration {
        }
 
        private static ParameterValues createParameterValues(Method method) {
+           // TODO generalize to multiple parameters. 
                return new ParameterValues(
-                               new String[] { getSetterName(method) }, new Class[] { method
+                               new String[] { method.getName() }, new Class[] { method
                                                .getParameterTypes()[0] });
        }
 
@@ -280,13 +299,12 @@ public class SetterConfiguration {
         * @return Parameter values.
         */
        public ParameterValues values(String aMethod) {
-               String name = createSetterName(aMethod);
                for (Method method : _setters.keySet()) {
-                       if (method.getName().equals(name)) {
+                       if (method.getName().equals(aMethod)) {
                                return _setters.get(method);
                        }
                }
-               throw new IllegalArgumentException("No setter method '" + name
+               throw new IllegalArgumentException("No setter method '" + aMethod
                                + "' found");
        }
 
@@ -299,7 +317,7 @@ public class SetterConfiguration {
         *            Method.
         * @return Setter name.
         */
-       private static String getSetterName(Method aMethod) {
+       private static String getxSetterName(Method aMethod) {
                String result = aMethod.getName().substring(3);
                return result.substring(0, 1).toLowerCase() + result.substring(1);
        }