updated coding rules.
[utils] / system / general / src / main / java / org / wamblee / system / adapters / SetterConfiguration.java
index f81c4a4bc405837d893f5e021635a3b3bd1e0137..77fd9720b4c66c9ac69a12bce27862a1458e5d41 100644 (file)
@@ -47,9 +47,9 @@ import org.wamblee.system.core.SystemAssemblyException;
 public class SetterConfiguration {
 
        private Class _class;
-       private boolean _publicOnly;
+       private boolean publicOnly;
 
-       private Map<Method, ParameterValues> _setters;
+       private Map<Method, ParameterValues> setters;
 
        /**
         * Constructs the setter configuration. By default no setters are added. 
@@ -59,17 +59,17 @@ public class SetterConfiguration {
         */
        public SetterConfiguration(Class aClass) {
                _class = aClass;
-               _publicOnly = true;
-               _setters = new HashMap<Method, ParameterValues>();
+               publicOnly = true;
+               setters = new HashMap<Method, ParameterValues>();
        }
 
        /**
         * Makes sure that all available setters are used. 
         */
        public SetterConfiguration initAllSetters() {
-           _setters.clear();
-               for (Method method: getAllSetters(_class, _publicOnly) ) { 
-                       _setters.put(method, createParameterValues(method));
+           setters.clear();
+               for (Method method: getAllSetters(_class, publicOnly) ) { 
+                       setters.put(method, createParameterValues(method));
                }
                return this; 
        }
@@ -82,7 +82,7 @@ public class SetterConfiguration {
         *            Non public flag.
         */
        public SetterConfiguration setNonPublic(boolean aIsNonPublic) {
-               _publicOnly = !aIsNonPublic;
+               publicOnly = !aIsNonPublic;
                return this; 
        }
 
@@ -92,7 +92,7 @@ public class SetterConfiguration {
         * @return Reference to the current object to allow call chaining.
         */
        public SetterConfiguration clear() {
-               _setters.clear();
+               setters.clear();
                return this;
        }
 
@@ -104,9 +104,9 @@ public class SetterConfiguration {
         * @return Reference to the current object to allow call chaining.
         */
        public SetterConfiguration remove(String aName) {
-               for (Method method : _setters.keySet()) {
+               for (Method method : setters.keySet()) {
                        if (method.getName().equals(aName)) {
-                               _setters.remove(method);
+                               setters.remove(method);
                                return this;
                        }
                }
@@ -123,9 +123,9 @@ public class SetterConfiguration {
            if ( !aMethod.getDeclaringClass().isAssignableFrom(_class) ) { 
                throw new RuntimeException("Method " + aMethod + " not found in class " + _class + " or its superclasses");
            }
-           for (Method method : _setters.keySet()) {
+           for (Method method : setters.keySet()) {
             if (method.equals(aMethod)) {
-                _setters.remove(method);
+                setters.remove(method);
                 return this;
             }
         }
@@ -133,20 +133,6 @@ public class SetterConfiguration {
                 "Method '" + aMethod + "' was not configured. ");
        }
        
-       
-
-       /**
-        * Creates the name of a setter based on the name of the setter without the
-        * "set" prefix.
-        * 
-        * @param aName
-        *            Setter name.
-        * @return Setter name.
-        */
-       private String createxSetterName(String aName) {
-               return "set" + aName.substring(0, 1).toUpperCase() + aName.substring(1);
-       }
-
        /**
         * Adds a given setter name to the setters.
         * 
@@ -154,9 +140,9 @@ public class SetterConfiguration {
         * @return Reference to the current object to allow call chaining.
         */
        public SetterConfiguration add(final String aName) {
-               int oldlen = _setters.size();
+               int oldlen = setters.size();
                List<Method> methods = new ArrayList<Method>();
-               CollectionFilter.filter(getAllSetters(_class, _publicOnly), methods,
+               CollectionFilter.filter(getAllSetters(_class, publicOnly), methods,
                                new Condition<Method>() {
                                        @Override
                                        public boolean matches(Method aObject) {
@@ -170,7 +156,7 @@ public class SetterConfiguration {
                }
                // TODO is it possible to get more than one setter here in case the subclass overrides
                // the baseclass method? 
-               _setters.put(methods.get(0), createParameterValues(methods.get(0)));
+               setters.put(methods.get(0), createParameterValues(methods.get(0)));
                return this;
        }
 
@@ -185,9 +171,9 @@ public class SetterConfiguration {
         * @throws IllegalArgumentException
         *             In case no setter is found or multiple setters are found.
         */
-       public SetterConfiguration add(final Class aType) {
+       public SetterConfiguration addSetter(final Class aType) {
                List<Method> result = new ArrayList<Method>();
-               CollectionFilter.filter(getAllSetters(_class, _publicOnly), result,
+               CollectionFilter.filter(getAllSetters(_class, publicOnly), result,
                                new Condition<Method>() {
                                        @Override
                                        public boolean matches(Method aObject) {
@@ -213,7 +199,7 @@ public class SetterConfiguration {
                                                        + setters);
                }
                Method method = result.get(0);
-               _setters.put(method, createParameterValues(method));
+               setters.put(method, createParameterValues(method));
                return this;
        }
 
@@ -237,11 +223,14 @@ public class SetterConfiguration {
                return result;
        }
 
-       private static ParameterValues createParameterValues(Method method) {
-           // TODO generalize to multiple parameters. 
-               return new ParameterValues(
-                               new String[] { method.getName() }, new Class[] { method
-                                               .getParameterTypes()[0] });
+       private static ParameterValues createParameterValues(Method aMethod) {
+           
+           Class[] paramTypes = aMethod.getParameterTypes();
+           String[] paramNames = new String[paramTypes.length];
+           for (int i = 0; i < paramTypes.length; i++) { 
+               paramNames[i] = aMethod.getName() + "." + i; 
+           } 
+               return new ParameterValues(paramNames, paramTypes);
        }
 
        private static final List<Method> getAllMethods(Class aClass) {
@@ -255,8 +244,8 @@ public class SetterConfiguration {
         */
        public List<RequiredInterface> getRequiredInterfaces() {
                List<RequiredInterface> result = new ArrayList<RequiredInterface>();
-               for (Method method : _setters.keySet()) {
-                       result.addAll(_setters.get(method).getRequiredInterfaces());
+               for (Method method : setters.keySet()) {
+                       result.addAll(setters.get(method).getRequiredInterfaces());
                }
                return result;
        }
@@ -274,8 +263,8 @@ public class SetterConfiguration {
                        throw new IllegalArgumentException("Object '" + aObject
                                        + "' is not an instance of " + _class.getName());
                }
-               for (Method method : _setters.keySet()) {
-                       ParameterValues values = _setters.get(method);
+               for (Method method : setters.keySet()) {
+                       ParameterValues values = setters.get(method);
 
                        try {
                                method.invoke(aObject, values.values(aScope));
@@ -299,30 +288,16 @@ public class SetterConfiguration {
         * @return Parameter values.
         */
        public ParameterValues values(String aMethod) {
-               for (Method method : _setters.keySet()) {
+               for (Method method : setters.keySet()) {
                        if (method.getName().equals(aMethod)) {
-                               return _setters.get(method);
+                               return setters.get(method);
                        }
                }
                throw new IllegalArgumentException("No setter method '" + aMethod
                                + "' found");
        }
 
-       /**
-        * Gets the setter name for a given setter method. This is the name of the
-        * setter without the "set" prefix and with the first character converted to
-        * lowercase.
-        * 
-        * @param aMethod
-        *            Method.
-        * @return Setter name.
-        */
-       private static String getxSetterName(Method aMethod) {
-               String result = aMethod.getName().substring(3);
-               return result.substring(0, 1).toLowerCase() + result.substring(1);
-       }
-
        public List<Method> getSetters() { 
-               return new ArrayList<Method>(_setters.keySet());
+               return new ArrayList<Method>(setters.keySet());
        }
 }