CharacterRange.java
001 package gate.creole.morph;
002 
003 import java.util.Arrays;
004 
005 /**
006  <p>Title: </p>
007  <p>Description: </p>
008  <p>Copyright: Copyright (c) 2003</p>
009  <p>Company: </p>
010  @author not attributable
011  @version 1.0
012  */
013 
014 public class CharacterRange extends Variable {
015 
016   private char [] varChars;
017 
018   /**
019    * Constructor
020    */
021   public CharacterRange() {
022 
023   }
024 
025   /**
026    * Tells if any value available which can be retrieved
027    @return true if value available, false otherwise
028    */
029   public boolean hasNext() {
030     if(pointer<varChars.length) {
031       return true;
032     else {
033       return false;
034     }
035   }
036 
037   /**
038    * Returns the next available value for this variable
039    @return value of the variable in the String format
040    */
041   public String next() {
042     if(pointer<varChars.length) {
043       pointer++;
044       return ""+varChars[pointer-1];
045     else {
046       return null;
047     }
048   }
049 
050 
051   /**
052    * Process the provided value and stores in the underlying data structure
053    @param varName name of the variable
054    @param varValue String that contains possible different values
055    @return true if successfully stored, false otherwise
056    */
057   public boolean set(String varName, String varValue) {
058     this.varName = varName;
059     this.varValue = varValue.trim();
060     // lets process the varValue
061     // remove the [- and ] from the varValue
062     varValue = varValue.substring(2,varValue.length()-1);
063     // we need to find the sets
064     String characters = "";
065     for(int i=0; i<varValue.length();i = i + 3) {
066       String set = varValue.substring(i,i+3);
067       char startWith = set.charAt(0);
068       char endWith = set.charAt(2);
069       if(startWith>endWith) {
070         char temp = startWith;
071         startWith = endWith;
072         endWith = temp;
073       }
074       for(int j=startWith;j<=endWith;j++) {
075         // if it is already present no need to add it
076         if(characters.indexOf((char)j)<0) {
077           characters = characters + (char)(j);
078         }
079       }
080     }
081     // convert it into the character array and sort it
082     this.varChars = characters.toCharArray();
083     Arrays.sort(this.varChars);
084 
085     // now we need to convert the varValue into the proper pattern
086     // simply remove the second character (i.e. '-')
087     this.varValue = new String(new StringBuffer(this.varValue).deleteCharAt(1));
088     return true;
089   }
090 
091   /**
092    * A method that tells if the characters of the provided value are
093    * from the character range only
094    @param value String of which the characters to be searched in the
095    * character range
096    @return true if all characters of value string are from the
097    * specified character range, false otherwise
098    */
099   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 }