CharacterSet.java |
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 CharacterSet extends Variable { 15 16 private char [] varChars; 17 /** 18 * Constructor 19 */ 20 public CharacterSet() { 21 22 } 23 24 /** 25 * Tells if any value available which can be retrieved 26 * @return true if value available, false otherwise 27 */ 28 public boolean hasNext() { 29 if(pointer<varChars.length) { 30 return true; 31 } else { 32 return false; 33 } 34 } 35 36 /** 37 * Returns the next available value for this variable 38 * @return value of the variable in the String format 39 */ 40 public String next() { 41 if(pointer<varChars.length) { 42 pointer++; 43 return ""+varChars[pointer-1]; 44 } else { 45 return null; 46 } 47 } 48 49 /** 50 * Process the provided value and stores in the underlying data structure 51 * @param varName name of the variable 52 * @param varValue String that contains possible different values 53 * @return true if successfully stored, false otherwise 54 */ 55 public boolean set(String varName, String varValue) { 56 this.varName = varName; 57 this.varValue = varValue; 58 // here the varValue would be in the following format 59 // [abcdefg] // we need to sort it, so that while searching it will be 60 // easier and faster to perform the binary search 61 varValue = varValue.substring(1,varValue.length()-1); 62 this.varChars = varValue.toCharArray(); 63 Arrays.sort(this.varChars); 64 return true; 65 } 66 67 /** 68 * A method that tells if the characters of the provided value are 69 * from the characterSet only 70 * @param value String of which the characters to be searched in the 71 * characterSet 72 * @return true if all characters of value string are from the 73 * specified characterSet, false otherwise 74 */ 75 public boolean contains(String value) { 76 for(int i=0;i<value.length();i++) { 77 if(Arrays.binarySearch(this.varChars,value.charAt(i))<0) { 78 return false; 79 } 80 } 81 return true; 82 } 83 }