001/**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.util;
017
018import java.io.Serializable;
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023/**
024 * @author Kuali Student Team
025 */
026public class AlphaIterator implements Serializable, Iterator {
027
028    private List<Inner> list = new ArrayList<Inner>();
029    private String prefix;
030
031    public AlphaIterator(String prefix){
032        super();
033        this.prefix = prefix;
034    }
035
036    @Override
037    public boolean hasNext() {
038        return true;
039    }
040
041    @Override
042    public Object next() {
043
044        //Add an initial value to the list
045        if (list.isEmpty()) {
046            list.add(new Inner());
047        } else {
048
049            //Increment the value
050            for (int i = list.size() - 1; i >= 0; i--) {
051                Inner in = list.get(i);
052                if (in.increment()>0) {
053                    break;
054                } else {
055                    in.reset();
056                    if (i == 0) {
057                        list.add(new Inner());
058                    }
059                }
060            }
061        }
062
063        //Create the return string
064        StringBuilder returnValue = new StringBuilder();
065        for (Inner in : list) {
066            returnValue.append((char)in.current());
067        }
068
069        return prefix + returnValue;
070    }
071
072    @Override
073    public void remove() {
074        //To change body of implemented methods use File | Settings | File Templates.
075    }
076
077    private class Inner implements Serializable{
078
079        private final static int START = 'A';
080        private final static int END = 'Z';
081
082        private int value;
083
084        public Inner() {
085            value = START;
086        }
087
088        public void reset() {
089            value = START;
090        }
091
092        public int current() {
093            return value;
094        }
095
096        public int increment() {
097            if (value == END) {
098                return -1;
099            } else {
100                return value++;
101            }
102        }
103
104    }
105}