001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util;
016    
017    import java.util.ArrayList;
018    import java.util.Collection;
019    import java.util.Iterator;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class UniqueList<E> extends ArrayList<E> {
025    
026            public UniqueList() {
027                    super();
028            }
029    
030            public UniqueList(Collection<E> c) {
031                    super(c.size());
032    
033                    addAll(c);
034            }
035    
036            public UniqueList(int initialCapacity) {
037                    super(initialCapacity);
038            }
039    
040            @Override
041            public boolean add(E e) {
042                    if (!contains(e)) {
043                            return super.add(e);
044                    }
045                    else {
046                            return false;
047                    }
048            }
049    
050            @Override
051            public void add(int index, E e) {
052                    if (!contains(e)) {
053                            super.add(index, e);
054                    }
055            }
056    
057            @Override
058            public boolean addAll(Collection<? extends E> c) {
059                    c = new ArrayList<E>(c);
060    
061                    Iterator<? extends E> itr = c.iterator();
062    
063                    while (itr.hasNext()) {
064                            E e = itr.next();
065    
066                            if (contains(e)) {
067                                    itr.remove();
068                            }
069                    }
070    
071                    return super.addAll(c);
072            }
073    
074            @Override
075            public boolean addAll(int index, Collection<? extends E> c) {
076                    c = new ArrayList<E>(c);
077    
078                    Iterator<? extends E> itr = c.iterator();
079    
080                    while (itr.hasNext()) {
081                            E e = itr.next();
082    
083                            if (contains(e)) {
084                                    itr.remove();
085                            }
086                    }
087    
088                    return super.addAll(index, c);
089            }
090    
091            @Override
092            public E set(int index, E e) {
093                    Thread currentThread = Thread.currentThread();
094    
095                    StackTraceElement[] stackTraceElements = currentThread.getStackTrace();
096    
097                    if (stackTraceElements.length >= 4) {
098                            StackTraceElement stackTraceElement = stackTraceElements[3];
099    
100                            String stackTraceElementString = stackTraceElement.toString();
101    
102                            if (stackTraceElementString.contains(_STACK_TRACE_COLLECTIONS)) {
103                                    return super.set(index, e);
104                            }
105                    }
106    
107                    if (!contains(e)) {
108                            return super.set(index, e);
109                    }
110                    else {
111                            return e;
112                    }
113            }
114    
115            private static final String _STACK_TRACE_COLLECTIONS =
116                    "java.util.Collections.sort(Collections.java";
117    
118    }