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  import org.millscript.millscript.tools.CastLibrary;
24  
25  import java.util.AbstractMap;
26  import java.util.AbstractSet;
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.NoSuchElementException;
30  import java.util.Set;
31  
32  /**
33   * This class implements a Map, backed by a string. Like a ListMap, this Map
34   * doesn't actually store any keys, as the key is the index of a particular
35   * character(i.e. value) within the String.
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
39   * {@link org.millscript.commons.util.list.IStringList}.
40   */
41  @Deprecated
42  public final class StringMap extends AbstractMap< Object, Object > {
43  
44      /**
45       * The backing string for this map.
46       */
47      private final String string;
48  
49      /**
50       * Constructs a new StringMap, with the specified backing string.
51       *
52       * @param s the backing string for this map
53       */
54      public StringMap( final String s ) {
55          this.string = s;
56      }
57  
58      /**
59       * @see java.util.Map#get(java.lang.Object)
60       */
61      @Override
62      public Object get( final Object key ) {
63          int n = CastLibrary.toInt( key );
64          if ( n > 0 ) {
65              return new Character( string.charAt( n - 1 ) );
66          } else {
67              final int s = string.length();
68              return new Character( string.charAt( s + n ) );
69          }
70      }
71  
72      /**
73       * @see java.util.Map#entrySet()
74       */
75      @Override
76      public Set< Entry< Object, Object > > entrySet() {
77          return new StringSet( string );
78      }
79  
80      /**
81       * This class represents the set of characters in a SringMap.
82       */
83      @SuppressWarnings("unchecked")
84      static final class StringSet extends AbstractSet< Entry< Object, Object > > {
85  
86          /**
87           * The backing string for this set.
88           */
89          private final String string;
90  
91          /**
92           * Constructs a new StringSet, with the specified backing string.
93           *
94           * @param s the backing string for this set
95           */
96          StringSet( final String s ) {
97              this.string = s;
98          }
99  
100         /**
101          * @see java.util.Collection#size()
102          */
103         @Override
104         public int size() {
105             return string.length();
106         }
107 
108         /**
109          * @see java.util.Collection#iterator()
110          */
111         @Override
112         public Iterator< Entry< Object, Object > > iterator() {
113             return new StringSetIterator( string );
114         }
115 
116     }
117 
118     /**
119      * This class implements a string set iterator, to iterate over the
120      * characters in a string based set.
121      */
122     @SuppressWarnings( "deprecation" )
123     static final class StringSetIterator extends SimpleIterator< Entry< Object, Object > > {
124 
125         /**
126          * The backing string for this iterator.
127          */
128         private final String string;
129 
130         /**
131          * The current position in the backing string.
132          */
133         private int count = 0;
134 
135         /**
136          * Constructs a new StringSetIterator to iterate over the characters in
137          * the specified string.
138          *
139          * @param s the string, whose characters to iterate over
140          */
141         StringSetIterator( final String s ) {
142             this.string = s;
143         }
144 
145         /**
146          * @see java.util.Iterator#hasNext()
147          */
148         public boolean hasNext() {
149             return count < string.length();
150         }
151 
152         /**
153          * @see java.util.Iterator#next()
154          */
155         public Entry< Object, Object > next() {
156             if ( count < string.length() ) {
157                 Map.Entry< Object, Object > e = (
158                     new Maplet(
159                         new Integer( count + 1 ),
160                         new Character( string.charAt( count ) )
161                     )
162                 );
163                 count += 1;
164                 return e;
165             } else {
166                 throw new NoSuchElementException( "no more characters in String" );
167             }
168         }
169 
170     }
171 
172 }