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

functionalj.list.ImmutableList Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// Copyright(c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net)
// ----------------------------------------------------------------------------
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.list;

import static java.util.Collections.unmodifiableList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import functionalj.stream.StreamPlus;
import functionalj.stream.Streamable;
import lombok.val;

@SuppressWarnings("javadoc")
public final class ImmutableList implements FuncList {
    
    private final static ImmutableList EMPTY = new ImmutableList<>(Collections.emptyList());
    
    @SuppressWarnings("unchecked")
    public static final  ImmutableList empty() {
        return (ImmutableList)EMPTY;
    }
    public static  ImmutableList from(Collection data) {
        if (data instanceof ImmutableList)
            return (ImmutableList)data;
        if (data == null)
            return empty();
        
        return new ImmutableList(data);
    }
    @SafeVarargs
    public static  ImmutableList of(T ... data) {
        return new ImmutableList<>(Arrays.asList(data));
    }
    public static  ImmutableList from(T[] datas) {
        return new ImmutableList<>(Arrays.asList(datas));
    }
    public static  ImmutableList from(Streamable streamable) {
        if (streamable instanceof ImmutableList)
            return (ImmutableList)streamable;
        if (streamable == null)
            return ImmutableList.empty();
        
        return new ImmutableList(streamable.toJavaList());
    }
    public static  ImmutableList from(Stream stream) {
        return new ImmutableList(stream.collect(Collectors.toList()));
    }
    public static  ImmutableList from(ReadOnlyList readOnlyList) {
        if (readOnlyList instanceof ImmutableList)
            return (ImmutableList)readOnlyList;
        if (readOnlyList == null)
            return ImmutableList.empty();
        
        return new ImmutableList(readOnlyList.toJavaList());
    }
    public static  ImmutableList from(FuncList funcList) {
        if (funcList instanceof ImmutableList)
            return (ImmutableList)funcList;
        if (funcList == null)
            return ImmutableList.empty();
        
        return new ImmutableList(funcList.toJavaList());
    }
    @SafeVarargs
    public static  ImmutableList listOf(T ... data) {
        return new ImmutableList(Arrays.asList(data));
    }
    
    private final List data;
    private final boolean    isLazy;
    
    public ImmutableList(Collection data) {
        this(data, true);
    }
    public ImmutableList(Collection data, boolean isLazy) {
        this.isLazy = isLazy;
        if (data == null) {
            this.data = Collections.emptyList();
        } else if (data instanceof ImmutableList) {
            this.data = ((ImmutableList)data).data;
        } else {
            val list = new ArrayList();
            data.forEach(list::add);
            this.data = unmodifiableList(list);
        }
    }
    
    @Override
    public StreamPlus stream() {
        return StreamPlus.from(data.stream());
    }
    
    public boolean isLazy() {
        return isLazy;
    }
    
    public boolean isEager() {
        return !isLazy;
    }
    
    public FuncList lazy() {
        if (isLazy)
            return this;
        
        return new ImmutableList(data, true);
    }
    public FuncList eager() {
        if (!isLazy)
            return this;
        
        return new ImmutableList(data, false);
    }
    
    @Override
    public ImmutableList toImmutableList() {
        return this;
    }
    
    @Override
    public int size() {
        return data.size();
    }
    
    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }
    
    @Override
    public  TARGET[] toArray(TARGET[] seed) {
        return data.toArray(seed);
    }
    
    @Override
    public DATA get(int index) {
        return data.get(index);
    }
    
    @Override
    public int indexOf(Object o) {
        return data.indexOf(o);
    }
    
    @Override
    public int lastIndexOf(Object o) {
        return data.lastIndexOf(o);
    }
    
    @Override
    public ListIterator listIterator() {
        return data.listIterator();
    }
    
    @Override
    public ListIterator listIterator(int index) {
        return data.listIterator();
    }
    
    @Override
    public String toString() {
        return this.data.toString();
    }
    
    @Override
    public boolean equals(Object o) {
        return this.data.equals(o);
    }
    
    @Override
    public int hashCode() {
        return this.data.hashCode();
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy