Example usage for java.util List clone

List of usage examples for java.util List clone

Introduction

In this page you can find the example usage for java.util List clone.

Prototype

@HotSpotIntrinsicCandidate
protected native Object clone() throws CloneNotSupportedException;

Source Link

Document

Creates and returns a copy of this object.

Usage

From source file:com.joliciel.talismane.machineLearning.features.AbstractFeatureParser.java

/**
 * Get the features corresponding to a particular descriptor by performing
 * reflection on the corresponding feature class to be instantiated.
 * @param descriptor/*  w  w w . ja  v a  2 s .com*/
 * @param featureClass
 * @return
 */
final List<Feature<T, ?>> getFeatures(FunctionDescriptor descriptor,
        @SuppressWarnings("rawtypes") Class<? extends Feature> featureClass,
        FunctionDescriptor topLevelDescriptor) {
    if (featureClass == null)
        throw new FeatureSyntaxException("No class provided for", descriptor, topLevelDescriptor);

    List<Feature<T, ?>> features = new ArrayList<Feature<T, ?>>();
    int i = 0;
    List<List<Object>> argumentLists = new ArrayList<List<Object>>();
    List<Object> initialArguments = new ArrayList<Object>();
    argumentLists.add(initialArguments);

    for (FunctionDescriptor argumentDescriptor : descriptor.getArguments()) {
        List<List<Object>> newArgumentLists = new ArrayList<List<Object>>();
        for (List<Object> arguments : argumentLists) {
            if (!argumentDescriptor.isFunction()) {
                Object literal = argumentDescriptor.getObject();
                Object convertedObject = literal;
                if (literal instanceof String) {
                    StringLiteralFeature<T> stringLiteralFeature = new StringLiteralFeature<T>(
                            (String) literal);
                    convertedObject = stringLiteralFeature;
                } else if (literal instanceof Boolean) {
                    BooleanLiteralFeature<T> booleanLiteralFeature = new BooleanLiteralFeature<T>(
                            (Boolean) literal);
                    convertedObject = booleanLiteralFeature;
                } else if (literal instanceof Double) {
                    DoubleLiteralFeature<T> doubleLiteralFeature = new DoubleLiteralFeature<T>(
                            (Double) literal);
                    convertedObject = doubleLiteralFeature;
                } else if (literal instanceof Integer) {
                    IntegerLiteralFeature<T> integerLiteralFeature = new IntegerLiteralFeature<T>(
                            (Integer) literal);
                    convertedObject = integerLiteralFeature;
                } else {
                    // do nothing - this was some sort of other object added by getModifiedDescriptors that should
                    // be handled as is.
                }
                arguments.add(convertedObject);
                newArgumentLists.add(arguments);

            } else {
                List<Feature<T, ?>> featureArguments = this.parseInternal(argumentDescriptor,
                        topLevelDescriptor);
                // a single argument descriptor could produce multiple arguments
                // e.g. when a function with an array argument is mapped onto multiple function calls
                for (Feature<T, ?> featureArgument : featureArguments) {
                    List<Object> newArguments = new ArrayList<Object>(arguments);
                    newArguments.add(featureArgument);
                    newArgumentLists.add(newArguments);
                }
            } // function or object?

        } // next argument list (under construction from original arguments)
        argumentLists = newArgumentLists;
    } // next argument

    for (List<Object> originalArgumentList : argumentLists) {
        // add the argument types (i.e. classes)
        // and convert arrays to multiple constructor calls
        List<Object[]> argumentsList = new ArrayList<Object[]>();
        argumentsList.add(new Object[originalArgumentList.size()]);

        Class<?>[] argumentTypes = new Class<?>[originalArgumentList.size()];
        List<Object[]> newArgumentsList = new ArrayList<Object[]>();
        for (i = 0; i < originalArgumentList.size(); i++) {
            Object arg = originalArgumentList.get(i);

            if (arg.getClass().isArray()) {
                // arrays represent multiple constructor calls
                Object[] argArray = (Object[]) arg;
                for (Object oneArg : argArray) {
                    for (Object[] arguments : argumentsList) {
                        Object[] newArguments = arguments.clone();
                        newArguments[i] = oneArg;
                        newArgumentsList.add(newArguments);
                    }
                }
                argumentTypes[i] = arg.getClass().getComponentType();
            } else {
                for (Object[] myArguments : argumentsList) {
                    newArgumentsList.add(myArguments);
                    myArguments[i] = arg;
                }
                argumentTypes[i] = arg.getClass();
            }
            argumentsList = newArgumentsList;
            newArgumentsList = new ArrayList<Object[]>();
        } // next argument

        @SuppressWarnings("rawtypes")
        Constructor<? extends Feature> constructor = null;
        MONITOR.startTask("findContructor");
        try {
            constructor = ConstructorUtils.getMatchingAccessibleConstructor(featureClass, argumentTypes);

            if (constructor == null) {
                Constructor<?>[] constructors = featureClass.getConstructors();

                // check if there's a variable argument constructor
                for (Constructor<?> oneConstructor : constructors) {
                    Class<?>[] parameterTypes = oneConstructor.getParameterTypes();

                    if (parameterTypes.length >= 1 && argumentsList.size() == 1
                            && argumentsList.get(0).length >= parameterTypes.length) {
                        Object[] arguments = argumentsList.get(0);
                        Class<?> parameterType = parameterTypes[parameterTypes.length - 1];
                        if (parameterType.isArray()) {
                            // assume it's a variable-argument constructor
                            // build the argument for this constructor
                            // find a common type for all of the arguments.
                            Object argument = arguments[parameterTypes.length - 1];
                            Class<?> clazz = null;
                            if (argument instanceof StringFeature)
                                clazz = StringFeature.class;
                            else if (argument instanceof BooleanFeature)
                                clazz = BooleanFeature.class;
                            else if (argument instanceof DoubleFeature)
                                clazz = DoubleFeature.class;
                            else if (argument instanceof IntegerFeature)
                                clazz = IntegerFeature.class;
                            else if (argument instanceof StringCollectionFeature)
                                clazz = StringFeature.class;
                            else {
                                // no good, can't make arrays of this type
                                continue;
                            }

                            Object[] argumentArray = (Object[]) Array.newInstance(clazz,
                                    (arguments.length - parameterTypes.length) + 1);
                            int j = 0;
                            for (int k = parameterTypes.length - 1; k < arguments.length; k++) {
                                Object oneArgument = arguments[k];
                                if (oneArgument instanceof StringCollectionFeature) {
                                    @SuppressWarnings("unchecked")
                                    StringCollectionFeature<T> stringCollectionFeature = (StringCollectionFeature<T>) oneArgument;
                                    StringCollectionFeatureProxy<T> proxy = new StringCollectionFeatureProxy<T>(
                                            stringCollectionFeature);
                                    oneArgument = proxy;
                                }
                                if (!clazz.isAssignableFrom(oneArgument.getClass())) {
                                    throw new FeatureSyntaxException(
                                            "Mismatched array types: " + clazz.getSimpleName() + ", "
                                                    + oneArgument.getClass().getSimpleName(),
                                            descriptor, topLevelDescriptor);
                                }
                                argumentArray[j++] = oneArgument;
                            } // next argument

                            Class<?>[] argumentTypesWithArray = new Class<?>[parameterTypes.length];
                            for (int k = 0; k < parameterTypes.length - 1; k++) {
                                Object oneArgument = arguments[k];
                                argumentTypesWithArray[k] = oneArgument.getClass();
                            }
                            argumentTypesWithArray[argumentTypesWithArray.length - 1] = argumentArray
                                    .getClass();
                            constructor = ConstructorUtils.getMatchingAccessibleConstructor(featureClass,
                                    argumentTypesWithArray);

                            if (constructor != null) {
                                argumentsList = new ArrayList<Object[]>();
                                Object[] argumentsWithArray = new Object[parameterTypes.length];
                                for (int k = 0; k < parameterTypes.length - 1; k++) {
                                    Object oneArgument = arguments[k];
                                    argumentsWithArray[k] = oneArgument;
                                }
                                argumentsWithArray[parameterTypes.length - 1] = argumentArray;
                                argumentsList.add(argumentsWithArray);
                                break;
                            }
                        } // constructor takes an array
                    } // exactly one parameter for constructor
                } // next constructor

                if (constructor == null) {
                    // See if various conversions allow us to find a constructor
                    // Integer to Double
                    // StringCollectionFeature to StringFeature
                    for (Constructor<?> oneConstructor : constructors) {
                        Class<?>[] parameterTypes = oneConstructor.getParameterTypes();
                        boolean isMatchingConstructor = false;
                        List<Integer> intParametersToConvert = new ArrayList<Integer>();
                        List<Integer> stringCollectionParametersToConvert = new ArrayList<Integer>();
                        List<Integer> customParametersToConvert = new ArrayList<Integer>();
                        if (parameterTypes.length == argumentTypes.length) {
                            int j = 0;
                            isMatchingConstructor = true;

                            for (Class<?> parameterType : parameterTypes) {
                                if (parameterType.isAssignableFrom(argumentTypes[j])
                                        && !StringCollectionFeature.class.isAssignableFrom(argumentTypes[j])) {
                                    // nothing to do here
                                } else if (parameterType.equals(DoubleFeature.class)
                                        && IntegerFeature.class.isAssignableFrom(argumentTypes[j])) {
                                    intParametersToConvert.add(j);
                                } else if ((parameterType.equals(StringFeature.class)
                                        || parameterType.equals(Feature.class))
                                        && StringCollectionFeature.class.isAssignableFrom(argumentTypes[j])) {
                                    stringCollectionParametersToConvert.add(j);
                                } else if (this.canConvert(parameterType, argumentTypes[j])) {
                                    customParametersToConvert.add(j);
                                } else {
                                    isMatchingConstructor = false;
                                    break;
                                }
                                j++;
                            }
                        }
                        if (isMatchingConstructor) {
                            @SuppressWarnings({ "rawtypes", "unchecked" })
                            Constructor<? extends Feature> matchingConstructor = (Constructor<? extends Feature>) oneConstructor;
                            constructor = matchingConstructor;

                            for (Object[] myArguments : argumentsList) {
                                for (int indexToConvert : intParametersToConvert) {
                                    @SuppressWarnings("unchecked")
                                    IntegerFeature<T> integerFeature = (IntegerFeature<T>) myArguments[indexToConvert];
                                    IntegerToDoubleFeature<T> intToDoubleFeature = new IntegerToDoubleFeature<T>(
                                            integerFeature);
                                    myArguments[indexToConvert] = intToDoubleFeature;
                                }
                                for (int indexToConvert : stringCollectionParametersToConvert) {
                                    @SuppressWarnings("unchecked")
                                    StringCollectionFeature<T> stringCollectionFeature = (StringCollectionFeature<T>) myArguments[indexToConvert];
                                    StringCollectionFeatureProxy<T> proxy = new StringCollectionFeatureProxy<T>(
                                            stringCollectionFeature);
                                    myArguments[indexToConvert] = proxy;
                                }
                                for (int indexToConvert : customParametersToConvert) {
                                    @SuppressWarnings("unchecked")
                                    Feature<T, ?> argumentToConvert = (Feature<T, ?>) myArguments[indexToConvert];
                                    Feature<T, ?> customArgument = this
                                            .convertArgument(parameterTypes[indexToConvert], argumentToConvert);
                                    myArguments[indexToConvert] = customArgument;
                                    customArgument.addArgument(argumentToConvert);
                                }
                            }
                            break;
                        } // found a matching constructor
                    } // next possible constructor
                } // still haven't found a constructor, what next?
            } // didn't find a constructor yet
        } finally {
            MONITOR.endTask("findContructor");
        }

        if (constructor == null)
            throw new NoConstructorFoundException("No constructor found for " + descriptor.getFunctionName()
                    + " (" + featureClass.getName() + ") matching the arguments provided", descriptor,
                    topLevelDescriptor);

        for (Object[] myArguments : argumentsList) {
            @SuppressWarnings("rawtypes")
            Feature feature;
            try {
                feature = constructor.newInstance(myArguments);
            } catch (IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }

            @SuppressWarnings("unchecked")
            Feature<T, ?> genericFeature = (Feature<T, ?>) feature;
            this.injectDependencies(feature);
            if (genericFeature instanceof ExternalResourceFeature) {
                if (this.getExternalResourceFinder() == null) {
                    throw new JolicielException("No external resource finder set.");
                }
                @SuppressWarnings("unchecked")
                ExternalResourceFeature<T> externalResourceFeature = (ExternalResourceFeature<T>) genericFeature;
                externalResourceFeature.setExternalResourceFinder(this.getExternalResourceFinder());
            } else if (genericFeature instanceof ExternalResourceDoubleFeature) {
                if (this.getExternalResourceFinder() == null) {
                    throw new JolicielException("No external resource finder set.");
                }
                @SuppressWarnings("unchecked")
                ExternalResourceDoubleFeature<T> externalResourceFeature = (ExternalResourceDoubleFeature<T>) genericFeature;
                externalResourceFeature.setExternalResourceFinder(this.getExternalResourceFinder());
            } else if (genericFeature instanceof MultivaluedExternalResourceFeature) {
                if (this.getExternalResourceFinder() == null) {
                    throw new JolicielException("No external resource finder set.");
                }
                @SuppressWarnings("unchecked")
                MultivaluedExternalResourceFeature<T> externalResourceFeature = (MultivaluedExternalResourceFeature<T>) genericFeature;
                externalResourceFeature.setExternalResourceFinder(this.getExternalResourceFinder());
            }

            // add this feature's arguments
            for (Object argument : myArguments) {
                if (argument instanceof Feature[]) {
                    @SuppressWarnings("unchecked")
                    Feature<T, ?>[] featureArray = (Feature<T, ?>[]) argument;
                    for (Feature<T, ?> oneFeature : featureArray) {
                        genericFeature.addArgument(oneFeature);
                    }
                } else {
                    @SuppressWarnings("unchecked")
                    Feature<T, ?> featureArgument = (Feature<T, ?>) argument;
                    genericFeature.addArgument(featureArgument);
                }
            }

            Feature<T, ?> convertedFeature = this.convertFeature(genericFeature);
            features.add(convertedFeature);
        } // next internal argument list
    } // next argument list
    return features;
}