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.VariableLengthListIterator;
28  
29  /**
30   * This class provides an immutable <code>List</code> implementation which
31   * contains a single object.
32   */
33  public class ISingletonList< V > extends AbstractIList< V > 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 = 8095301075490721350L;
39  
40      /**
41       * The single value contained in this list.
42       */
43      private final V store;
44  
45      /**
46        * Constructs a new immutable singleton list with the specified object as
47        * its single element.
48        *
49        * @param value the single object for this list
50        */
51      public ISingletonList( final V value ) {
52          this.store = value;
53      }
54  
55      /**
56       * @see java.lang.Object#clone()
57       */
58      @Override
59      public Object clone() throws CloneNotSupportedException {
60          // Nothing special required for this clone
61          return super.clone();
62      }
63  
64      /**
65       * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
66       */
67      @Override
68      protected V doGet( final int pos ) {
69          return this.store;
70      }
71  
72      /**
73       * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
74       */
75      @Override
76      protected IList< V > doSlice( final int first, final int last, final boolean share ) {
77          // It's an immutable singleton list, just return this object as the
78          // only valid slice is a slice with one element(after the generic
79          // handling for a zero length slice)
80          // NOTE - There's no need to worry about sharing either
81          return this;
82      }
83  
84      /**
85       * @see org.millscript.commons.util.IList#indexOf(java.lang.Object)
86       */
87      public int indexOf( final V value ) {
88          if ( value == null ? this.store == null : value.equals( this.store ) ) {
89              return 1;
90          } else {
91              return 0;
92          }
93      }
94  
95      /**
96       * @see org.millscript.commons.util.IMap#iterator(boolean)
97       */
98      public ListIterator< V > iterator( final boolean share ) {
99          // We're not gonna start trying to clone the backing object, it doesn't
100         // make sense, so we can ignore the share parameter
101         return new VariableLengthListIterator< V >( 1, this.store );
102     }
103 
104     /**
105      * @see org.millscript.commons.util.IMap#size()
106      */
107     public int size() {
108         return 1;
109     }
110 
111 }