Coverage Report - net.mtu.eggplant.util.gui.SortedListModel
 
Classes in this File Line Coverage Branch Coverage Complexity
SortedListModel
0%
0/27
0%
0/10
1.833
 
 1  
 /*
 2  
  * Copyright (c) 2000
 3  
  *      Jon Schewe.  All rights reserved
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  * 1. Redistributions of source code must retain the above copyright
 9  
  *    notice, this list of conditions and the following disclaimer.
 10  
  * 2. Redistributions in binary form must reproduce the above copyright
 11  
  *    notice, this list of conditions and the following disclaimer in the
 12  
  *    documentation and/or other materials provided with the distribution.
 13  
  *
 14  
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 15  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 18  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  
  * SUCH DAMAGE.
 25  
  *
 26  
  * I'd appreciate comments/suggestions on the code jpschewe@mtu.net
 27  
  */
 28  
 package net.mtu.eggplant.util.gui;
 29  
 
 30  
 import javax.swing.AbstractListModel;
 31  
 
 32  
 import java.util.List;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Comparator;
 35  
 import java.util.Iterator;
 36  
 import java.util.Collection;
 37  
 import java.util.Collections;
 38  
 
 39  
 /**
 40  
  * A ListModel that orders the objects according to a comparator and does
 41  
  * sorted insert.  The comparator must be able to handle all objects in the
 42  
  * list.
 43  
  *
 44  
  * @param <E> type of elements in the table model
 45  
  * @version $Revision$
 46  
  */
 47  
 public class SortedListModel<E> extends AbstractListModel {
 48  
 
 49  
   /**
 50  
      Create a SortedListModel with comparator.
 51  
 
 52  
      @pre (comparator != null)
 53  
   **/
 54  
   public SortedListModel(final Comparator<E> comparator) {
 55  0
     this(comparator, new ArrayList<E>(10));
 56  0
   }
 57  
 
 58  
   /**
 59  
      Create a SortedListModel with comparator and initalize with the list of
 60  
      objects in collection.
 61  
      
 62  
      @pre (comparator != null)
 63  
      @pre (collection != null)
 64  
   **/
 65  0
   public SortedListModel(final Comparator<E> comparator, final Collection<E> collection) {
 66  0
     _list = new ArrayList<E>(collection);
 67  0
     _comparator = comparator;
 68  0
     Collections.sort(_list, _comparator);
 69  0
   }
 70  
   
 71  
   private Comparator<E> _comparator;
 72  
   private List<E> _list;
 73  
 
 74  
   //ListModel
 75  
   public int getSize() {
 76  0
     return _list.size();
 77  
   }
 78  
 
 79  
   public E getElementAt(final int index) {
 80  0
     return _list.get(index);
 81  
   }
 82  
   //end ListModel
 83  
   
 84  
   /**
 85  
      Add o to the list.  Sorted insert.
 86  
   **/
 87  
   public void add(final E o) {
 88  0
     final Iterator<E> iter = _list.iterator();
 89  0
     int index = 0;
 90  0
     boolean flag = false;
 91  0
     while(iter.hasNext() && !flag) {
 92  0
       final E listElement = iter.next();
 93  0
       if(_comparator.compare(o, listElement) > 0) {
 94  0
         flag = true;
 95  0
         _list.add(index, o);
 96  
       }
 97  0
       index++;
 98  0
     }
 99  0
     if(!flag) {
 100  0
       _list.add(index, o);
 101  
     }
 102  0
     fireIntervalAdded(this, index, index);
 103  0
   }
 104  
 
 105  
   /**
 106  
      Remove the first occurrance of o from the list.
 107  
   **/
 108  
   public void remove(final E o) {
 109  0
     final int index = _list.indexOf(o);
 110  0
     if(_list.remove(o)) {
 111  0
       fireIntervalRemoved(this, index, index);
 112  
     }
 113  0
   }
 114  
   
 115  
 }