From 9630fc96e8382d68505a4cb8ab2ae08aec48e776 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Tue, 26 Mar 2013 02:42:14 +0100 Subject: Massive performance improvements, and a few potentially breaking changes for other lombok plugin developers. --- src/utils/lombok/core/ImmutableList.java | 188 +++++++++++++++++++++++++++++++ src/utils/lombok/eclipse/Eclipse.java | 6 +- 2 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 src/utils/lombok/core/ImmutableList.java (limited to 'src/utils') diff --git a/src/utils/lombok/core/ImmutableList.java b/src/utils/lombok/core/ImmutableList.java new file mode 100644 index 00000000..a151ae6f --- /dev/null +++ b/src/utils/lombok/core/ImmutableList.java @@ -0,0 +1,188 @@ +/* + * 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 ImmutableList implements Iterable { + private Object[] content; + private static final ImmutableList EMPTY = new ImmutableList(new Object[0]); + + @SuppressWarnings("unchecked") + public static ImmutableList of() { + return (ImmutableList) EMPTY; + } + + public static ImmutableList of(T a) { + return new ImmutableList(new Object[] {a}); + } + + public static ImmutableList of(T a, T b) { + return new ImmutableList(new Object[] {a, b}); + } + + public static ImmutableList of(T a, T b, T c) { + return new ImmutableList(new Object[] {a, b, c}); + } + + public static ImmutableList of(T a, T b, T c, T d) { + return new ImmutableList(new Object[] {a, b, c, d}); + } + + public static ImmutableList of(T a, T b, T c, T d, T e) { + return new ImmutableList(new Object[] {a, b, c, d, e}); + } + + public static ImmutableList of(T a, T b, T c, T d, T e, T f, @SuppressWarnings("unchecked") 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 ImmutableList(val); + } + + public static ImmutableList copyOf(Collection list) { + return new ImmutableList(list.toArray()); + } + + public static ImmutableList copyOf(Iterable iterable) { + List list = new ArrayList(); + for (T o : iterable) list.add(o); + return copyOf(list); + } + + private ImmutableList(Object[] content) { + this.content = content; + } + + public ImmutableList replaceElementAt(int idx, T newValue) { + Object[] newContent = content.clone(); + newContent[idx] = newValue; + return new ImmutableList(newContent); + } + + public ImmutableList 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 ImmutableList(newContent); + } + + public ImmutableList 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 ImmutableList(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 ImmutableList removeElement(T val) { + int idx = indexOf(val); + return idx == -1 ? this : removeElementAt(idx); + } + + public ImmutableList 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 ImmutableList(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 ImmutableList)) return false; + if (obj == this) return true; + return Arrays.equals(content, ((ImmutableList) obj).content); + } + + @Override public int hashCode() { + return Arrays.hashCode(content); + } +} diff --git a/src/utils/lombok/eclipse/Eclipse.java b/src/utils/lombok/eclipse/Eclipse.java index 301925d1..150f3a96 100644 --- a/src/utils/lombok/eclipse/Eclipse.java +++ b/src/utils/lombok/eclipse/Eclipse.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011 The Project Lombok Authors. + * Copyright (C) 2009-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 @@ -58,7 +58,9 @@ public class Eclipse { * but we need to deal with it. This turns [[java][lang][String]] into "java.lang.String". */ public static String toQualifiedName(char[][] typeName) { - StringBuilder sb = new StringBuilder(); + int len = typeName.length - 1; + for (char[] c : typeName) len += c.length; + StringBuilder sb = new StringBuilder(len); boolean first = true; for (char[] c : typeName) { sb.append(first ? "" : ".").append(c); -- cgit