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

lombok.core.LombokImmutableList Maven / Gradle / Ivy

Go to download

Spice up your java: Automatic Resource Management, automatic generation of getters, setters, equals, hashCode and toString, and more!

There is a newer version: 1.18.36
Show newest version
/*
 * Copyright (C) 2013 The Project Lombok Authors.
 * 
 * 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 lombok.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public final class LombokImmutableList implements Iterable {
	private Object[] content;
	private static final LombokImmutableList EMPTY = new LombokImmutableList(new Object[0]);
	
	@SuppressWarnings("unchecked")
	public static  LombokImmutableList of() {
		return (LombokImmutableList) EMPTY;
	}
	
	public static  LombokImmutableList of(T a) {
		return new LombokImmutableList(new Object[] {a});
	}
	
	public static  LombokImmutableList of(T a, T b) {
		return new LombokImmutableList(new Object[] {a, b});
	}
	
	public static  LombokImmutableList of(T a, T b, T c) {
		return new LombokImmutableList(new Object[] {a, b, c});
	}
	
	public static  LombokImmutableList of(T a, T b, T c, T d) {
		return new LombokImmutableList(new Object[] {a, b, c, d});
	}
	
	public static  LombokImmutableList of(T a, T b, T c, T d, T e) {
		return new LombokImmutableList(new Object[] {a, b, c, d, e});
	}
	
	public static  LombokImmutableList of(T a, T b, T c, T d, T e, T f, T... g) {
		Object[] rest = g == null ? new Object[] {null} : g;
		Object[] val = new Object[rest.length + 6];
		System.arraycopy(rest, 0, val, 6, rest.length);
		val[0] = a;
		val[1] = b;
		val[2] = c;
		val[3] = d;
		val[4] = e;
		val[5] = f;
		return new LombokImmutableList(val);
	}
	
	public static  LombokImmutableList copyOf(Collection list) {
		return new LombokImmutableList(list.toArray());
	}
	
	public static  LombokImmutableList copyOf(Iterable iterable) {
		List list = new ArrayList();
		for (T o : iterable) list.add(o);
		return copyOf(list);
	}
	
	public static  LombokImmutableList copyOf(T[] array) {
		Object[] content = new Object[array.length];
		System.arraycopy(array, 0, content, 0, array.length);
		return new LombokImmutableList(content);
	}
	
	private LombokImmutableList(Object[] content) {
		this.content = content;
	}
	
	public LombokImmutableList replaceElementAt(int idx, T newValue) {
		Object[] newContent = content.clone();
		newContent[idx] = newValue;
		return new LombokImmutableList(newContent);
	}
	
	public LombokImmutableList append(T newValue) {
		int len = content.length;
		Object[] newContent = new Object[len + 1];
		System.arraycopy(content, 0, newContent, 0, len);
		newContent[len] = newValue;
		return new LombokImmutableList(newContent);
	}
	
	public LombokImmutableList prepend(T newValue) {
		int len = content.length;
		Object[] newContent = new Object[len + 1];
		System.arraycopy(content, 0, newContent, 1, len);
		newContent[0] = newValue;
		return new LombokImmutableList(newContent);
	}
	
	public int indexOf(T val) {
		int len = content.length;
		if (val == null) {
			for (int i = 0; i < len; i++) if (content[i] == null) return i;
			return -1;
		}
		
		for (int i = 0; i < len; i++) if (val.equals(content[i])) return i;
		return -1;
	}
	
	public LombokImmutableList removeElement(T val) {
		int idx = indexOf(val);
		return idx == -1 ? this : removeElementAt(idx);
	}
	
	public LombokImmutableList removeElementAt(int idx) {
		int len = content.length;
		Object[] newContent = new Object[len - 1];
		if (idx > 0) System.arraycopy(content, 0, newContent, 0, idx);
		if (idx < len - 1) System.arraycopy(content, idx + 1, newContent, idx, len - idx - 1);
		return new LombokImmutableList(newContent);
	}
	
	public boolean isEmpty() {
		return content.length == 0;
	}
	
	public int size() {
		return content.length;
	}
	
	@SuppressWarnings("unchecked")
	public T get(int idx) {
		return (T) content[idx];
	}
	
	public boolean contains(T in) {
		if (in == null) {
			for (Object e : content) if (e == null) return true;
			return false;
		}
		
		for (Object e : content) if (in.equals(e)) return true;
		return false;
	}
	
	public Iterator iterator() {
		return new Iterator() {
			private int idx = 0;
			@Override public boolean hasNext() {
				return idx < content.length;
			}
			
			@SuppressWarnings("unchecked")
			@Override public T next() {
				if (idx < content.length) return (T) content[idx++];
				throw new NoSuchElementException();
			}
			
			@Override public void remove() {
				throw new UnsupportedOperationException("List is immutable");
			}
		};
	}
	
	@Override public String toString() {
		return Arrays.toString(content);
	}
	
	@Override public boolean equals(Object obj) {
		if (!(obj instanceof LombokImmutableList)) return false;
		if (obj == this) return true;
		return Arrays.equals(content, ((LombokImmutableList) obj).content);
	}
	
	@Override public int hashCode() {
		return Arrays.hashCode(content);
	}
}