View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Util: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript-Util.
6   //
7   // MillScript-Util is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-Util is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-Util; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.commons.util.list;
22  
23  import java.io.Serializable;
24  
25  import org.millscript.commons.util.IList;
26  import org.millscript.commons.util.ListIterator;
27  import org.millscript.commons.util.iterator.AbstractListIterator;
28  
29  /**
30   * This class provides an immutable <code>List</code> implementation which is
31   * holds a continuous list of integers.
32   */
33  public class IIntegerList extends AbstractIList< Integer > implements Cloneable, Serializable {
34  
35      /**
36       * This is the ID from the release 0.1.0 for future compatibility.
37       */
38      private static final long serialVersionUID = -9044367947395107412L;
39  
40      /**
41       * This class provides a map interator implementation which iterates over a
42       * continuous set of integers.
43       */
44      public static class IntegerListIterator extends AbstractListIterator< Integer > {
45  
46          /**
47           * The number of values in the iteration.
48           */
49          private final int size;
50  
51          /**
52           * The first integer in the list.
53           */
54          private final int start;
55  
56          /**
57           * Constructs a new immutable integer map iterator which starts and
58           * ends at the specified integers.
59           *
60           * @param f the first integer in the list
61           * @param l the last integer in the list, if this is less than the
62           * first integer, the iterator will have no values
63           */
64          public IntegerListIterator( final int f, final int l ) {
65              this.size = l - f + 1;
66              this.start = f;
67          }
68  
69          /**
70           * @see org.millscript.commons.util.iterator.AbstractMapIterator#getValue()
71           */
72          @Override
73          protected Integer getValue() {
74              return new Integer( super.position + this.start - 1 );
75          }
76          
77          /**
78           * @see org.millscript.commons.util.MapIterator#hasNext()
79           */
80          public boolean hasNext() {
81              return super.position + 1 <= this.size;
82          }
83  
84          /**
85           * @see org.millscript.commons.util.iterator.AbstractMapIterator#outOfBounds()
86           */
87          @Override
88          protected boolean outOfBounds() {
89              return super.position == 0 || super.position > this.size;
90          }
91  
92      }
93  
94      /**
95       * The last integer in the list.
96       */
97      private final int end;
98  
99      /**
100      * The first integer in the list.
101      */
102     private final int start;
103 
104     /**
105      * Constructs a new empty immutable integer list. 
106      */
107     public IIntegerList() {
108         this.end = -1;
109         this.start = 0;
110     }
111 
112     /**
113      * Constructs a new immutable integer list which starts and ends at the
114      * specified integers.
115      *
116      * @param f the first integer in the list
117      * @param l the last integer in the list, if this is less than the first
118      * integer, the list will be of zero length
119      */
120     public IIntegerList( final int f, final int l ) {
121         this.end = l;
122         this.start = f;
123     }
124 
125     /**
126      * @see java.lang.Object#clone()
127      */
128     @Override
129     public Object clone() throws CloneNotSupportedException {
130         // Nothing special required for this clone
131         return super.clone();
132     }
133 
134     /**
135      * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
136      */
137     @Override
138     protected Integer doGet( final int pos ) {
139         return new Integer( this.start + pos - 1 );
140     }
141 
142     /**
143      * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
144      */
145     @Override
146     protected IList< Integer > doSlice( final int first, final int last, final boolean share ) {
147         // Return a copy of the relevant slice of the list
148         // NOTE - we can't really share backing store here as there isn't any!
149         return new IIntegerList( this.start + first - 1, this.start + last - 1 );
150     }
151 
152     /**
153      * @see org.millscript.commons.util.IList#indexOf(java.lang.Object)
154      */
155     public int indexOf( final Integer value ) {
156         if ( value == null ) {
157             return 0;
158         } else {
159             return value.intValue() < start || value.intValue() > end ? 0 : value.intValue() - start + 1;
160         }
161     }
162 
163     /**
164      * @see org.millscript.commons.util.IMap#iterator(boolean)
165      */
166     public ListIterator< Integer > iterator( final boolean share ) {
167         // There is no concept of shared store for an integer list
168         return new IntegerListIterator( this.start, this.end );
169     }
170 
171     /**
172      * @see org.millscript.commons.util.IMap#size()
173      */
174     public int size() {
175         return start > end ? 0 : end - start + 1;
176     }
177 
178 }