/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.apache.commons.collections;
/** * Defines a functor interface implemented by classes that transform one * object into another. * <p> * A <code>Transformer</code> converts the input object to the output object. * The input object should be left unchanged. * Transformers are typically used for type conversions, or extracting data * from an object. * <p> * Standard implementations of common transformers are provided by * {@link TransformerUtils}. These include method invokation, returning a constant, * cloning and returning the string value. * * @since Commons Collections 1.0 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ * * @author James Strachan * @author Stephen Colebourne */ 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);
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.apache.commons.collections.functors;
/** * Transformer implementation that returns the same constant each time. * <p> * No check is made that the object is immutable. In general, only immutable * objects should use the constant factory. Mutable objects should * use the prototype factory. * * @since Commons Collections 3.0 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ * * @author Stephen Colebourne */ publicclassConstantTransformerimplementsTransformer, Serializable {
/** Serial version UID */ privatestaticfinallongserialVersionUID=6374440726369055124L;
/** Returns null each time */ publicstaticfinalTransformerNULL_INSTANCE=newConstantTransformer(null);
/** The closures to call in turn */ privatefinal Object iConstant;
/** * Transformer method that performs validation. * * @param constantToReturn the constant object to return each time in the factory * @return the <code>constant</code> factory. */ publicstatic Transformer getInstance(Object constantToReturn) { if (constantToReturn == null) { return NULL_INSTANCE; } returnnewConstantTransformer(constantToReturn); }
/** * 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; }
/** * Gets the constant. * * @return the constant * @since Commons Collections 3.1 */ public Object getConstant() { return iConstant; }
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.apache.commons.collections.functors;
/** * Transformer implementation that creates a new object instance by reflection. * * @since Commons Collections 3.0 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ * * @author Stephen Colebourne */ publicclassInvokerTransformerimplementsTransformer, Serializable {
/** The serial version */ privatestaticfinallongserialVersionUID= -8653385846894047688L;
/** The method name to call */ privatefinal String iMethodName; /** The array of reflection parameter types */ privatefinal Class[] iParamTypes; /** The array of reflection arguments */ privatefinal Object[] iArgs;
/** * Gets an instance of this transformer calling a specific method with no arguments. * * @param methodName the method name to call * @return an invoker transformer * @since Commons Collections 3.1 */ publicstatic Transformer getInstance(String methodName) { if (methodName == null) { thrownewIllegalArgumentException("The method to invoke must not be null"); } returnnewInvokerTransformer(methodName); }
/** * Gets an instance of this transformer calling a specific method with specific values. * * @param methodName the method name to call * @param paramTypes the parameter types of the method * @param args the arguments to pass to the method * @return an invoker transformer */ publicstatic Transformer getInstance(String methodName, Class[] paramTypes, Object[] args) { if (methodName == null) { thrownewIllegalArgumentException("The method to invoke must not be null"); } if (((paramTypes == null) && (args != null)) || ((paramTypes != null) && (args == null)) || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) { thrownewIllegalArgumentException("The parameter types must match the arguments"); } if (paramTypes == null || paramTypes.length == 0) { returnnewInvokerTransformer(methodName); } else { paramTypes = (Class[]) paramTypes.clone(); args = (Object[]) args.clone(); returnnewInvokerTransformer(methodName, paramTypes, args); } }
/** * Constructor for no arg instance. * * @param methodName the method to call */ privateInvokerTransformer(String methodName) { super(); iMethodName = methodName; iParamTypes = null; iArgs = null; }
/** * Constructor that performs no validation. * Use <code>getInstance</code> if you want that. * * @param methodName the method to call * @param paramTypes the constructor parameter types, not cloned * @param args the constructor arguments, not cloned */ publicInvokerTransformer(String methodName, Class[] paramTypes, Object[] args) { super(); iMethodName = methodName; iParamTypes = paramTypes; iArgs = args; }
/** * Transforms the input to result by invoking a method on the input. * * @param input the input object to transform * @return the transformed result, null if null input */ public Object transform(Object input) { if (input == null) { returnnull; } try { Classcls= input.getClass(); Methodmethod= cls.getMethod(iMethodName, iParamTypes); return method.invoke(input, iArgs);
} catch (NoSuchMethodException ex) { thrownewFunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' does not exist"); } catch (IllegalAccessException ex) { thrownewFunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' cannot be accessed"); } catch (InvocationTargetException ex) { thrownewFunctorException("InvokerTransformer: The method '" + iMethodName + "' on '" + input.getClass() + "' threw an exception", ex); } }
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.apache.commons.collections.functors;
/** * Transformer implementation that chains the specified transformers together. * <p> * The input object is passed to the first transformer. The transformed result * is passed to the second transformer and so on. * * @since Commons Collections 3.0 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ * * @author Stephen Colebourne */ publicclassChainedTransformerimplementsTransformer, Serializable {
/** Serial version UID */ privatestaticfinallongserialVersionUID=3514945074733160196L;
/** The transformers to call in turn */ privatefinal Transformer[] iTransformers;
/** * Factory method that performs validation and copies the parameter array. * * @param transformers the transformers to chain, copied, no nulls * @return the <code>chained</code> transformer * @throws IllegalArgumentException if the transformers array is null * @throws IllegalArgumentException if any transformer in the array is null */ publicstatic Transformer getInstance(Transformer[] transformers) { FunctorUtils.validate(transformers); if (transformers.length == 0) { return NOPTransformer.INSTANCE; } transformers = FunctorUtils.copy(transformers); returnnewChainedTransformer(transformers); }
/** * Create a new Transformer that calls each transformer in turn, passing the * result into the next transformer. The ordering is that of the iterator() * method on the collection. * * @param transformers a collection of transformers to chain * @return the <code>chained</code> transformer * @throws IllegalArgumentException if the transformers collection is null * @throws IllegalArgumentException if any transformer in the collection is null */ publicstatic Transformer getInstance(Collection transformers) { if (transformers == null) { thrownewIllegalArgumentException("Transformer collection must not be null"); } if (transformers.size() == 0) { return NOPTransformer.INSTANCE; } // convert to array like this to guarantee iterator() ordering Transformer[] cmds = newTransformer[transformers.size()]; inti=0; for (Iteratorit= transformers.iterator(); it.hasNext();) { cmds[i++] = (Transformer) it.next(); } FunctorUtils.validate(cmds); returnnewChainedTransformer(cmds); }
/** * Factory method that performs validation. * * @param transformer1 the first transformer, not null * @param transformer2 the second transformer, not null * @return the <code>chained</code> transformer * @throws IllegalArgumentException if either transformer is null */ publicstatic Transformer getInstance(Transformer transformer1, Transformer transformer2) { if (transformer1 == null || transformer2 == null) { thrownewIllegalArgumentException("Transformers must not be null"); } Transformer[] transformers = newTransformer[] { transformer1, transformer2 }; returnnewChainedTransformer(transformers); }
/** * 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 (inti=0; i < iTransformers.length; i++) { object = iTransformers[i].transform(object); } return object; }
/** * Gets the transformers, do not modify the array. * @return the transformers * @since Commons Collections 3.1 */ public Transformer[] getTransformers() { return iTransformers; }
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.apache.commons.collections.map;
/** * Decorates another <code>Map</code> to transform objects that are added. * <p> * The Map put methods and Map.Entry setValue method are affected by this class. * Thus objects must be removed or searched for using their transformed form. * For example, if the transformation converts Strings to Integers, you must * use the Integer form to remove objects. * <p> * <strong>Note that TransformedMap is not synchronized and is not thread-safe.</strong> * If you wish to use this map from multiple threads concurrently, you must use * appropriate synchronization. The simplest approach is to wrap this map * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw * exceptions when accessed by concurrent threads without synchronization. * <p> * This class is Serializable from Commons Collections 3.1. * * @since Commons Collections 3.0 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ * * @author Stephen Colebourne */ publicclassTransformedMap extendsAbstractInputCheckedMapDecorator implementsSerializable {
/** Serialization version */ privatestaticfinallongserialVersionUID=7023152376788900464L;
/** The transformer to use for the key */ protectedfinal Transformer keyTransformer; /** The transformer to use for the value */ protectedfinal Transformer valueTransformer;
/** * Factory method to create a transforming map. * <p> * If there are any elements already in the map being decorated, they * are NOT transformed. * Constrast this with {@link #decorateTransform}. * * @param map the map to decorate, must not be null * @param keyTransformer the transformer to use for key conversion, null means no transformation * @param valueTransformer the transformer to use for value conversion, null means no transformation * @throws IllegalArgumentException if map is null */ publicstatic Map decorate(Map map, Transformer keyTransformer, Transformer valueTransformer) { returnnewTransformedMap(map, keyTransformer, valueTransformer); }
/** * Factory method to create a transforming map that will transform * existing contents of the specified map. * <p> * If there are any elements already in the map being decorated, they * will be transformed by this method. * Constrast this with {@link #decorate}. * * @param map the map to decorate, must not be null * @param keyTransformer the transformer to use for key conversion, null means no transformation * @param valueTransformer the transformer to use for value conversion, null means no transformation * @throws IllegalArgumentException if map is null * @since Commons Collections 3.2 */ publicstatic Map decorateTransform(Map map, Transformer keyTransformer, Transformer valueTransformer) { TransformedMapdecorated=newTransformedMap(map, keyTransformer, valueTransformer); if (map.size() > 0) { Maptransformed= decorated.transformMap(map); decorated.clear(); decorated.getMap().putAll(transformed); // avoids double transformation } return decorated; }
//----------------------------------------------------------------------- /** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection 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 */ protectedTransformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer) { super(map); this.keyTransformer = keyTransformer; this.valueTransformer = valueTransformer; }
//----------------------------------------------------------------------- /** * Write the map out using a custom routine. * * @param out the output stream * @throws IOException * @since Commons Collections 3.1 */ privatevoidwriteObject(ObjectOutputStream out)throws IOException { out.defaultWriteObject(); out.writeObject(map); }
/** * Read the map in using a custom routine. * * @param in the input stream * @throws IOException * @throws ClassNotFoundException * @since Commons Collections 3.1 */ privatevoidreadObject(ObjectInputStream in)throws IOException, ClassNotFoundException { in.defaultReadObject(); map = (Map) in.readObject(); }
//----------------------------------------------------------------------- /** * Transforms a key. * <p> * The transformer itself may throw an exception if necessary. * * @param object the object to transform * @throws the transformed object */ protected Object transformKey(Object object) { if (keyTransformer == null) { return object; } return keyTransformer.transform(object); }
/** * Transforms a value. * <p> * The transformer itself may throw an exception if necessary. * * @param object the object to transform * @throws the transformed object */ protected Object transformValue(Object object) { if (valueTransformer == null) { return object; } return valueTransformer.transform(object); }
/** * Transforms a map. * <p> * The transformer itself may throw an exception if necessary. * * @param map the map to transform * @throws the transformed object */ protected Map transformMap(Map map) { if (map.isEmpty()) { return map; } Mapresult=newLinkedMap(map.size()); for (Iteratorit= map.entrySet().iterator(); it.hasNext(); ) { Map.Entryentry= (Map.Entry) it.next(); result.put(transformKey(entry.getKey()), transformValue(entry.getValue())); } return result; }
/** * Override to transform the value when using <code>setValue</code>. * * @param value the value to transform * @return the transformed value * @since Commons Collections 3.1 */ protected Object checkSetValue(Object value) { return valueTransformer.transform(value); }
/** * Override to only return true when there is a value transformer. * * @return true if a value transformer is in use * @since Commons Collections 3.1 */ protectedbooleanisSetValueChecking() { return (valueTransformer != null); }
//----------------------------------------------------------------------- public Object put(Object key, Object value) { key = transformKey(key); value = transformValue(value); return getMap().put(key, value); }