001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.ArrayList;
018    import java.util.Collection;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     * @author Shuyang Zhou
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                    boolean modified = false;
060    
061                    for (E e : c) {
062                            if (!contains(e)) {
063                                    super.add(e);
064    
065                                    modified = true;
066                            }
067                    }
068    
069                    return modified;
070            }
071    
072            @Override
073            public boolean addAll(int index, Collection<? extends E> c) {
074                    boolean modified = false;
075    
076                    for (E e : c) {
077                            if (!contains(e)) {
078                                    super.add(index++, e);
079    
080                                    modified = true;
081                            }
082                    }
083    
084                    return modified;
085            }
086    
087            @Override
088            public E set(int index, E e) {
089                    Thread currentThread = Thread.currentThread();
090    
091                    StackTraceElement[] stackTraceElements = currentThread.getStackTrace();
092    
093                    if (stackTraceElements.length >= 4) {
094                            StackTraceElement stackTraceElement = stackTraceElements[3];
095    
096                            String stackTraceElementString = stackTraceElement.toString();
097    
098                            if (stackTraceElementString.contains(_STACK_TRACE_COLLECTIONS)) {
099                                    return super.set(index, e);
100                            }
101                    }
102    
103                    if (!contains(e)) {
104                            return super.set(index, e);
105                    }
106                    else {
107                            return e;
108                    }
109            }
110    
111            private static final String _STACK_TRACE_COLLECTIONS =
112                    "java.util.Collections.sort(Collections.java";
113    
114    }