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

org.jfaster.badger.util.Lists Maven / Gradle / Ivy

The newest version!
package org.jfaster.badger.util;

import static org.jfaster.badger.util.CheckConditions.checkNotNull;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.AbstractSequentialList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
import java.util.function.Function;

/**
 * list 工具类
 * @author yanpengfang
 * create 2019-01-29 11:40 AM
 */
public class Lists {

    public static  List transform(List fromList, Function function) {
        return (fromList instanceof RandomAccess)
                ? new TransformingRandomAccessList(fromList, function)
                : new TransformingSequentialList(fromList, function);
    }

    private static class TransformingRandomAccessList
            extends AbstractList implements RandomAccess, Serializable {
        final List fromList;
        final Function function;

        TransformingRandomAccessList(
                List fromList, Function function) {
            this.fromList = checkNotNull(fromList);
            this.function = checkNotNull(function);
        }

        @Override
        public void clear() {
            fromList.clear();
        }

        @Override
        public T get(int index) {
            return function.apply(fromList.get(index));
        }

        @Override
        public Iterator iterator() {
            return listIterator();
        }

        @Override
        public ListIterator listIterator(int index) {
            return new TransformedListIterator(fromList.listIterator(index)) {
                @Override
                T transform(F from) {
                    return function.apply(from);
                }
            };
        }

        @Override
        public boolean isEmpty() {
            return fromList.isEmpty();
        }

        @Override
        public T remove(int index) {
            return function.apply(fromList.remove(index));
        }

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

        private static final long serialVersionUID = 0;
    }

    private static class TransformingSequentialList
            extends AbstractSequentialList implements Serializable {
        final List fromList;
        final Function function;

        TransformingSequentialList(
                List fromList, Function function) {
            this.fromList = checkNotNull(fromList);
            this.function = checkNotNull(function);
        }

        @Override
        public void clear() {
            fromList.clear();
        }

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

        @Override
        public ListIterator listIterator(final int index) {
            return new TransformedListIterator(fromList.listIterator(index)) {
                @Override
                T transform(F from) {
                    return function.apply(from);
                }
            };
        }

        private static final long serialVersionUID = 0;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy