001    /**
002     * <copyright>
003     *
004     * Copyright (c) 2002-2006 IBM Corporation and others.
005     * All rights reserved.   This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors: 
011     *   IBM - Initial API and implementation
012     *
013     * </copyright>
014     *
015     * $Id: UniqueEList.java,v 1.4 2006/12/05 20:19:56 emerks Exp $
016     */
017    package org.eclipse.emf.common.util;
018    
019    
020    import java.util.Collection;
021    
022    
023    /**
024     * A <code>BasicEList</code> that allows only {@link #isUnique unique} elements.
025     */
026    public class UniqueEList<E> extends BasicEList<E>
027    {
028      private static final long serialVersionUID = 1L;
029    
030      /**
031       * Creates an empty instance with no initial capacity.
032       */
033      public UniqueEList()
034      {
035        super();
036      }
037    
038      /**
039       * Creates an empty instance with the given capacity.
040       * @param initialCapacity the initial capacity of the list before it must grow.
041       * @exception IllegalArgumentException if the <code>initialCapacity</code> is negative.
042       */
043      public UniqueEList(int initialCapacity)
044      {
045        super(initialCapacity);
046      }
047    
048      /**
049       * Creates an instance that is a copy of the collection, with duplicates removed.
050       * @param collection the initial contents of the list.
051       */
052      public UniqueEList(Collection<? extends E> collection)
053      {
054        super(collection.size());
055        addAll(collection);   
056      }
057    
058      /**
059       * Returns <code>true</code> because this list requires uniqueness.
060       * @return <code>true</code>.
061       */
062      @Override
063      protected boolean isUnique()
064      {
065        return true;
066      }
067    
068      /**
069       * A <code>UniqueEList</code> that {@link #useEquals uses} <code>==</code> instead of <code>equals</code> to compare members.
070       */
071      public static class FastCompare<E> extends UniqueEList<E>
072      {
073        private static final long serialVersionUID = 1L;
074    
075        /**
076         * Creates an empty instance with no initial capacity.
077         */
078        public FastCompare()
079        {
080          super();
081        }
082    
083        /**
084         * Creates an empty instance with the given capacity.
085         * @param initialCapacity the initial capacity of the list before it must grow.
086         * @exception IllegalArgumentException if the <code>initialCapacity</code> is negative.
087         */
088        public FastCompare(int initialCapacity)
089        {
090          super(initialCapacity);
091        }
092    
093        /**
094         * Creates an instance that is a copy of the collection, with duplicates removed.
095         * @param collection the initial contents of the list.
096         */
097        public FastCompare(Collection<? extends E> collection)
098        {
099          super(collection.size());
100          addAll(collection);
101        }
102    
103        /**
104         * Returns <code>false</code> because this list uses <code>==</code>.
105         * @return <code>false</code>.
106         */
107        @Override
108        protected boolean useEquals()
109        {
110          return false;
111        }
112      }
113    }