1   package gate.creole.morph;
2   
3   import java.util.Arrays;
4   
5   /**
6    * <p>Title: </p>
7    * <p>Description: </p>
8    * <p>Copyright: Copyright (c) 2003</p>
9    * <p>Company: </p>
10   * @author not attributable
11   * @version 1.0
12   */
13  
14  public class CharacterRange extends Variable {
15  
16    private char [] varChars;
17  
18    /**
19     * Constructor
20     */
21    public CharacterRange() {
22  
23    }
24  
25    /**
26     * Tells if any value available which can be retrieved
27     * @return true if value available, false otherwise
28     */
29    public boolean hasNext() {
30      if(pointer<varChars.length) {
31        return true;
32      } else {
33        return false;
34      }
35    }
36  
37    /**
38     * Returns the next available value for this variable
39     * @return value of the variable in the String format
40     */
41    public String next() {
42      if(pointer<varChars.length) {
43        pointer++;
44        return ""+varChars[pointer-1];
45      } else {
46        return null;
47      }
48    }
49  
50  
51    /**
52     * Process the provided value and stores in the underlying data structure
53     * @param varName name of the variable
54     * @param varValue String that contains possible different values
55     * @return true if successfully stored, false otherwise
56     */
57    public boolean set(String varName, String varValue) {
58      this.varName = varName;
59      this.varValue = varValue.trim();
60      // lets process the varValue
61      // remove the [- and ] from the varValue
62      varValue = varValue.substring(2,varValue.length()-1);
63      // we need to find the sets
64      String characters = "";
65      for(int i=0; i<varValue.length();i = i + 3) {
66        String set = varValue.substring(i,i+3);
67        char startWith = set.charAt(0);
68        char endWith = set.charAt(2);
69        if(startWith>endWith) {
70          char temp = startWith;
71          startWith = endWith;
72          endWith = temp;
73        }
74        for(int j=startWith;j<=endWith;j++) {
75          // if it is already present no need to add it
76          if(characters.indexOf((char)j)<0) {
77            characters = characters + (char)(j);
78          }
79        }
80      }
81      // convert it into the character array and sort it
82      this.varChars = characters.toCharArray();
83      Arrays.sort(this.varChars);
84  
85      // now we need to convert the varValue into the proper pattern
86      // simply remove the second character (i.e. '-')
87      this.varValue = new String(new StringBuffer(this.varValue).deleteCharAt(1));
88      return true;
89    }
90  
91    /**
92     * A method that tells if the characters of the provided value are
93     * from the character range only
94     * @param value String of which the characters to be searched in the
95     * character range
96     * @return true if all characters of value string are from the
97     * specified character range, false otherwise
98     */
99    public boolean contains(String value) {
100     for(int i=0;i<value.length();i++) {
101       if(Arrays.binarySearch(this.varChars,value.charAt(i))<0) {
102         return false;
103       }
104     }
105     return true;
106   }
107 
108 }