All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openprovenance.prov.template.compiler.sql.Collections Maven / Gradle / Ivy

The newest version!
package org.openprovenance.prov.template.compiler.sql;

/*
 * 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.
 */

import java.util.*;
import java.util.function.Predicate;

/**
 * Provides static utility methods for working with collections.
 */
public class Collections {
    private Collections() {
    }

    /**
     * Creates an immutable list of elements.
     *
     * @param 
     * The element type.
     *
     * @param elements
     * The list elements.
     *
     * @return
     * An immutable list containing the provided elements.
     */
    @SafeVarargs
    public static  List listOf(E... elements) {
        if (elements == null) {
            throw new IllegalArgumentException();
        }

        return Arrays.asList(elements);//java.util.Collections.unmodifiableList(Arrays.asList(elements));
    }

    /**
     * Creates an immutable map of entries.
     *
     * @param 
     * The key type.
     *
     * @param 
     * The value type.
     *
     * @param entries
     * The map entries.
     *
     * @return
     * An immutable map containing the provided entries.
     */
    @SafeVarargs
    public static  Map mapOf(Map.Entry... entries) {
        if (entries == null) {
            throw new IllegalArgumentException();
        }

        Map map = new LinkedHashMap<>();

        for (Map.Entry entry : entries) {
            map.put(entry.getKey(), entry.getValue());
        }

        return map;//java.util.Collections.unmodifiableMap(map);
    }



    /**
     * Creates an immutable map entry.
     *
     * @param 
     * The key type.
     *
     * @param 
     * The value type.
     *
     * @param key
     * The entry key.
     *
     * @param value
     * The entry value.
     *
     * @return
     * An immutable map entry containing the provided key/value pair.
     */
    public static  Map.Entry entry(K key, V value) {
        return new AbstractMap.SimpleImmutableEntry<>(key, value);
    }

    /**
     * Returns an empty list.
     *
     * @param 
     * The element type.
     *
     * @param elementType
     * The element type.
     *
     * @return
     * An empty list.
     */
    public static  List emptyListOf(Class elementType) {
        if (elementType == null) {
            throw new IllegalArgumentException();
        }

        return listOf();
    }

    /**
     * Returns an empty map.
     *
     * @param 
     * The key type.
     *
     * @param 
     * The value type.
     *
     * @param keyType
     * The key type.
     *
     * @param valueType
     * The value type.
     *
     * @return
     * An empty map.
     */
    public static  Map emptyMapOf(Class keyType, Class valueType) {
        if (keyType == null || valueType == null) {
            throw new IllegalArgumentException();
        }

        return mapOf();
    }

    /**
     * Returns the index of the first element in a list that matches the given
     * predicate.
     *
     * @param 
     * The element type.
     *
     * @param list
     * The list of elements.
     *
     * @param predicate
     * The predicate.
     *
     * @return
     * The index of the first matching element, or {@code -1} if no match was
     * found.
     */
    public static  int firstIndexWhere(List list, Predicate predicate) {
        Iterator iterator = list.iterator();

        int i = 0;

        while (iterator.hasNext()) {
            if (predicate.test(iterator.next())) {
                return i;
            }

            i++;
        }

        return -1;
    }

    /**
     * Returns the index of the last element in a list that matches the given
     * predicate.
     *
     * @param 
     * The element type.
     *
     * @param list
     * The list of elements.
     *
     * @param predicate
     * The predicate.
     *
     * @return
     * The index of the last matching element, or {@code -1} if no match was
     * found.
     */
    public static  int lastIndexWhere(List list, Predicate predicate) {
        int i = list.size();

        ListIterator iterator = list.listIterator(i);

        while (iterator.hasPrevious()) {
            i--;

            if (predicate.test(iterator.previous())) {
                return i;
            }
        }

        return -1;
    }

    /**
     * Returns the value at a given path.
     *
     * @param root
     * The root object.
     *
     * @param path
     * The path to the value.
     *
     * @return
     * The value at the given path, or {@code null} if the value does not
     * exist.
     */
    public static Object valueAt(Object root, Object... path) {
        return valueAt(root, new LinkedList<>(Arrays.asList(path)));
    }

    private static Object valueAt(Object root, List path) {
        if (root == null) {
            return null;
        } else if (path.isEmpty()) {
            return root;
        } else {
            Object component = path.remove(0);

            Object value;
            if (root instanceof List && component instanceof Number) {
                value = ((List)root).get(((Number)component).intValue());
            } else if (root instanceof Map) {
                value = ((Map)root).get(component);
            } else {
                throw new IllegalArgumentException();
            }

            return valueAt(value, path);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy