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.alert.alerts.IllegalArgumentAlert;
26  import org.millscript.commons.util.IList;
27  import org.millscript.commons.util.ListIterator;
28  import org.millscript.commons.util.iterator.VariableLengthListIterator;
29  
30  /**
31   * This class implements a variable length list which has the same value for
32   * each element.
33   */
34  public class IVarLengthList< V > extends AbstractIList< V > implements Cloneable, Serializable {
35  
36      /**
37       * This is the ID from the release 0.1.0 for future compatibility.
38       */
39      private static final long serialVersionUID = -9221933010435703703L;
40  
41      /**
42       * The value to use for each value in the list.
43       */
44      private final V store;
45  
46      /**
47       * The size of the list.
48       */
49      private final int size;
50  
51      /**
52       * Constructs a new variable length list with the specified size and object
53       * to use for each value in it.
54       *
55       * @param length    the size of the list
56       * @param value the value for each element
57       */
58      public IVarLengthList( final int length, final V value ) {
59          if ( length < 0 ) {
60              throw new IllegalArgumentAlert(
61                  "Can't construct a list of negative length"
62              ).culprit(
63                  "requested length",
64                  length
65              ).culprit(
66                  "list value",
67                  value
68              ).mishap();
69          }
70          this.size = length;
71          this.store = value;
72      }
73  
74      /**
75       * @see java.lang.Object#clone()
76       */
77      @Override
78      public Object clone() throws CloneNotSupportedException {
79          // Nothing special required for this clone
80          return super.clone();
81      }
82  
83      /**
84       * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
85       */
86      @Override
87      protected V doGet( final int pos ) {
88          return this.store;
89      }
90  
91      /**
92       * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
93       */
94      @Override
95      protected IList< V > doSlice( final int first, final int last, final boolean share ) {
96          // We don't need to worry about sharing here as there isn't any backing
97          // store to share
98          return new IVarLengthList< V >( last - first + 1, this.store );
99      }
100 
101     /**
102      * @see org.millscript.commons.util.IList#indexOf(java.lang.Object)
103      */
104     public int indexOf( final V value ) {
105         if ( this.size != 0 ) {
106             if ( value == null ? this.store == null : value.equals( this.store ) ) {
107                 return 1;
108             }
109         }
110         return 0;
111     }
112 
113     /**
114      * @see org.millscript.commons.util.IMap#iterator(boolean)
115      */
116     public ListIterator< V > iterator( final boolean share ) {
117         // No proper backing store to copy, so we just use the reference to
118         // the single list element value
119         return new VariableLengthListIterator< V >( this.size, this.store );
120     }
121 
122     /**
123      * @see org.millscript.commons.util.IMap#size()
124      */
125     public int size() {
126         return this.size;
127     }
128 
129 }