1 8 9 package net.sourceforge.chaperon.model.extended; 10 11 import net.sourceforge.chaperon.common.Decoder; 12 import net.sourceforge.chaperon.model.Violations; 13 14 20 public class CharacterInterval 21 { 22 private SingleCharacter first = null; 23 private SingleCharacter last = null; 24 private String location = null; 25 26 29 public CharacterInterval() {} 30 31 36 public void setFirstCharacter(SingleCharacter first) 37 { 38 if ((this.last==null) || (this.last.getCharacter()<first.getCharacter())) 39 this.last = first; 40 41 this.first = first; 42 } 43 44 49 public SingleCharacter getFirstCharacter() 50 { 51 return this.first; 52 } 53 54 59 public void setLastCharacter(SingleCharacter last) 60 { 61 if ((this.first==null) || (this.first.getCharacter()>last.getCharacter())) 62 this.first = last; 63 64 this.last = last; 65 } 66 67 72 public SingleCharacter getLastCharacter() 73 { 74 return this.last; 75 } 76 77 public void addSingleCharacter(SingleCharacter character) 78 { 79 if (character==null) 80 return; 81 82 if (getLastCharacter()!=null) 83 setFirstCharacter(getLastCharacter()); 84 85 setLastCharacter(character); 86 } 87 88 public SingleCharacter[] getSingleCharacters() 89 { 90 return new SingleCharacter[]{first, last}; 91 } 92 93 public char[] getLimits() 94 { 95 return new char[]{first.getCharacter(), last.getCharacter()}; 96 } 97 98 public boolean contains(char minimum, char maximum) 99 { 100 return (first.getCharacter()<=minimum) && (minimum<=last.getCharacter()) && 101 (first.getCharacter()<=maximum) && (maximum<=last.getCharacter()); 102 } 103 104 public boolean contains(char c) 105 { 106 return (first.getCharacter()<=c) && (c<=last.getCharacter()); 107 } 108 109 114 public void setLocation(String location) 115 { 116 this.location = location; 117 } 118 119 124 public String getLocation() 125 { 126 return location; 127 } 128 129 134 public String toString() 135 { 136 StringBuffer buffer = new StringBuffer (); 137 138 buffer.append(Decoder.decode(this.first.getCharacter(), Decoder.CLASS)); 139 buffer.append("-"); 140 buffer.append(Decoder.decode(this.last.getCharacter(), Decoder.CLASS)); 141 return buffer.toString(); 142 } 143 144 151 public Object clone() 152 { 153 CharacterInterval clone = new CharacterInterval(); 154 155 clone.setFirstCharacter(getFirstCharacter()); 156 clone.setLastCharacter(getLastCharacter()); 157 158 return clone; 159 } 160 161 166 public Violations validate() 167 { 168 Violations violations = new Violations(); 169 170 if ((first==null) || (last==null)) 171 violations.addViolation("Interval is incomplete", getLocation()); 172 173 if (first.getCharacter()>last.getCharacter()) 174 violations.addViolation("First is greater than the last", getLocation()); 175 176 if (first.getCharacter()==last.getCharacter()) 177 violations.addViolation("First is equal than the last", getLocation()); 178 179 return violations; 180 } 181 } 182 | Popular Tags |