001    /**
002     * Copyright (c) 2000-present 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     * @deprecated As of 7.0.0, with no direct replacement
024     */
025    @Deprecated
026    public class UniqueList<E> extends ArrayList<E> {
027    
028            public UniqueList() {
029                    super();
030            }
031    
032            public UniqueList(Collection<E> c) {
033                    super(c.size());
034    
035                    addAll(c);
036            }
037    
038            public UniqueList(int initialCapacity) {
039                    super(initialCapacity);
040            }
041    
042            @Override
043            public boolean add(E e) {
044                    if (!contains(e)) {
045                            return super.add(e);
046                    }
047                    else {
048                            return false;
049                    }
050            }
051    
052            @Override
053            public void add(int index, E e) {
054                    if (!contains(e)) {
055                            super.add(index, e);
056                    }
057            }
058    
059            @Override
060            public boolean addAll(Collection<? extends E> c) {
061                    boolean modified = false;
062    
063                    for (E e : c) {
064                            if (!contains(e)) {
065                                    super.add(e);
066    
067                                    modified = true;
068                            }
069                    }
070    
071                    return modified;
072            }
073    
074            @Override
075            public boolean addAll(int index, Collection<? extends E> c) {
076                    boolean modified = false;
077    
078                    for (E e : c) {
079                            if (!contains(e)) {
080                                    super.add(index++, e);
081    
082                                    modified = true;
083                            }
084                    }
085    
086                    return modified;
087            }
088    
089            @Override
090            public E set(int index, E e) {
091                    Thread currentThread = Thread.currentThread();
092    
093                    StackTraceElement[] stackTraceElements = currentThread.getStackTrace();
094    
095                    if (stackTraceElements.length >= 4) {
096                            StackTraceElement stackTraceElement = stackTraceElements[3];
097    
098                            String stackTraceElementString = stackTraceElement.toString();
099    
100                            if (stackTraceElementString.contains(_STACK_TRACE_COLLECTIONS)) {
101                                    return super.set(index, e);
102                            }
103                    }
104    
105                    if (!contains(e)) {
106                            return super.set(index, e);
107                    }
108                    else {
109                            return e;
110                    }
111            }
112    
113            private static final String _STACK_TRACE_COLLECTIONS =
114                    "java.util.Collections.sort(Collections.java";
115    
116    }