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

uk.autores.handling.Lists Maven / Gradle / Ivy

There is a newer version: 11.0.35-beta
Show newest version
package uk.autores.handling;

import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

/**
 * Utility type.
 */
final class Lists {

    private Lists() {}

    /**
     * @param original list to copy
     * @return unmodifiable copy
     * @param  list content type
     */
    static  List copy(List original) {
        switch (original.size()) {
            case 0:
                return emptyList();
            case 1:
                return singletonList(original.get(0));
            case 2:
                return new Pair<>(original);
            default:
                return new Unmodifiable<>(original);
        }
    }

    private static final class Unmodifiable extends AbstractList implements RandomAccess {
        private final Object[] contents;

        Unmodifiable(List original) {
            contents = original.toArray(new Object[0]);
        }

        @SuppressWarnings("unchecked")
        @Override
        public T get(int index) {
            return (T) contents[index];
        }

        @Override
        public int size() {
            return contents.length;
        }
    }

    private static final class Pair extends AbstractList implements RandomAccess {

        private final T first;
        private final T second;

        Pair(List original) {
            first = original.get(0);
            second = original.get(1);
        }

        @Override
        public T get(int index) {
            if (index == 0) {
                return first;
            }
            if (index == 1) {
                return second;
            }
            throw new IndexOutOfBoundsException(Integer.toString(index));
        }

        @Override
        public int size() {
            return 2;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy