1 24 25 package com.mckoi.database; 26 27 import java.util.Locale ; 28 import java.text.Collator ; 29 import com.mckoi.database.global.StringAccessor; 30 import java.io.Reader ; 31 import java.io.IOException ; 32 33 38 39 public final class TStringType extends TType { 40 41 static final long serialVersionUID = -4189752898050725908L; 42 43 46 private int max_size; 47 48 51 private Locale locale; 52 53 57 private int strength; 58 59 63 private int decomposition; 64 65 68 private transient Collator collator; 69 70 78 public TStringType(int sql_type, int max_size, Locale locale, 79 int strength, int decomposition) { 80 super(sql_type); 81 this.max_size = max_size; 82 this.strength = strength; 83 this.decomposition = decomposition; 84 this.locale = locale; 85 } 86 87 98 public TStringType(int sql_type, int max_size, String locale_str, 99 int strength, int decomposition) { 100 super(sql_type); 101 this.max_size = max_size; 102 this.strength = strength; 103 this.decomposition = decomposition; 104 105 if (locale_str != null && locale_str.length() >= 2) { 106 String language = locale_str.substring(0, 2); 107 String country = ""; 108 String variant = ""; 109 if (locale_str.length() > 2) { 110 country = locale_str.substring(2, 4); 111 if (locale_str.length() > 4) { 112 variant = locale_str.substring(4); 113 } 114 } 115 locale = new Locale (language, country, variant); 116 } 117 118 } 119 120 124 public TStringType(int sql_type, int max_size, String locale_str) { 125 this(sql_type, max_size, locale_str, -1, -1); 126 } 127 128 129 130 131 134 public int getMaximumSize() { 135 return max_size; 136 } 137 138 141 public int getStrength() { 142 return strength; 143 } 144 145 149 public int getDecomposition() { 150 return decomposition; 151 } 152 153 156 public Locale getLocale() { 157 return locale; 158 } 159 160 169 public String getLocaleString() { 170 if (locale == null) { 171 return ""; 172 } 173 else { 174 StringBuffer locale_str = new StringBuffer (); 175 locale_str.append(locale.getLanguage()); 176 locale_str.append(locale.getCountry()); 177 locale_str.append(locale.getVariant()); 178 return new String (locale_str); 179 } 180 } 181 182 187 private int lexicographicalOrder(StringAccessor str1, StringAccessor str2) { 188 long str1_size = str1.length(); 192 long str2_size = str2.length(); 193 if (str1_size < 32 * 1024 && 194 str2_size < 32 * 1024) { 195 return str1.toString().compareTo(str2.toString()); 196 } 197 198 long size = Math.min(str1_size, str2_size); 200 Reader r1 = str1.getReader(); 201 Reader r2 = str2.getReader(); 202 try { 203 try { 204 while (size > 0) { 205 int c1 = r1.read(); 206 int c2 = r2.read(); 207 if (c1 != c2) { 208 return c1 - c2; 209 } 210 --size; 211 } 212 if (str1_size > str2_size) { 214 return 1; 216 } 217 else if (str1_size < str2_size) { 218 return -1; 220 } 221 return 0; 223 } 224 finally { 225 r1.close(); 226 r2.close(); 227 } 228 } 229 catch (IOException e) { 230 throw new RuntimeException ("IO Error: " + e.getMessage()); 231 } 232 233 } 234 235 242 private synchronized Collator getCollator() { 243 if (collator != null) { 244 return collator; 245 } 246 else { 247 collator = Collator.getInstance(locale); 251 int strength = getStrength(); 252 int decomposition = getStrength(); 253 if (strength >= 0) { 254 collator.setStrength(strength); 255 } 256 if (decomposition >= 0) { 257 collator.setDecomposition(decomposition); 258 } 259 return collator; 260 } 261 } 262 263 265 272 public boolean comparableTypes(TType type) { 273 if (type instanceof TStringType) { 275 TStringType s_type = (TStringType) type; 276 if (getLocale() == null || s_type.getLocale() == null) { 278 return true; 279 } 280 return getLocale().equals(s_type.getLocale()); 282 } 283 return false; 284 } 285 286 public int compareObs(Object ob1, Object ob2) { 287 if (ob1 == ob2) { 288 return 0; 289 } 290 if (locale == null) { 292 return lexicographicalOrder((StringAccessor) ob1, (StringAccessor) ob2); 293 } 295 else { 296 return getCollator().compare(ob1.toString(), ob2.toString()); 297 } 298 } 299 300 public int calculateApproximateMemoryUse(Object ob) { 301 if (ob != null) { 302 return (((StringAccessor) ob).length() * 2) + 24; 303 } 304 else { 305 return 32; 306 } 307 } 308 309 public Class javaClass() { 310 return StringAccessor.class; 311 } 312 313 } 314 | Popular Tags |