X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fadapters%2FSetterConfiguration.java;fp=system%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fadapters%2FSetterConfiguration.java;h=83992d388d5f325d55ee75e33ee3ee1f9e8cbb75;hb=1eaba9434d63bf7109b0651e6d26d246962c959a;hp=0000000000000000000000000000000000000000;hpb=183281472cb842ed1be98c1590fc7cdef4f088db;p=utils diff --git a/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java b/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java new file mode 100644 index 00000000..83992d38 --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java @@ -0,0 +1,211 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.adapters; + +import java.awt.CompositeContext; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import org.wamblee.collections.CollectionFilter; +import org.wamblee.conditions.Condition; +import org.wamblee.conditions.FixedCondition; +import org.wamblee.general.Pair; +import org.wamblee.system.core.DefaultProvidedInterface; +import org.wamblee.system.core.DefaultRequiredInterface; +import org.wamblee.system.core.ProvidedInterface; +import org.wamblee.system.core.RequiredInterface; +import org.wamblee.system.core.Scope; +import org.wamblee.system.core.SystemAssemblyException; + +/** + * Represents the configuration for exposing the setters of a class as required + * interfaces. + * + * @author Erik Brakkee + */ +public class SetterConfiguration { + + private Class _class; + + private List> _setters; + + /** + * Constructs the setter configuration. By default all setters are added. + * + * @param aClass + * Class which is being configured. + */ + public SetterConfiguration(Class aClass) { + _class = aClass; + _setters = getAllSetters(); + } + + /** + * Removes all setters. + * + * @return Reference to the current object to allow call chaining. + */ + public SetterConfiguration clear() { + _setters.clear(); + return this; + } + + /** + * Removes a setter from the set of methods. + * + * @param aName + * Name of the setter to remove (without the "set" prefix). + * @return Reference to the current object to allow call chaining. + */ + public SetterConfiguration remove(String aName) { + final String name = createSetterName(aName); + List> setters = + new ArrayList>(); + CollectionFilter.filter(_setters, setters, new Condition>() { + @Override + public boolean matches(Pair aObject) { + return !aObject.getFirst().getName().equals(name); + } + + }); + if ( _setters.size() == setters.size()) { + throw new IllegalArgumentException("No setter configured by the name of '" + aName + "'"); + } + _setters = setters; + return this; + } + + /** + * 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 createSetterName(String aName) { + return "set" + aName.substring(0, 1).toUpperCase() + + aName.substring(1); + } + + /** + * Adds a given setter name to the setters. + * @param aName + * @return Reference to the current object to allow call chaining. + */ + public SetterConfiguration add(String aName) { + final String name = createSetterName(aName); + int oldlen = _setters.size(); + CollectionFilter.filter(getAllSetters(), _setters, + new Condition>() { + @Override + public boolean matches(Pair aObject) { + return aObject.getFirst().getName().equals(name); + } + + }); + if ( _setters.size() == oldlen) { + throw new IllegalArgumentException("No setter found for '" + aName + "'"); + } + return this; + } + + /** + * Gets all setters for the current class. + * @return List of all setters. + */ + private List> getAllSetters() { + List> result = + new ArrayList>(); + for (Method method : _class.getMethods()) { + if (method.getName().startsWith("set") + && method.getParameterTypes().length == 1) { + String name = getSetterName(method); + result.add(new Pair(method, + new ParameterValues( + new String[] { name }, new Class[] { method.getParameterTypes()[0] }))); + } + } + return result; + } + + /** + * Gets the required interfaces based on the configured setteres. + * @return List of required interfaces. + */ + public List getRequiredInterfaces() { + List result = new ArrayList(); + for (Pair method: _setters) { + result.addAll(method.getSecond().getRequiredInterfaces()); + } + return result; + } + + + /** + * Invokes all configured setters with the appropriate values. + * @param aScope Scope within which invocation takes place. + * @param aObject Object on which the invocation takes place. + */ + public void invoke(Scope aScope, Object aObject) { + if ( !_class.isInstance(aObject)) { + throw new IllegalArgumentException("Object '" + aObject + "' is not an instance of " + + _class.getName()); + } + for (Pair setter: _setters) { + Method method = setter.getFirst(); + ParameterValues values = setter.getSecond(); + + try { + method.invoke(aObject, values.values(aScope)); + } catch (IllegalAccessException e) { + throw new SystemAssemblyException("Problem invoking " + method + " with " + values, e); + } catch (InvocationTargetException e) { + throw new SystemAssemblyException("Problem invoking " + method + " with " + values, e); + } + } + } + + /** + * Returns the parameter values for allowing detailed configuration of how + * parameter values are set. + * @param aSetter Setter name without the "set" prefix with the first + * character converted to lower case. + * @return Parameter values. + */ + public ParameterValues values(String aMethod) { + String name = createSetterName(aMethod); + for (Pair method: _setters) { + if ( method.getFirst().getName().equals(name) ) { + return method.getSecond(); + } + } + throw new IllegalArgumentException("No setter method '" + name + "' 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 String getSetterName(Method aMethod) { + String result = aMethod.getName().substring(3); + return result.substring(0,1).toLowerCase() + result.substring(1); + } + +}