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

com.palantir.conjure.java.lib.internal.ConjureCollections Maven / Gradle / Ivy

/*
 * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
 *
 * 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.
 */

package com.palantir.conjure.java.lib.internal;

import com.palantir.conjure.java.lib.SafeLong;
import com.palantir.logsafe.Preconditions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.collections.impl.list.mutable.primitive.DoubleArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.LongArrayList;

/**
 * Utility functions for conjure. Consumers should prefer to use something like guava instead of using these functions
 * directly.
 */
public final class ConjureCollections {

    private ConjureCollections() {
        // cannot instantiate
    }

    public static  List unmodifiableList(List list) {
        return Collections.unmodifiableList(list);
    }

    @SuppressWarnings("unchecked")
    public static  void addAll(Collection addTo, Iterable elementsToAdd) {
        Preconditions.checkNotNull(elementsToAdd, "elementsToAdd cannot be null");
        if (elementsToAdd instanceof Collection) {
            // This special-casing allows us to take advantage of the more performant
            // ArrayList#addAll method which does a single System.arraycopy.
            addTo.addAll((Collection) elementsToAdd);
        } else {
            for (T element : elementsToAdd) {
                addTo.add(element);
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static  void addAllAndCheckNonNull(Collection addTo, Iterable elementsToAdd) {
        Preconditions.checkNotNull(elementsToAdd, "elementsToAdd cannot be null");
        // If we know the number of elements we are adding and the addTo Collection is an ArrayList, we can eagerly
        // resize it to only do one grow() of the array.
        if (elementsToAdd instanceof Collection) {
            Collection collectionElementsToAdd = (Collection) elementsToAdd;
            if (addTo instanceof ArrayList) {
                ((ArrayList) addTo).ensureCapacity(collectionElementsToAdd.size() + addTo.size());
            }
        }
        for (T element : elementsToAdd) {
            Preconditions.checkNotNull(element, "elementsToAdd cannot contain null elements");
            addTo.add(element);
        }
    }

    // Prefer to use newList(iterable)
    // explicitly need to return mutable list for generated builders
    @SuppressWarnings({"IllegalType", "unchecked", "NonApiType"})
    public static  ArrayList newArrayList(Iterable iterable) {
        Preconditions.checkNotNull(iterable, "iterable cannot be null");
        if (iterable instanceof Collection) {
            return new ArrayList<>((Collection) iterable);
        }
        ArrayList list = new ArrayList<>();
        for (T item : iterable) {
            list.add(item);
        }
        return list;
    }

    // Prefer to use newSet(iterable)
    @SuppressWarnings({"IllegalType", "NonApiType"}) // explicitly need to return mutable list for generated builders
    public static  LinkedHashSet newLinkedHashSet(Iterable iterable) {
        Preconditions.checkNotNull(iterable, "iterable cannot be null");
        if (iterable instanceof Collection) {
            return new LinkedHashSet<>((Collection) iterable);
        }
        LinkedHashSet set = new LinkedHashSet<>();
        for (T item : iterable) {
            set.add(item);
        }
        return set;
    }

    public static  List newList() {
        return new ArrayList<>();
    }

    public static  List newList(Iterable iterable) {
        return newArrayList(iterable);
    }

    public static  List newNonNullList() {
        return new ArrayList<>();
    }

    public static  List newNonNullList(Iterable iterable) {
        List arrayList = newList(iterable);
        for (T item : arrayList) {
            Preconditions.checkNotNull(item, "iterable cannot contain null elements");
        }

        return arrayList;
    }

    public static  Set newSet() {
        return new LinkedHashSet<>();
    }

    public static  Set newSet(Iterable iterable) {
        return newLinkedHashSet(iterable);
    }

    public static  Set newNonNullSet() {
        return new LinkedHashSet<>();
    }

    public static  Set newNonNullSet(Iterable iterable) {
        Set set = newSet(iterable);
        for (T item : set) {
            Preconditions.checkNotNull(item, "iterable cannot contain null elements");
        }

        return set;
    }

    /**
     * The following Conjure boxed list wrappers for the eclipse-collections [type]ArrayList are temporary (except
     * ConjureSafeLongList). In eclipse-collections 12, a BoxedMutable[type]List will be released. Once available,
     * Conjure[type]List should be replaced with that.
     */

    // This method returns a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static List newNonNullDoubleList() {
        return new ConjureDoubleList(new DoubleArrayList());
    }

    // This method returns a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static List newNonNullDoubleList(Iterable iterable) {
        List doubleList;
        if (iterable instanceof Collection) {
            doubleList = new ConjureDoubleList(new DoubleArrayList(((Collection) iterable).size()));
        } else {
            doubleList = new ConjureDoubleList(new DoubleArrayList());
        }
        addAll(doubleList, iterable);

        return doubleList;
    }

    // This method modifies a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static void addAllToDoubleList(Collection addTo, double[] elementsToAdd) {
        for (double el : elementsToAdd) {
            addTo.add(el);
        }
    }

    // This method returns a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static List newNonNullIntegerList() {
        return new ConjureIntegerList(new IntArrayList());
    }

    // This method returns a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static List newNonNullIntegerList(Iterable iterable) {
        List integerList;
        if (iterable instanceof Collection) {
            integerList = new ConjureIntegerList(new IntArrayList(((Collection) iterable).size()));
        } else {
            integerList = new ConjureIntegerList(new IntArrayList());
        }
        addAll(integerList, iterable);

        return integerList;
    }

    // This method modifies a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static void addAllToIntegerList(Collection addTo, int[] elementsToAdd) {
        for (int el : elementsToAdd) {
            addTo.add(el);
        }
    }

    /**
     * Deprecated, this should only ever be called by a previously generated conjure internal implementation.
     */
    // This method returns a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static List newNonNullBooleanList() {
        return newNonNullList();
    }

    /**
     * Deprecated, this should only ever be called by a previously generated conjure internal implementation.
     */
    public static List newNonNullBooleanList(Iterable iterable) {
        return newNonNullList(iterable);
    }

    // This method returns a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static List newNonNullSafeLongList() {
        return new ConjureSafeLongList(new LongArrayList());
    }

    // This method returns a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static List newNonNullSafeLongList(Iterable iterable) {
        List safeLongList;
        if (iterable instanceof Collection) {
            safeLongList = new ConjureSafeLongList(new LongArrayList(((Collection) iterable).size()));
        } else {
            safeLongList = new ConjureSafeLongList(new LongArrayList());
        }
        addAll(safeLongList, iterable);

        return safeLongList;
    }

    // This method modifies a list that can't handle nulls. Do not use this unless the nonNullCollections flag is set
    public static void addAllToSafeLongList(Collection addTo, long[] elementsToAdd) {
        for (long el : elementsToAdd) {
            addTo.add(SafeLong.of(el));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy