View Javadoc
1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.util;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  /**
24   * @author Kuali Student Team
25   */
26  public class AlphaIterator implements Serializable, Iterator {
27  
28      private List<Inner> list = new ArrayList<Inner>();
29      private String prefix;
30  
31      public AlphaIterator(String prefix){
32          super();
33          this.prefix = prefix;
34      }
35  
36      @Override
37      public boolean hasNext() {
38          return true;
39      }
40  
41      @Override
42      public Object next() {
43  
44          //Add an initial value to the list
45          if (list.isEmpty()) {
46              list.add(new Inner());
47          } else {
48  
49              //Increment the value
50              for (int i = list.size() - 1; i >= 0; i--) {
51                  Inner in = list.get(i);
52                  if (in.increment()>0) {
53                      break;
54                  } else {
55                      in.reset();
56                      if (i == 0) {
57                          list.add(new Inner());
58                      }
59                  }
60              }
61          }
62  
63          //Create the return string
64          StringBuilder returnValue = new StringBuilder();
65          for (Inner in : list) {
66              returnValue.append((char)in.current());
67          }
68  
69          return prefix + returnValue;
70      }
71  
72      @Override
73      public void remove() {
74          //To change body of implemented methods use File | Settings | File Templates.
75      }
76  
77      private class Inner implements Serializable{
78  
79          private final static int START = 'A';
80          private final static int END = 'Z';
81  
82          private int value;
83  
84          public Inner() {
85              value = START;
86          }
87  
88          public void reset() {
89              value = START;
90          }
91  
92          public int current() {
93              return value;
94          }
95  
96          public int increment() {
97              if (value == END) {
98                  return -1;
99              } else {
100                 return value++;
101             }
102         }
103 
104     }
105 }