View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript 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 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; 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.millscript.datatypes;
22  
23  
24  import org.millscript.millscript.tools.CastLibrary;
25  
26  import java.util.AbstractMap;
27  import java.util.AbstractSet;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Set;
31  
32  /**
33   * This class implements a Map, backed by a list. As such this map doesn't
34   * technically store keys, just values. The keys for this type of map are the
35   * index of the relevant value in the backing list.
36   *
37   * @deprecated  As of MillScript 10.2.0, this functionallity has been subsumed
38   * by the MillScript-Util project and it's Map API, see the list interface at
39   * {@link org.millscript.commons.util.IList}.
40   */
41  @Deprecated
42  public final class ListMap extends AbstractMap< Object, Object > {
43  
44      /**
45       * The backing list for this map.
46       */
47      private final List< Object > list;
48  
49      /**
50       * Constructs a new ListMap, backed by the specified list.
51       *
52       * @param x the list to use as a backing list for this map
53       */
54      public ListMap( final List< Object > x ) {
55          this.list = x;
56      }
57  
58      /**
59       * @see java.util.Map#entrySet()
60       */
61      @Override
62      public Set< Entry< Object, Object > > entrySet() {
63          return new MapletSet( list );
64      }
65  
66      /**
67       * NOTE - This method is 1-indexed.
68       *
69       * @see java.util.Map#get(java.lang.Object)
70       */
71      @Override
72      public Object get( final Object key ) {
73          final int n = CastLibrary.toInt( key );
74          if ( n < 0 ) {
75              final int s = list.size();
76              return list.get( s + n );
77          } else {
78              return list.get( n - 1 );
79          }
80      }
81  
82      /**
83       * @see java.util.Map#put(java.lang.Object, java.lang.Object)
84       */
85      @Override
86      public Object put( final Object key, final Object val ) {
87          int idx = CastLibrary.toInt( key );
88          return list.set( idx, val );
89      }
90  
91      /**
92       * This class represents a set of maplets for a ListMap.
93       */
94      static final class MapletSet extends AbstractSet< Entry< Object, Object > > {
95  
96          /**
97           * The list which backs this maplet set.
98           */
99          private final List< Object > list;
100 
101         /**
102          * Constructs a new MapletSet backed by the specified list.
103          *
104          * @param x the list to use as a backing list for this set
105          */
106         MapletSet( final List< Object > x ) {
107             this.list = x;
108         }
109 
110         /**
111          * @see java.util.Collection#size()
112          */
113         @Override
114         public int size() { return list.size(); }
115 
116         /**
117          * @see java.util.Collection#iterator()
118          */
119         @Override
120         public Iterator< Entry< Object, Object >  > iterator() {
121             return new MapletSetIterator( list );
122         }
123     }
124 
125     /**
126      * This class implements an interator over a maplet set, for a ListMap.
127      */
128     static final class MapletSetIterator implements Iterator< Entry< Object, Object > > {
129 
130         /**
131          * The backing iterator, iterating over the values in the parent list.
132          */
133         private Iterator< Object > it;
134 
135         /**
136          * The current iteration position.
137          */
138         private int count = 0;
139 
140         /**
141          * Constructs a new MapletSetIterator to iterate over the specified
142          * list.
143          *
144          * @param x the backing list to iterate over
145          */
146         MapletSetIterator( final List< Object > x ) {
147             this.it = x.iterator();
148         }
149 
150         /**
151          * @see java.util.Iterator#hasNext()
152          */
153         public boolean hasNext() {
154             return it.hasNext();
155         }
156 
157         /**
158          * @see java.util.Iterator#next()
159          */
160         public Entry< Object, Object > next() {
161             // ++count makes lists 1-indexed when viewed as maps
162             return new Maplet( new Integer( ++count ), it.next() );
163         }
164 
165         /**
166          * @see java.util.Iterator#remove()
167          */
168         public void remove() {
169             it.remove();
170         }
171     }
172 
173 }