Coverage Report - org.xmlhammer.gui.util.HistoryList
 
Classes in this File Line Coverage Branch Coverage Complexity
HistoryList
86% 
89% 
0
 
 1  
 /*
 2  
  * $Id$
 3  
  *
 4  
  * The contents of this file are subject to the Mozilla Public License 
 5  
  * Version 1.1 (the "License"); you may not use this file except in 
 6  
  * compliance with the License. You may obtain a copy of the License at 
 7  
  * http://www.mozilla.org/MPL/ 
 8  
  *
 9  
  * Software distributed under the License is distributed on an "AS IS" basis, 
 10  
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 11  
  * for the specific language governing rights and limitations under the License.
 12  
  *
 13  
  * The Original Code is XML Hammer code. (org.xmlhammer.*)
 14  
  *
 15  
  * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
 16  
  * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
 17  
  * All Rights Reserved.
 18  
  *
 19  
  * Contributor(s): Edwin Dankert <edankert@gmail.com>
 20  
  */
 21  
 package org.xmlhammer.gui.util;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 
 26  
 public class HistoryList<T> {
 27  550
     private List<T> history = null;
 28  550
     private boolean enabled = true;
 29  550
     private int index = -1;
 30  
     
 31  550
     public HistoryList() {
 32  550
         history = new ArrayList<T>();
 33  550
         reset();
 34  550
     }
 35  
     
 36  
     public void setEnabled(boolean enabled) {
 37  8468
         this.enabled = enabled;
 38  8468
     }
 39  
     
 40  
     public boolean isEnabled() {
 41  0
         return enabled;
 42  
     }
 43  
 
 44  
     public void put(T item) {
 45  6742
         if (enabled) {
 46  176
             for (int i = history.size() - 1; i > index; i--) {
 47  66
                 history.remove(i);
 48  
             }
 49  
 
 50  110
             history.add(item);
 51  110
             index++;
 52  
         }
 53  6742
     }
 54  
     
 55  
     public boolean hasNext() {
 56  7380
         boolean next = history.size()-1 > index;
 57  7380
         return next;
 58  
     }
 59  
 
 60  
     public T next() {
 61  330
         if (hasNext()) {
 62  132
             index++;
 63  132
             return history.get(index);
 64  
         }
 65  
         
 66  198
         return null;
 67  
     }
 68  
 
 69  
     public T current() {
 70  418
         if (index >= 0 && index < history.size()) {
 71  396
             return history.get(index);
 72  
         }
 73  
         
 74  22
         return null;
 75  
     }
 76  
 
 77  
     public boolean hasPrevious() {
 78  7380
         return index > 0;
 79  
     }
 80  
 
 81  
     public T previous() {
 82  330
         if (hasPrevious()) {
 83  198
             index--;
 84  198
             return history.get(index);
 85  
         }
 86  
         
 87  132
         return null;
 88  
     }
 89  
     
 90  
     public T last() {
 91  22
         if (history.size() > 0) {
 92  22
             index = history.size() - 1;
 93  22
             return history.get(index);
 94  
         }
 95  
         
 96  0
         return null;
 97  
     }
 98  
 
 99  
     public T first() {
 100  0
         if (history.size() > 0) {
 101  0
             index = 0;
 102  0
             return history.get(0);
 103  
         }
 104  
         
 105  0
         return null;
 106  
     }
 107  
 
 108  
     public void reset() {
 109  670
         history.clear();
 110  670
         index = -1;
 111  670
         enabled = true;
 112  670
     }
 113  
 }