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