org.nomin.util.ArrayHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nomin Show documentation
Show all versions of nomin Show documentation
Nomin is a mapping engine for the Java platform. It provides abilities to transform object trees according to
declarative mapping rules. Main features of Nomin are no XML configuration, intuitively looking mapping,
using arbitrary expressions and method invocations in mappings, pre and postprocessing right in a mapping listing,
customizations. It's applicable for any Java compatible classes, not only JavaBeans.
package org.nomin.util;
import java.util.*;
import org.nomin.core.preprocessing.Preprocessing;
import static org.nomin.core.preprocessing.Preprocessing.preprocess;
/**
* Assists to manipulate with arrays.
* @author Dmitry Dobrynin
* Date: 25.10.2010 Time: 21:54:18
*/
public class ArrayHelper extends ContainerHelper {
public ArrayHelper(TypeInfo typeInfo) {
super(typeInfo.getParameter(0).type, typeInfo.getParameter(0));
}
public Object[] createContainer(int size) throws Exception {
return (Object[]) java.lang.reflect.Array.newInstance(containerClass, size);
}
public Object[] convert(Collection> source, Preprocessing[] preprocessings) throws Exception {
Object[] target = createContainer(source.size());
int index = 0;
for (Object object : source) target[index++] = preprocess(object, preprocessings, 0);
return target;
}
public Object setElement(Object target, Object index, Object element, Preprocessing[] preprocessing) throws Exception {
Integer i = (Integer) index;
Object[] targetArray = (Object[]) target;
if (targetArray != null && (i < 0 || i >= targetArray.length))
targetArray = Arrays.copyOf(targetArray, i < 0 ? targetArray.length + 1 : i + 1);
else
targetArray = createContainer(i < 0 ? 1 : i + 1);
targetArray[i < 0 ? targetArray.length - 1 : i] = preprocess(element, preprocessing, 0);
return targetArray;
}
public Object getElement(Object source, Object index) {
Object[] array = (Object[]) source;
return array.length > (Integer) index ? array[(Integer) index] : null;
}
}