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.Fault;
26  import org.millscript.commons.util.IList;
27  import org.millscript.commons.util.ListIterator;
28  import org.millscript.commons.util.iterator.NullListIterator;
29  
30  /**
31   * This class provides an immutable empty <code>List</code> implementation.
32   */
33  public class IEmptyList< 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 = 4150759314269848376L;
39  
40      /**
41       * The immutable empty list. This is provided for convenience within your
42       * applications, where an immutable empty list is required.
43       */
44      public static final IList EMPTY_LIST = new IEmptyList();
45  
46      /**
47       * @see java.lang.Object#clone()
48       */
49      @Override
50      public Object clone() throws CloneNotSupportedException {
51          // Nothing special required for this clone
52          return super.clone();
53      }
54  
55      /**
56       * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
57       */
58      @Override
59      protected V doGet( final int pos ) {
60          throw new Fault(
61              "An empty list has no elements to index!"
62          ).culprit( "index", pos ).decorate( this ).mishap();
63      }
64  
65      /**
66       * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
67       */
68      @Override
69      protected IList< V > doSlice( final int first, final int last, final boolean share ) {
70          // It's an immutable empty list, just return this object as the only
71          // valid slice is a slice with zero elements(after the generic
72          // handling for a zero length slice)
73          throw new Fault(
74              "Cannot make a non-zero length slice of an empty list"
75          ).culprit(
76              "first index",
77              first
78          ).culprit(
79              "last index",
80              last
81          ).decorate( this ).mishap();
82      }
83  
84      /**
85       * @see org.millscript.commons.util.IList#indexOf(java.lang.Object)
86       */
87      public int indexOf( final V value ) {
88          return 0;
89      }
90  
91      /**
92       * @see org.millscript.commons.util.IMap#iterator(boolean)
93       */
94      @SuppressWarnings( "unchecked" )
95      public ListIterator< V > iterator( final boolean share ) {
96          // There is no concept of sharing store for an empty list...
97          return new NullListIterator();
98      }
99  
100     /**
101      * @see org.millscript.commons.util.IMap#size()
102      */
103     public int size() {
104         return 0;
105     }
106 
107 }