Coverage Report - org.argouml.uml.util.SortedListModel
 
Classes in this File Line Coverage Branch Coverage Complexity
SortedListModel
0%
0/57
0%
0/20
1.722
 
 1  
 /* $Id: SortedListModel.java 17891 2010-01-12 21:21:06Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.util;
 40  
 
 41  
 import java.util.Collection;
 42  
 import java.util.Iterator;
 43  
 import java.util.Set;
 44  
 import java.util.TreeSet;
 45  
 
 46  
 import javax.swing.AbstractListModel;
 47  
 
 48  
 
 49  
 
 50  
 /**
 51  
  * A ListModel which keeps the list in sorted order. Many, but not all, of the
 52  
  * methods from the java.util.List are implemented. Those which are obey its
 53  
  * contract.
 54  
  * <p>
 55  
  * This is a low performance implementation designed for use with small lists
 56  
  * (such as typically appear in the GUI). It does a linear search of the 
 57  
  * set for any indexed operations (e.g. getElementAt(int)).
 58  
  */
 59  0
 public class SortedListModel extends AbstractListModel implements Collection {
 60  
     
 61  0
     private Set delegate = new TreeSet(new PathComparator());
 62  
 
 63  
     /**
 64  
      * Returns the number of components in this list.
 65  
      * <p>
 66  
      * This method is identical to <code>size</code>, which implements the 
 67  
      * <code>List</code> interface defined in the 1.2 Collections framework.
 68  
      * This method exists in conjunction with <code>setSize</code> so that
 69  
      * <code>size</code> is identifiable as a JavaBean property.
 70  
      *
 71  
      * @return  the number of components in this list
 72  
      * @see #size()
 73  
      */
 74  
     public int getSize() {
 75  0
         return delegate.size();
 76  
     }
 77  
 
 78  
     /**
 79  
      * Returns the component at the specified index.
 80  
      * <blockquote>
 81  
      * <b>Note:</b> Although this method is not deprecated, the preferred
 82  
      *    method to use is <code>get(int)</code>, which implements the 
 83  
      *    <code>List</code> interface defined in the 1.2 Collections framework.
 84  
      * </blockquote>
 85  
      * @param      index   an index into this list
 86  
      * @return     the component at the specified index
 87  
      * @exception  ArrayIndexOutOfBoundsException  if the <code>index</code> 
 88  
      *             is negative or greater than the current size of this 
 89  
      *             list
 90  
      * @see #get(int)
 91  
      */
 92  
     public Object getElementAt(int index) {
 93  0
         Object result = null;
 94  
         // TODO: If this turns out to be a performance bottleneck, we can 
 95  
         // probably optimize the common case by caching our iterator and current
 96  
         // position, assuming that the next request will be for a greater index
 97  0
         Iterator it = delegate.iterator();
 98  0
         while (index >= 0) {
 99  0
             if (it.hasNext()) {
 100  0
                 result = it.next();
 101  
             } else {
 102  0
                 throw new ArrayIndexOutOfBoundsException();
 103  
             }
 104  0
             index--;
 105  
         }
 106  0
         return result;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Returns the component at the specified index.
 111  
      * @param      index   an index into this list
 112  
      * @return     the component at the specified index
 113  
      * @exception  ArrayIndexOutOfBoundsException  if the <code>index</code> 
 114  
      *             is negative or greater than the current size of this 
 115  
      *             list
 116  
      */
 117  
     public Object get(int index) {
 118  0
         return getElementAt(index);
 119  
     }
 120  
     
 121  
     /**
 122  
      * @param o object to search for
 123  
      * @return index of object or -1 if not found
 124  
      * @see java.util.List#indexOf(Object)
 125  
      */
 126  
     public int indexOf(Object o) {
 127  0
         int index = 0;
 128  0
         Iterator it = delegate.iterator();
 129  0
         if (o == null) {
 130  0
             while (it.hasNext()) {
 131  0
                 if (o == it.next()) {
 132  0
                     return index;
 133  
                 }
 134  0
                 index++;
 135  
             }            
 136  
         } else {
 137  0
             while (it.hasNext()) {
 138  0
                 if (o.equals(it.next())) {
 139  0
                     return index;
 140  
                 }
 141  0
                 index++;
 142  
             }
 143  
         }
 144  0
         return -1;
 145  
     }
 146  
 
 147  
     public int size() {
 148  0
         return getSize();
 149  
     }
 150  
 
 151  
 
 152  
     public boolean isEmpty() {
 153  0
         return delegate.isEmpty();
 154  
     }
 155  
 
 156  
 
 157  
     public boolean contains(Object elem) {
 158  0
         return delegate.contains(elem);
 159  
     }
 160  
 
 161  
 
 162  
     public boolean add(Object obj) {
 163  0
         boolean status = delegate.add(obj);
 164  0
         int index = indexOf(obj);
 165  0
         fireIntervalAdded(this, index, index);
 166  0
         return status;
 167  
     }
 168  
     
 169  
     
 170  
     public boolean addAll(Collection c) {
 171  0
         boolean status = delegate.addAll(c);
 172  0
         fireContentsChanged(this, 0, delegate.size() - 1);
 173  0
         return status;
 174  
     }
 175  
 
 176  
     
 177  
     public boolean remove(Object obj) {
 178  0
         int index = indexOf(obj);
 179  0
         boolean rv = delegate.remove(obj);
 180  0
         if (index >= 0) {
 181  0
             fireIntervalRemoved(this, index, index);
 182  
         }
 183  0
         return rv;
 184  
     }
 185  
     
 186  
     public boolean removeAll(Collection c) {
 187  0
         boolean status = false;
 188  0
         for (Object o : c) {
 189  0
             status = status | remove(o);
 190  
         }
 191  0
         return status;
 192  
     }
 193  
 
 194  
     @Override
 195  
     public String toString() {
 196  0
         return delegate.toString();
 197  
     }
 198  
 
 199  
     public Object[] toArray() {
 200  0
         return delegate.toArray();
 201  
     }
 202  
     
 203  
 
 204  
     public Object[] toArray(Object[] a) {
 205  0
         return delegate.toArray(a);
 206  
     }
 207  
 
 208  
 
 209  
     public void clear() {
 210  0
         int index1 = delegate.size() - 1;
 211  0
         delegate.clear();
 212  0
         if (index1 >= 0) {
 213  0
             fireIntervalRemoved(this, 0, index1);
 214  
         }
 215  0
     }
 216  
 
 217  
     public boolean containsAll(Collection c) {
 218  0
         return delegate.containsAll(c);
 219  
     }
 220  
 
 221  
     public Iterator iterator() {
 222  0
         return delegate.iterator();
 223  
     }
 224  
 
 225  
     public boolean retainAll(Collection c) {
 226  0
         int size = delegate.size();
 227  0
         boolean status =  delegate.retainAll(c);
 228  
         // TODO: is this the right range here?
 229  0
         fireContentsChanged(this, 0, size - 1);
 230  0
         return status;
 231  
     }
 232  
 
 233  
 }
 234