1 35 package org.codehaus.groovy.runtime; 36 37 import groovy.lang.*; 38 import groovy.util.CharsetToolkit; 39 import groovy.util.ClosureComparator; 40 import groovy.util.OrderBy; 41 42 import java.io.*; 43 import java.lang.reflect.Array ; 44 import java.lang.reflect.Field ; 45 import java.lang.reflect.Modifier ; 46 import java.net.MalformedURLException ; 47 import java.net.ServerSocket ; 48 import java.net.Socket ; 49 import java.net.URL ; 50 import java.security.AccessController ; 51 import java.security.PrivilegedAction ; 52 import java.util.*; 53 import java.util.logging.Logger ; 54 import java.util.regex.Matcher ; 55 import java.util.regex.Pattern ; 56 57 70 public class DefaultGroovyMethods { 71 72 private static Logger log = Logger.getLogger(DefaultGroovyMethods.class.getName()); 73 74 private static final Integer ONE = new Integer (1); 75 private static final char ZERO_CHAR = '\u0000'; 76 77 86 public static Object getAt(Object self, String property) { 87 return InvokerHelper.getProperty(self, property); 88 } 89 90 98 public static void putAt(Object self, String property, Object newValue) { 99 InvokerHelper.setProperty(self, property, newValue); 100 } 101 102 106 public static String dump(Object self) { 107 if (self == null) { 108 return "null"; 109 } 110 StringBuffer buffer = new StringBuffer ("<"); 111 Class klass = self.getClass(); 112 buffer.append(klass.getName()); 113 buffer.append("@"); 114 buffer.append(Integer.toHexString(self.hashCode())); 115 boolean groovyObject = self instanceof GroovyObject; 116 117 121 while (klass != null) { 122 Field [] fields = klass.getDeclaredFields(); 123 for (int i = 0; i < fields.length; i++) { 124 final Field field = fields[i]; 125 if ((field.getModifiers() & Modifier.STATIC) == 0) { 126 if (groovyObject && field.getName().equals("metaClass")) { 127 continue; 128 } 129 AccessController.doPrivileged(new PrivilegedAction () { 130 public Object run() { 131 field.setAccessible(true); 132 return null; 133 } 134 }); 135 buffer.append(" "); 136 buffer.append(field.getName()); 137 buffer.append("="); 138 try { 139 buffer.append(InvokerHelper.toString(field.get(self))); 140 } catch (Exception e) { 141 buffer.append(e); 142 } 143 } 144 } 145 146 klass = klass.getSuperclass(); 147 } 148 149 154 174 175 buffer.append(">"); 176 return buffer.toString(); 177 } 178 179 public static void eachPropertyName(Object self, Closure closure) { 180 List props = allProperties(self); 181 for (Iterator itr = props.iterator(); itr.hasNext();) { 182 PropertyValue pv = (PropertyValue) itr.next(); 183 closure.call(pv.getName()); 184 } 185 } 186 187 public static void eachProperty(Object self, Closure closure) { 188 List props = allProperties(self); 189 for (Iterator itr = props.iterator(); itr.hasNext();) { 190 PropertyValue pv = (PropertyValue) itr.next(); 191 closure.call(pv); 192 } 193 } 194 195 public static List allProperties(Object self) { 196 List props = new ArrayList(); 197 MetaClass metaClass = InvokerHelper.getMetaClass(self); 198 199 List mps; 200 201 if (self instanceof groovy.util.Expando) { 202 mps = ((groovy.util.Expando) self).getProperties(); 203 } else { 204 mps = metaClass.getProperties(); 206 } 207 208 for (Iterator itr = mps.iterator(); itr.hasNext();) { 209 MetaProperty mp = (MetaProperty) itr.next(); 210 PropertyValue pv = new PropertyValue(self, mp); 211 props.add(pv); 212 } 213 214 return props; 215 } 216 217 220 public static void use(Object self, Class categoryClass, Closure closure) { 221 GroovyCategorySupport.use(categoryClass, closure); 222 } 223 224 227 public static void use(Object self, List categoryClassList, Closure closure) { 228 GroovyCategorySupport.use(categoryClassList, closure); 229 } 230 231 232 235 public static void print(Object self, Object value) { 236 System.out.print(InvokerHelper.toString(value)); 237 } 238 239 242 public static void println(Object self) { 243 System.out.println(); 244 } 245 246 249 public static void println(Object self, Object value) { 250 System.out.println(InvokerHelper.toString(value)); 251 } 252 253 257 public static String inspect(Object self) { 258 return InvokerHelper.inspect(self); 259 } 260 261 264 public static void print(Object self, PrintWriter out) { 265 if (out == null) { 266 out = new PrintWriter(System.out); 267 } 268 out.print(InvokerHelper.toString(self)); 269 } 270 271 276 public static void println(Object self, PrintWriter out) { 277 if (out == null) { 278 out = new PrintWriter(System.out); 279 } 280 InvokerHelper.invokeMethod(self, "print", out); 281 out.println(); 282 } 283 284 288 public static Object invokeMethod(Object object, String method, Object arguments) { 289 return InvokerHelper.invokeMethod(object, method, arguments); 290 } 291 292 public static boolean isCase(Object caseValue, Object switchValue) { 295 return caseValue.equals(switchValue); 296 } 297 298 public static boolean isCase(String caseValue, Object switchValue) { 299 if (switchValue == null) { 300 return caseValue == null; 301 } 302 return caseValue.equals(switchValue.toString()); 303 } 304 305 public static boolean isCase(Class caseValue, Object switchValue) { 306 return caseValue.isInstance(switchValue); 307 } 308 309 public static boolean isCase(Collection caseValue, Object switchValue) { 310 return caseValue.contains(switchValue); 311 } 312 313 public static boolean isCase(Pattern caseValue, Object switchValue) { 314 Matcher matcher = caseValue.matcher(switchValue.toString()); 315 if (matcher.matches()) { 316 RegexSupport.setLastMatcher(matcher); 317 return true; 318 } else { 319 return false; 320 } 321 } 322 323 326 332 public static void each(Object self, Closure closure) { 333 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 334 closure.call(iter.next()); 335 } 336 } 337 338 344 public static void eachWithIndex(Object self, Closure closure) { 345 int counter = 0; 346 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 347 closure.call(new Object []{iter.next(), new Integer (counter++)}); 348 } 349 } 350 351 357 public static void each(Collection self, Closure closure) { 358 for (Iterator iter = self.iterator(); iter.hasNext();) { 359 closure.call(iter.next()); 360 } 361 } 362 363 372 public static void each(Map self, Closure closure) { 373 if (closure.getParameterTypes().length == 2) { 374 for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { 375 Map.Entry entry = (Map.Entry) iter.next(); 376 closure.call(new Object []{entry.getKey(), entry.getValue()}); 377 } 378 } else { 379 for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { 380 closure.call(iter.next()); 381 } 382 } 383 } 384 385 393 public static boolean every(Object self, Closure closure) { 394 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 395 if (!InvokerHelper.asBool(closure.call(iter.next()))) { 396 return false; 397 } 398 } 399 return true; 400 } 401 402 409 public static boolean any(Object self, Closure closure) { 410 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 411 if (InvokerHelper.asBool(closure.call(iter.next()))) { 412 return true; 413 } 414 } 415 return false; 416 } 417 418 427 public static List grep(Object self, Object filter) { 428 List answer = new ArrayList(); 429 MetaClass metaClass = InvokerHelper.getMetaClass(filter); 430 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 431 Object object = iter.next(); 432 if (InvokerHelper.asBool(metaClass.invokeMethod(filter, "isCase", object))) { 433 answer.add(object); 434 } 435 } 436 return answer; 437 } 438 439 446 public static int count(Collection self, Object value) { 447 int answer = 0; 448 for (Iterator iter = self.iterator(); iter.hasNext();) { 449 if (InvokerHelper.compareEqual(iter.next(), value)) { 450 ++answer; 451 } 452 } 453 return answer; 454 } 455 456 462 public static List toList(Collection self) { 463 List answer = new ArrayList(self.size()); 464 answer.addAll(self); 465 return answer; 466 } 467 468 476 public static List collect(Object self, Closure closure) { 477 return (List) collect(self, new ArrayList(), closure); 478 } 479 480 489 public static Collection collect(Object self, Collection collection, Closure closure) { 490 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 491 collection.add(closure.call(iter.next())); 492 } 493 return collection; 494 } 495 496 504 public static List collect(Collection self, Closure closure) { 505 return (List) collect(self, new ArrayList(self.size()), closure); 506 } 507 508 517 public static Collection collect(Collection self, Collection collection, Closure closure) { 518 for (Iterator iter = self.iterator(); iter.hasNext();) { 519 collection.add(closure.call(iter.next())); 520 if (closure.getDirective() == Closure.DONE) { 521 break; 522 } 523 } 524 return collection; 525 } 526 527 535 public static Collection collect(Map self, Collection collection, Closure closure) { 536 for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { 537 collection.add(closure.call(iter.next())); 538 } 539 return collection; 540 } 541 542 551 public static List collect(Map self, Closure closure) { 552 return (List) collect(self, new ArrayList(self.size()), closure); 553 } 554 555 562 public static Object find(Object self, Closure closure) { 563 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 564 Object value = iter.next(); 565 if (InvokerHelper.asBool(closure.call(value))) { 566 return value; 567 } 568 } 569 return null; 570 } 571 572 579 public static Object find(Collection self, Closure closure) { 580 for (Iterator iter = self.iterator(); iter.hasNext();) { 581 Object value = iter.next(); 582 if (InvokerHelper.asBool(closure.call(value))) { 583 return value; 584 } 585 } 586 return null; 587 } 588 589 596 public static Object find(Map self, Closure closure) { 597 for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { 598 Object value = iter.next(); 599 if (InvokerHelper.asBool(closure.call(value))) { 600 return value; 601 } 602 } 603 return null; 604 } 605 606 613 public static List findAll(Object self, Closure closure) { 614 List answer = new ArrayList(); 615 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext();) { 616 Object value = iter.next(); 617 if (InvokerHelper.asBool(closure.call(value))) { 618 answer.add(value); 619 } 620 } 621 return answer; 622 } 623 624 631 public static List findAll(Collection self, Closure closure) { 632 List answer = new ArrayList(self.size()); 633 for (Iterator iter = self.iterator(); iter.hasNext();) { 634 Object value = iter.next(); 635 if (InvokerHelper.asBool(closure.call(value))) { 636 answer.add(value); 637 } 638 } 639 return answer; 640 } 641 642 649 public static List findAll(Map self, Closure closure) { 650 List answer = new ArrayList(self.size()); 651 for (Iterator iter = self.entrySet().iterator(); iter.hasNext();) { 652 Object value = iter.next(); 653 if (InvokerHelper.asBool(closure.call(value))) { 654 answer.add(value); 655 } 656 } 657 return answer; 658 } 659 660 670 public static Object inject(Collection self, Object value, Closure closure) { 671 Object [] params = new Object [2]; 672 for (Iterator iter = self.iterator(); iter.hasNext();) { 673 Object item = iter.next(); 674 params[0] = value; 675 params[1] = item; 676 value = closure.call(params); 677 } 678 return value; 679 } 680 681 688 public static String join(Collection self, String separator) { 689 StringBuffer buffer = new StringBuffer (); 690 boolean first = true; 691 for (Iterator iter = self.iterator(); iter.hasNext();) { 692 Object value = iter.next(); 693 if (first) { 694 first = false; 695 } else { 696 buffer.append(separator); 697 } 698 buffer.append(InvokerHelper.toString(value)); 699 } 700 return buffer.toString(); 701 } 702 703 710 public static String join(Object [] self, String separator) { 711 StringBuffer buffer = new StringBuffer (); 712 boolean first = true; 713 for (int i = 0; i < self.length; i++) { 714 String value = InvokerHelper.toString(self[i]); 715 if (first) { 716 first = false; 717 } else { 718 buffer.append(separator); 719 } 720 buffer.append(value); 721 } 722 return buffer.toString(); 723 } 724 725 731 public static Object max(Collection self) { 732 Object answer = null; 733 for (Iterator iter = self.iterator(); iter.hasNext();) { 734 Object value = iter.next(); 735 if (value != null) { 736 if (answer == null || InvokerHelper.compareGreaterThan(value, answer)) { 737 answer = value; 738 } 739 } 740 } 741 return answer; 742 } 743 744 751 public static Object max(Collection self, Comparator comparator) { 752 Object answer = null; 753 for (Iterator iter = self.iterator(); iter.hasNext();) { 754 Object value = iter.next(); 755 if (answer == null || comparator.compare(value, answer) > 0) { 756 answer = value; 757 } 758 } 759 return answer; 760 } 761 762 768 public static Object min(Collection self) { 769 Object answer = null; 770 for (Iterator iter = self.iterator(); iter.hasNext();) { 771 Object value = iter.next(); 772 if (value != null) { 773 if (answer == null || InvokerHelper.compareLessThan(value, answer)) { 774 answer = value; 775 } 776 } 777 } 778 return answer; 779 } 780 781 788 public static Object min(Collection self, Comparator comparator) { 789 Object answer = null; 790 for (Iterator iter = self.iterator(); iter.hasNext();) { 791 Object value = iter.next(); 792 if (answer == null || comparator.compare(value, answer) < 0) { 793 answer = value; 794 795 } 796 } 797 return answer; 798 } 799 800 807 public static Object min(Collection self, Closure closure) { 808 return min(self, new ClosureComparator(closure)); 809 } 810 811 818 public static Object max(Collection self, Closure closure) { 819 return max(self, new ClosureComparator(closure)); 820 } 821 822 828 public static int size(String text) { 829 return text.length(); 830 } 831 832 838 public static int size(Object [] self) { 839 return self.length; 840 } 841 842 849 public static CharSequence getAt(CharSequence text, int index) { 850 index = normaliseIndex(index, text.length()); 851 return text.subSequence(index, index + 1); 852 } 853 854 860 public static String getAt(String text, int index) { 861 index = normaliseIndex(index, text.length()); 862 return text.substring(index, index + 1); 863 } 864 865 872 public static CharSequence getAt(CharSequence text, Range range) { 873 int from = normaliseIndex(InvokerHelper.asInt(range.getFrom()), text.length()); 874 int to = normaliseIndex(InvokerHelper.asInt(range.getTo()), text.length()); 875 876 if (from > to) { 878 int tmp = from; 879 from = to; 880 to = tmp; 881 } 882 883 return text.subSequence(from, to + 1); 884 } 885 886 893 public static String getAt(String text, Range range) { 894 int from = normaliseIndex(InvokerHelper.asInt(range.getFrom()), text.length()); 895 int to = normaliseIndex(InvokerHelper.asInt(range.getTo()), text.length()); 896 897 boolean reverse = range.isReverse(); 899 if (from > to) { 900 int tmp = to; 901 to = from; 902 from = tmp; 903 reverse = !reverse; 904 } 905 906 String answer = text.substring(from, to + 1); 907 if (reverse) { 908 answer = reverse(answer); 909 } 910 return answer; 911 } 912 913 919 public static String reverse(String self) { 920 int size = self.length(); 921 StringBuffer buffer = new StringBuffer (size); 922 for (int i = size - 1; i >= 0; i--) { 923 buffer.append(self.charAt(i)); 924 } 925 return buffer.toString(); 926 } 927 928 935 public static URL toURL(String self) throws MalformedURLException { 936 return new URL (self); 937 } 938 939 private static String getPadding(String padding, int length) { 940 if (padding.length() < length) { 941 return multiply(padding, new Integer (length / padding.length() + 1)).substring(0, length); 942 } else { 943 return padding.substring(0, length); 944 } 945 } 946 947 954 public static String padLeft(String self, Number numberOfChars, String padding) { 955 int numChars = numberOfChars.intValue(); 956 if (numChars <= self.length()) { 957 return self; 958 } else { 959 return getPadding(padding, numChars - self.length()) + self; 960 } 961 } 962 963 969 970 public static String padLeft(String self, Number numberOfChars) { 971 return padLeft(self, numberOfChars, " "); 972 } 973 974 981 982 public static String padRight(String self, Number numberOfChars, String padding) { 983 int numChars = numberOfChars.intValue(); 984 if (numChars <= self.length()) { 985 return self; 986 } else { 987 return self + getPadding(padding, numChars - self.length()); 988 } 989 } 990 991 997 998 public static String padRight(String self, Number numberOfChars) { 999 return padRight(self, numberOfChars, " "); 1000 } 1001 1002 1009 public static String center(String self, Number numberOfChars, String padding) { 1010 int numChars = numberOfChars.intValue(); 1011 if (numChars <= self.length()) { 1012 return self; 1013 } else { 1014 int charsToAdd = numChars - self.length(); 1015 String semiPad = charsToAdd % 2 == 1 ? 1016 getPadding(padding, charsToAdd / 2 + 1) : 1017 getPadding(padding, charsToAdd / 2); 1018 if (charsToAdd % 2 == 0) 1019 return semiPad + self + semiPad; 1020 else 1021 return semiPad.substring(0, charsToAdd / 2) + self + semiPad; 1022 } 1023 } 1024 1025 1031 public static String center(String self, Number numberOfChars) { 1032 return center(self, numberOfChars, " "); 1033 } 1034 1035 1042 public static String getAt(Matcher matcher, int idx) { 1043 matcher.reset(); 1044 idx = normaliseIndex(idx, matcher.groupCount()); 1045 1046 if (matcher.groupCount() > 0) { 1048 matcher.find(); 1050 return matcher.group(idx); 1051 } else { 1052 for (int i = 0; i <= idx; i++) { 1055 matcher.find(); 1056 } 1057 return matcher.group(); 1058 } 1059 } 1060 1061 1068 public static List getAt(List self, Range range) { 1069 int size = self.size(); 1070 int from = normaliseIndex(InvokerHelper.asInt(range.getFrom()), size); 1071 int to = normaliseIndex(InvokerHelper.asInt(range.getTo()), size); 1072 boolean reverse = range.isReverse(); 1073 if (from > to) { 1074 int tmp = to; 1075 to = from; 1076 from = tmp; 1077 reverse = !reverse; 1078 } 1079 if (++to > size) { 1080 to = size; 1081 } 1082 List answer = self.subList(from, to); 1083 if (reverse) { 1084 answer = reverse(answer); 1085 } 1086 return answer; 1087 } 1088 1089 1096 public static List getAt(List self, Collection indices) { 1097 List answer = new ArrayList(indices.size()); 1098 for (Iterator iter = indices.iterator(); iter.hasNext();) { 1099 Object value = iter.next(); 1100 if (value instanceof Range) { 1101 answer.addAll(getAt(self, (Range) value)); 1102 } else if (value instanceof List) { 1103 answer.addAll(getAt(self, (List) value)); 1104 } else { 1105 int idx = InvokerHelper.asInt(value); 1106 answer.add(getAt(self, idx)); 1107 } 1108 } 1109 return answer; 1110 } 1111 1112 1119 public static List getAt(Object [] self, Collection indices) { 1120 List answer = new ArrayList(indices.size()); 1121 for (Iterator iter = indices.iterator(); iter.hasNext();) { 1122 Object value = iter.next(); 1123 if (value instanceof Range) { 1124 answer.addAll(getAt(self, (Range) value)); 1125 } else if (value instanceof Collection) { 1126 answer.addAll(getAt(self, (Collection) value)); 1127 } else { 1128 int idx = InvokerHelper.asInt(value); 1129 answer.add(getAt(self, idx)); 1130 } 1131 } 1132 return answer; 1133 } 1134 1135 1142 public static CharSequence getAt(CharSequence self, Collection indices) { 1143 StringBuffer answer = new StringBuffer (); 1144 for (Iterator iter = indices.iterator(); iter.hasNext();) { 1145 Object value = iter.next(); 1146 if (value instanceof Range) { 1147 answer.append(getAt(self, (Range) value)); 1148 } else if (value instanceof Collection) { 1149 answer.append(getAt(self, (Collection) value)); 1150 } else { 1151 int idx = InvokerHelper.asInt(value); 1152 answer.append(getAt(self, idx)); 1153 } 1154 } 1155 return answer.toString(); 1156 } 1157 1158 1165 public static String getAt(String self, Collection indices) { 1166 return (String ) getAt((CharSequence ) self, indices); 1167 } 1168 1169 1176 public static String getAt(Matcher self, Collection indices) { 1177 StringBuffer answer = new StringBuffer (); 1178 for (Iterator iter = indices.iterator(); iter.hasNext();) { 1179 Object value = iter.next(); 1180 if (value instanceof Range) { 1181 answer.append(getAt(self, (Range) value)); 1182 } else if (value instanceof Collection) { 1183 answer.append(getAt(self, (Collection) value)); 1184 } else { 1185 int idx = InvokerHelper.asInt(value); 1186 answer.append(getAt(self, idx)); 1187 } 1188 } 1189 return answer.toString(); 1190 } 1191 1192 1200 public static Map subMap(Map map, Collection keys) { 1201 Map answer = new HashMap(keys.size()); 1202 for (Iterator iter = keys.iterator(); iter.hasNext();) { 1203 Object key = iter.next(); 1204 answer.put(key, map.get(key)); 1205 } 1206 return answer; 1207 } 1208 1209 1221 public static Object get(Map map, Object key, Object defaultValue) { 1222 Object answer = map.get(key); 1223 if (answer == null) { 1224 answer = defaultValue; 1225 map.put(key, answer); 1226 } 1227 return answer; 1228 } 1229 1230 1238 public static List getAt(Object [] array, Range range) { 1239 List list = Arrays.asList(array); 1240 return getAt(list, range); 1241 } 1242 1243 1250 public static Object getAt(Object [] array, int idx) { 1251 return array[normaliseIndex(idx, array.length)]; 1252 } 1253 1254 1261 public static void putAt(Object [] array, int idx, Object value) { 1262 if (value instanceof Number ) { 1263 Class arrayComponentClass = array.getClass().getComponentType(); 1264 1265 if (!arrayComponentClass.equals(value.getClass())) { 1266 Object newVal = InvokerHelper.asType(value, arrayComponentClass); 1267 array[normaliseIndex(idx, array.length)] = newVal; 1268 return; 1269 } 1270 } 1271 array[normaliseIndex(idx, array.length)] = value; 1272 } 1273 1274 1280 public static List toList(Object [] array) { 1281 int size = array.length; 1282 List list = new ArrayList(size); 1283 for (int i = 0; i < size; i++) { 1284 list.add(array[i]); 1285 } 1286 return list; 1287 } 1288 1289 1296 public static Object getAt(List self, int idx) { 1297 int size = self.size(); 1298 int i = normaliseIndex(idx, size); 1299 if (i < size) { 1300 return self.get(i); 1301 } else { 1302 return null; 1303 } 1304 } 1305 1306 1313 public static void putAt(List self, int idx, Object value) { 1314 int size = self.size(); 1315 idx = normaliseIndex(idx, size); 1316 if (idx < size) { 1317 self.set(idx, value); 1318 } else { 1319 while (size < idx) { 1320 self.add(size++, null); 1321 } 1322 self.add(idx, value); 1323 } 1324 } 1325 1326 1333 public static Object getAt(Map self, Object key) { 1334 return self.get(key); 1335 } 1336 1337 1344 public static Object putAt(Map self, Object key, Object value) { 1345 return self.put(key, value); 1346 } 1347 1348 1355 protected static int normaliseIndex(int i, int size) { 1356 int temp = i; 1357 if (i < 0) { 1358 i += size; 1359 } 1360 if (i < 0) { 1361 throw new ArrayIndexOutOfBoundsException ("Negative array index [" + temp + "] too large for array size " + size); 1362 } 1363 return i; 1364 } 1365 1366 1373 public static List getAt(Collection coll, String property) { 1374 List answer = new ArrayList(coll.size()); 1375 for (Iterator iter = coll.iterator(); iter.hasNext();) { 1376 Object item = iter.next(); 1377 Object value = InvokerHelper.getProperty(item, property); 1378 if (value instanceof Collection) { 1379 answer.addAll((Collection) value); 1380 } else { 1381 answer.add(value); 1382 } 1383 } 1384 return answer; 1385 } 1386 1387 1393 public static Map asImmutable(Map self) { 1394 return Collections.unmodifiableMap(self); 1395 } 1396 1397 1403 public static SortedMap asImmutable(SortedMap self) { 1404 return Collections.unmodifiableSortedMap(self); 1405 } 1406 1407 1413 public static List asImmutable(List self) { 1414 return Collections.unmodifiableList(self); 1415 } 1416 1417 1423 public static Set asImmutable(Set self) { 1424 return Collections.unmodifiableSet(self); 1425 } 1426 1427 1433 public static SortedSet asImmutable(SortedSet self) { 1434 return Collections.unmodifiableSortedSet(self); 1435 } 1436 1437 1443 public static Collection asImmutable(Collection self) { 1444 return Collections.unmodifiableCollection(self); 1445 } 1446 1447 1453 public static Map asSynchronized(Map self) { 1454 return Collections.synchronizedMap(self); 1455 } 1456 1457 1463 public static SortedMap asSynchronized(SortedMap self) { 1464 return Collections.synchronizedSortedMap(self); 1465 } 1466 1467 1473 public static Collection asSynchronized(Collection self) { 1474 return Collections.synchronizedCollection(self); 1475 } 1476 1477 1483 public static List asSynchronized(List self) { 1484 return Collections.synchronizedList(self); 1485 } 1486 1487 1493 public static Set asSynchronized(Set self) { 1494 return Collections.synchronizedSet(self); 1495 } 1496 1497 1503 public static SortedSet asSynchronized(SortedSet self) { 1504 return Collections.synchronizedSortedSet(self); 1505 } 1506 1507 1513 public static List sort(Collection self) { 1514 List answer = asList(self); 1515 Collections.sort(answer); 1516 return answer; 1517 } 1518 1519 1525 public static SortedSet sort(SortedSet self) { 1526 return self; 1527 } 1528 1529 1535 public static List sort(List self) { 1536 Collections.sort(self); 1537 return self; 1538 } 1539 1540 1548 public static Object pop(List self) { 1549 if (self.isEmpty()) { 1550 throw new UnsupportedOperationException ("Cannot pop() an empty List"); 1551 } 1552 return self.remove(self.size() - 1); 1553 } 1554 1555 1562 public static List sort(List self, Comparator comparator) { 1563 Collections.sort(self, comparator); 1564 return self; 1565 } 1566 1567 1574 public static List sort(Collection self, Comparator comparator) { 1575 return sort(asList(self), comparator); 1576 } 1577 1578 1585 public static List sort(List self, Closure closure) { 1586 Class [] params = closure.getParameterTypes(); 1588 if (params.length == 1) { 1589 Collections.sort(self, new OrderBy(closure)); 1590 } else { 1591 Collections.sort(self, new ClosureComparator(closure)); 1592 } 1593 return self; 1594 } 1595 1596 1603 public static List sort(Collection self, Closure closure) { 1604 return sort(asList(self), closure); 1605 } 1606 1607 1613 public static List asList(Collection self) { 1614 if (self instanceof List) { 1615 return (List) self; 1616 } else { 1617 return new ArrayList(self); 1618 } 1619 } 1620 1621 1627 public static List reverse(List self) { 1628 int size = self.size(); 1629 List answer = new ArrayList(size); 1630 ListIterator iter = self.listIterator(size); 1631 while (iter.hasPrevious()) { 1632 answer.add(iter.previous()); 1633 } 1634 return answer; 1635 } 1636 1637 1644 public static List plus(Collection left, Collection right) { 1645 List answer = new ArrayList(left.size() + right.size()); 1646 answer.addAll(left); 1647 answer.addAll(right); 1648 return answer; 1649 } 1650 1651 1658 public static List plus(Collection left, Object right) { 1659 List answer = new ArrayList(left.size() + 1); 1660 answer.addAll(left); 1661 answer.add(right); 1662 return answer; 1663 } 1664 1665 1672 public static List multiply(Collection self, Number factor) { 1673 int size = factor.intValue(); 1674 List answer = new ArrayList(self.size() * size); 1675 for (int i = 0; i < size; i++) { 1676 answer.addAll(self); 1677 } 1678 return answer; 1679 } 1680 1681 1688 public static List intersect(List left, Collection right) { 1689 1690 if (left.size() == 0) 1691 return new ArrayList(); 1692 1693 boolean nlgnSort = sameType(new Collection[]{left, right}); 1694 1695 ArrayList result = new ArrayList(); 1696 Collection pickFrom = nlgnSort ? (Collection) new TreeSet(left) : left; 1698 1699 for (Iterator iter = right.iterator(); iter.hasNext();) { 1700 final Object o = iter.next(); 1701 if (pickFrom.contains(o)) 1702 result.add(o); 1703 } 1704 return result; 1705 } 1706 1707 1714 public static List minus(List self, Collection removeMe) { 1715 1716 if (self.size() == 0) 1717 return new ArrayList(); 1718 1719 boolean nlgnSort = sameType(new Collection[]{self, removeMe}); 1720 1721 1725 if (nlgnSort) { 1726 Set answer = new TreeSet(self); 1728 answer.removeAll(removeMe); 1729 return new ArrayList(answer); 1730 } else { 1731 List tmpAnswer = new LinkedList(self); 1733 for (Iterator iter = tmpAnswer.iterator(); iter.hasNext();) { 1734 Object element = iter.next(); 1735 for (Iterator iterator = removeMe.iterator(); iterator.hasNext();) { 1737 if (element.equals(iterator.next())) { 1738 iter.remove(); 1739 } 1740 } 1741 } 1742 List answer = new LinkedList(); 1745 Object [] array = tmpAnswer.toArray(new Object [tmpAnswer.size()]); 1746 1747 for (int i = 0; i < array.length; i++) { 1748 if (array[i] != null) { 1749 for (int j = i + 1; j < array.length; j++) { 1750 if (array[i].equals(array[j])) { 1751 array[j] = null; 1752 } 1753 } 1754 answer.add(array[i]); 1755 } 1756 } 1757 return new ArrayList(answer); 1758 } 1759 } 1760 1761 1767 public static List flatten(List self) { 1768 return new ArrayList(flatten(self, new LinkedList())); 1769 } 1770 1771 1777 public static void reverseEach(List self, Closure closure) { 1778 List reversed = reverse(self); 1779 for (Iterator iter = reversed.iterator(); iter.hasNext();) { 1780 closure.call(iter.next()); 1781 } 1782 } 1783 1784 private static List flatten(Collection elements, List addTo) { 1785 Iterator iter = elements.iterator(); 1786 while (iter.hasNext()) { 1787 Object element = iter.next(); 1788 if (element instanceof Collection) { 1789 flatten((Collection) element, addTo); 1790 } else if (element instanceof Map) { 1791 flatten(((Map) element).values(), addTo); 1792 } else { 1793 addTo.add(element); 1794 } 1795 } 1796 return addTo; 1797 } 1798 1799 1806 public static Collection leftShift(Collection self, Object value) { 1807 self.add(value); 1808 return self; 1809 } 1810 1811 1819 public static StringWriter leftShift(String self, Object value) { 1820 StringWriter answer = createStringWriter(self); 1821 try { 1822 leftShift(answer, value); 1823 } catch (IOException e) { 1824 throw new StringWriterIOException(e); 1825 } 1826 return answer; 1827 } 1828 1829 protected static StringWriter createStringWriter(String self) { 1830 StringWriter answer = new StringWriter(); 1831 answer.write(self); 1832 return answer; 1833 } 1834 1835 protected static StringBufferWriter createStringBufferWriter(StringBuffer self) { 1836 return new StringBufferWriter(self); 1837 } 1838 1839 1847 public static Writer leftShift(StringBuffer self, Object value) { 1848 StringBufferWriter answer = createStringBufferWriter(self); 1849 try { 1850 leftShift(answer, value); 1851 } catch (IOException e) { 1852 throw new StringWriterIOException(e); 1853 } 1854 return answer; 1855 } 1856 1857 1864 public static Writer leftShift(Writer self, Object value) throws IOException { 1865 InvokerHelper.write(self, value); 1866 return self; 1867 } 1868 1869 1873 public static Number leftShift(Number left, Number right) { 1874 return NumberMath.leftShift(left, right); 1875 } 1876 1877 1881 public static Number rightShift(Number left, Number right) { 1882 return NumberMath.rightShift(left, right); 1883 } 1884 1885 1889 public static Number rightShiftUnsigned(Number left, Number right) { 1890 return NumberMath.rightShiftUnsigned(left, right); 1891 } 1892 1893 1901 public static void write(Writer self, Writable writable) throws IOException { 1902 writable.writeTo(self); 1903 } 1904 1905 1912 public static Writer leftShift(OutputStream self, Object value) throws IOException { 1913 OutputStreamWriter writer = new FlushingStreamWriter(self); 1914 leftShift(writer, value); 1915 return writer; 1916 } 1917 1918 1925 public static OutputStream leftShift(OutputStream self, byte[] value) throws IOException { 1926 self.write(value); 1927 self.flush(); 1928 return self; 1929 } 1930 1931 private static boolean sameType(Collection[] cols) { 1932 List all = new LinkedList(); 1933 for (int i = 0; i < cols.length; i++) { 1934 all.addAll(cols[i]); 1935 } 1936 if (all.size() == 0) 1937 return true; 1938 1939 Object first = all.get(0); 1940 1941 Class baseClass; 1944 if (first instanceof Number ) { 1945 baseClass = Number .class; 1946 } else { 1947 baseClass = first.getClass(); 1948 } 1949 1950 for (int i = 0; i < cols.length; i++) { 1951 for (Iterator iter = cols[i].iterator(); iter.hasNext();) { 1952 if (!baseClass.isInstance(iter.next())) { 1953 return false; 1954 } 1955 } 1956 } 1957 return true; 1958 } 1959 1960 1963 public static Object getAt(byte[] array, int idx) { 1964 return primitiveArrayGet(array, idx); 1965 } 1966 1967 public static Object getAt(char[] array, int idx) { 1968 return primitiveArrayGet(array, idx); 1969 } 1970 1971 public static Object getAt(short[] array, int idx) { 1972 return primitiveArrayGet(array, idx); 1973 } 1974 1975 public static Object getAt(int[] array, int idx) { 1976 return primitiveArrayGet(array, idx); 1977 } 1978 1979 public static Object getAt(long[] array, int idx) { 1980 return primitiveArrayGet(array, idx); 1981 } 1982 1983 public static Object getAt(float[] array, int idx) { 1984 return primitiveArrayGet(array, idx); 1985 } 1986 1987 public static Object getAt(double[] array, int idx) { 1988 return primitiveArrayGet(array, idx); 1989 } 1990 1991 public static Object getAt(byte[] array, Range range) { 1992 return primitiveArrayGet(array, range); 1993 } 1994 1995 public static Object getAt(char[] array, Range range) { 1996 return primitiveArrayGet(array, range); 1997 } 1998 1999 public static Object getAt(short[] array, Range range) { 2000 return primitiveArrayGet(array, range); 2001 } 2002 2003 public static Object getAt(int[] array, Range range) { 2004 return primitiveArrayGet(array, range); 2005 } 2006 2007 public static Object getAt(long[] array, Range range) { 2008 return primitiveArrayGet(array, range); 2009 } 2010 2011 public static Object getAt(float[] array, Range range) { 2012 return primitiveArrayGet(array, range); 2013 } 2014 2015 public static Object getAt(double[] array, Range range) { 2016 return primitiveArrayGet(array, range); 2017 } 2018 2019 public static Object getAt(byte[] array, Collection indices) { 2020 return primitiveArrayGet(array, indices); 2021 } 2022 2023 public static Object getAt(char[] array, Collection indices) { 2024 return primitiveArrayGet(array, indices); 2025 } 2026 2027 public static Object getAt(short[] array, Collection indices) { 2028 return primitiveArrayGet(array, indices); 2029 } 2030 2031 public static Object getAt(int[] array, Collection indices) { 2032 return primitiveArrayGet(array, indices); 2033 } 2034 2035 public static Object getAt(long[] array, Collection indices) { 2036 return primitiveArrayGet(array, indices); 2037 } 2038 2039 public static Object getAt(float[] array, Collection indices) { 2040 return primitiveArrayGet(array, indices); 2041 } 2042 2043 public static Object getAt(double[] array, Collection indices) { 2044 return primitiveArrayGet(array, indices); 2045 } 2046 2047 public static void putAt(byte[] array, int idx, Object newValue) { 2048 primitiveArrayPut(array, idx, newValue); 2049 } 2050 2051 public static void putAt(char[] array, int idx, Object newValue) { 2052 primitiveArrayPut(array, idx, newValue); 2053 } 2054 2055 public static void putAt(short[] array, int idx, Object newValue) { 2056 primitiveArrayPut(array, idx, newValue); 2057 } 2058 2059 public static void putAt(int[] array, int idx, Object newValue) { 2060 primitiveArrayPut(array, idx, newValue); 2061 } 2062 2063 public static void putAt(long[] array, int idx, Object newValue) { 2064 primitiveArrayPut(array, idx, newValue); 2065 } 2066 2067 public static void putAt(float[] array, int idx, Object newValue) { 2068 primitiveArrayPut(array, idx, newValue); 2069 } 2070 2071 public static void putAt(double[] array, int idx, Object newValue) { 2072 primitiveArrayPut(array, idx, newValue); 2073 } 2074 2075 public static int size(byte[] array) { 2076 return Array.getLength(array); 2077 } 2078 2079 public static int size(char[] array) { 2080 return Array.getLength(array); 2081 } 2082 2083 public static int size(short[] array) { 2084 return Array.getLength(array); 2085 } 2086 2087 public static int size(int[] array) { 2088 return Array.getLength(array); 2089 } 2090 2091 public static int size(long[] array) { 2092 return Array.getLength(array); 2093 } 2094 2095 public static int size(float[] array) { 2096 return Array.getLength(array); 2097 } 2098 2099 public static int size(double[] array) { 2100 return Array.getLength(array); 2101 } 2102 2103 public static List toList(byte[] array) { 2104 return InvokerHelper.primitiveArrayToList(array); 2105 } 2106 2107 public static List toList(char[] array) { 2108 return InvokerHelper.primitiveArrayToList(array); 2109 } 2110 2111 public static List toList(short[] array) { 2112 return InvokerHelper.primitiveArrayToList(array); 2113 } 2114 2115 public static List toList(int[] array) { 2116 return InvokerHelper.primitiveArrayToList(array); 2117 } 2118 2119 public static List toList(long[] array) { 2120 return InvokerHelper.primitiveArrayToList(array); 2121 } 2122 2123 public static List toList(float[] array) { 2124 return InvokerHelper.primitiveArrayToList(array); 2125 } 2126 2127 public static List toList(double[] array) { 2128 return InvokerHelper.primitiveArrayToList(array); 2129 } 2130 2131 private static final char[] tTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray(); 2132 2133 public static Writable encodeBase64(final Byte [] data) { 2134 return encodeBase64(InvokerHelper.convertToByteArray(data)); 2135 } 2136 2137 2144 public static Writable encodeBase64(final byte[] data) { 2145 return new Writable() { 2146 public Writer writeTo(final Writer writer) throws IOException { 2147 int charCount = 0; 2148 final int dLimit = (data.length / 3) * 3; 2149 2150 for (int dIndex = 0; dIndex != dLimit; dIndex += 3) { 2151 int d = ((data[dIndex] & 0XFF) << 16) | ((data[dIndex + 1] & 0XFF) << 8) | (data[dIndex + 2] & 0XFF); 2152 2153 writer.write(tTable[d >> 18]); 2154 writer.write(tTable[(d >> 12) & 0X3F]); 2155 writer.write(tTable[(d >> 6) & 0X3F]); 2156 writer.write(tTable[d & 0X3F]); 2157 2158 if (++charCount == 18) { 2159 writer.write('\n'); 2160 charCount = 0; 2161 } 2162 } 2163 2164 if (dLimit != data.length) { 2165 int d = (data[dLimit] & 0XFF) << 16; 2166 2167 if (dLimit + 1 != data.length) { 2168 d |= (data[dLimit + 1] & 0XFF) << 8; 2169 } 2170 2171 writer.write(tTable[d >> 18]); 2172 writer.write(tTable[(d >> 12) & 0X3F]); 2173 writer.write((dLimit + 1 < data.length) ? tTable[(d >> 6) & 0X3F] : '='); 2174 writer.write('='); 2175 } 2176 2177 return writer; 2178 } 2179 2180 public String toString() { 2181 StringWriter buffer = new StringWriter(); 2182 2183 try { 2184 writeTo(buffer); 2185 } catch (IOException e) { 2186 throw new RuntimeException (e); } 2188 2189 return buffer.toString(); 2190 } 2191 }; 2192 } 2193 2194 private static final byte[] translateTable = ( 2195 "\u0042\u0042\u0042\u0042\u0042\u0042\u0042\u0042" 2197 + "\u0042\u0042\u0041\u0041\u0042\u0042\u0041\u0042" 2199 + "\u0042\u0042\u0042\u0042\u0042\u0042\u0042\u0042" 2201 + "\u0042\u0042\u0042\u0042\u0042\u0042\u0042\u0042" 2203 + "\u0041\u0042\u0042\u0042\u0042\u0042\u0042\u0042" 2205 + "\u0042\u0042\u0042\u003E\u0042\u0042\u0042\u003F" 2207 + "\u0034\u0035\u0036\u0037\u0038\u0039\u003A\u003B" 2209 + "\u003C\u003D\u0042\u0042\u0042\u0040\u0042\u0042" 2211 + "\u0042\u0000\u0001\u0002\u0003\u0004\u0005\u0006" 2213 + "\u0007\u0008\t\n\u000B\u000C\r\u000E" 2215 + "\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016" 2217 + "\u0017\u0018\u0019\u0042\u0042\u0042\u0042\u0042" 2219 + "\u0042\u001A\u001B\u001C\u001D\u001E\u001F\u0020" 2221 + "\u0021\"\u0023\u0024\u0025\u0026\u0027\u0028" 2223 + "\u0029\u002A\u002B\u002C\u002D\u002E\u002F\u0030" 2225 + "\u0031\u0032\u0033").getBytes(); 2227 2228 2234 public static byte[] decodeBase64(final String value) { 2235 int byteShift = 4; 2236 int tmp = 0; 2237 boolean done = false; 2238 final StringBuffer buffer = new StringBuffer (); 2239 2240 for (int i = 0; i != value.length(); i++) { 2241 final char c = value.charAt(i); 2242 final int sixBit = (c < 123) ? translateTable[c] : 66; 2243 2244 if (sixBit < 64) { 2245 if (done) throw new RuntimeException ("= character not at end of base64 value"); 2247 tmp = (tmp << 6) | sixBit; 2248 2249 if (byteShift-- != 4) { 2250 buffer.append((char) ((tmp >> (byteShift * 2)) & 0XFF)); 2251 } 2252 2253 } else if (sixBit == 64) { 2254 2255 byteShift--; 2256 done = true; 2257 2258 } else if (sixBit == 66) { 2259 throw new RuntimeException ("bad character in base64 value"); } 2264 2265 if (byteShift == 0) byteShift = 4; 2266 } 2267 2268 try { 2269 return buffer.toString().getBytes("ISO-8859-1"); 2270 } catch (UnsupportedEncodingException e) { 2271 throw new RuntimeException ("Base 64 decode produced byte values > 255"); } 2273 } 2274 2275 2278 protected static Object primitiveArrayGet(Object array, int idx) { 2279 return Array.get(array, normaliseIndex(idx, Array.getLength(array))); 2280 } 2281 2282 2285 protected static List primitiveArrayGet(Object array, Range range) { 2286 List answer = new ArrayList(); 2287 for (Iterator iter = range.iterator(); iter.hasNext();) { 2288 int idx = InvokerHelper.asInt(iter.next()); 2289 answer.add(primitiveArrayGet(array, idx)); 2290 } 2291 return answer; 2292 } 2293 2294 2297 protected static List primitiveArrayGet(Object self, Collection indices) { 2298 List answer = new ArrayList(); 2299 for (Iterator iter = indices.iterator(); iter.hasNext();) { 2300 Object value = iter.next(); 2301 if (value instanceof Range) { 2302 answer.addAll(primitiveArrayGet(self, (Range) value)); 2303 } else if (value instanceof List) { 2304 answer.addAll(primitiveArrayGet(self, (List) value)); 2305 } else { 2306 int idx = InvokerHelper.asInt(value); 2307 answer.add(primitiveArrayGet(self, idx)); 2308 } 2309 } 2310 return answer; 2311 } 2312 2313 2316 protected static void primitiveArrayPut(Object array, int idx, Object newValue) { 2317 Array.set(array, normaliseIndex(idx, Array.getLength(array)), newValue); 2318 } 2319 2320 2323 2330 public static Character toCharacter(String self) { 2331 2332 return new Character (self.charAt(0)); 2333 } 2334 2335 2342 public static List tokenize(String self, String token) { 2343 return InvokerHelper.asList(new StringTokenizer(self, token)); 2344 } 2345 2346 2352 public static List tokenize(String self) { 2353 return InvokerHelper.asList(new StringTokenizer(self)); 2354 } 2355 2356 2363 public static String plus(String left, Object value) { 2364 return left + toString(value); 2366 } 2367 2368 2375 public static String plus(Number value, String right) { 2376 return toString(value) + right; 2377 } 2378 2379 2386 public static String plus(StringBuffer left, String value) { 2387 return left + value; 2388 } 2389 2390 2391 2398 public static String minus(String left, Object value) { 2399 String text = toString(value); 2400 return left.replaceFirst(text, ""); 2401 } 2402 2403 2411 public static boolean contains(String self, String text) { 2412 int idx = self.indexOf(text); 2413 return idx >= 0; 2414 } 2415 2416 2423 public static int count(String self, String text) { 2424 int answer = 0; 2425 for (int idx = 0; true; idx++) { 2426 idx = self.indexOf(text, idx); 2427 if (idx >= 0) { 2428 ++answer; 2429 } else { 2430 break; 2431 } 2432 } 2433 return answer; 2434 } 2435 2436 2444 public static String next(String self) { 2445 StringBuffer buffer = new StringBuffer (self); 2446 char firstCh = firstCharacter(); 2447 for (int idx = buffer.length() - 1; idx >= 0; idx--) { 2448 char ch = next(buffer.charAt(idx)); 2449 if (ch != ZERO_CHAR) { 2450 buffer.setCharAt(idx, ch); 2451 break; 2452 } else { 2453 if (idx == 0) { 2455 buffer.append("1"); 2456 } else { 2457 buffer.setCharAt(idx, firstCh); 2458 } 2459 } 2460 } 2461 return buffer.toString(); 2462 } 2463 2464 2472 public static String previous(String self) { 2473 StringBuffer buffer = new StringBuffer (self); 2474 char lastCh = lastCharacter(); 2475 for (int idx = buffer.length() - 1; idx >= 0; idx--) { 2476 char ch = previous(buffer.charAt(idx)); 2477 if (ch != ZERO_CHAR) { 2478 buffer.setCharAt(idx, ch); 2479 break; 2480 } else { 2481 if (idx == 0) { 2482 return null; 2483 } else { 2484 buffer.setCharAt(idx, lastCh); 2486 } 2487 } 2488 } 2489 return buffer.toString(); 2490 } 2491 2492 2499 public static Process execute(String self) throws IOException { 2500 return Runtime.getRuntime().exec(self); 2501 } 2502 2503 private static char next(char ch) { 2504 if (Character.isLetterOrDigit(++ch)) { 2505 return ch; 2506 } else { 2507 return ZERO_CHAR; 2508 } 2509 } 2510 2511 private static char previous(char ch) { 2512 if (Character.isLetterOrDigit(--ch)) { 2513 return ch; 2514 } else { 2515 return ZERO_CHAR; 2516 } 2517 } 2518 2519 2522 private static char firstCharacter() { 2523 char ch = ZERO_CHAR; 2524 while (!Character.isLetterOrDigit(ch)) { 2525 ch++; 2526 } 2527 return ch; 2528 } 2529 2530 2533 private static char lastCharacter() { 2534 char ch = firstCharacter(); 2535 while (Character.isLetterOrDigit(++ch)) ; 2536 return --ch; 2537 } 2538 2539 2547 public static String multiply(String self, Number factor) { 2548 int size = factor.intValue(); 2549 if (size == 0) 2550 return ""; 2551 else if (size < 0) { 2552 throw new IllegalArgumentException ("multiply() should be called with a number of 0 or greater not: " + size); 2553 } 2554 StringBuffer answer = new StringBuffer (self); 2555 for (int i = 1; i < size; i++) { 2556 answer.append(self); 2557 } 2558 return answer.toString(); 2559 } 2560 2561 protected static String toString(Object value) { 2562 return (value == null) ? "null" : value.toString(); 2563 } 2564 2565 2568 2574 public static Number next(Character self) { 2575 return plus(self, ONE); 2576 } 2577 2578 2584 public static Number next(Number self) { 2585 return plus(self, ONE); 2586 } 2587 2588 2594 public static Number previous(Character self) { 2595 return minus(self, ONE); 2596 } 2597 2598 2604 public static Number previous(Number self) { 2605 return minus(self, ONE); 2606 } 2607 2608 2615 public static Number plus(Character left, Number right) { 2616 return plus(new Integer (left.charValue()), right); 2617 } 2618 2619 2626 public static Number plus(Number left, Character right) { 2627 return plus(left, new Integer (right.charValue())); 2628 } 2629 2630 2637 public static Number plus(Character left, Character right) { 2638 return plus(new Integer (left.charValue()), right); 2639 } 2640 2641 2648 public static Number plus(Number left, Number right) { 2649 return NumberMath.add(left, right); 2650 } 2651 2652 2659 public static int compareTo(Character left, Number right) { 2660 return compareTo(new Integer (left.charValue()), right); 2661 } 2662 2663 2670 public static int compareTo(Number left, Character right) { 2671 return compareTo(left, new Integer (right.charValue())); 2672 } 2673 2674 2681 public static int compareTo(Character left, Character right) { 2682 return compareTo(new Integer (left.charValue()), right); 2683 } 2684 2685 2692 public static int compareTo(Number left, Number right) { 2693 2694 return NumberMath.compareTo(left, right); 2695 } 2696 2697 2704 public static Number minus(Character left, Number right) { 2705 return minus(new Integer (left.charValue()), right); 2706 } 2707 2708 2715 public static Number minus(Number left, Character right) { 2716 return minus(left, new Integer (right.charValue())); 2717 } 2718 2719 2726 public static Number minus(Character left, Character right) { 2727 return minus(new Integer (left.charValue()), right); 2728 } 2729 2730 2737 public static Number minus(Number left, Number right) { 2738 return NumberMath.subtract(left, right); 2739 } 2740 2741 2748 public static Number multiply(Character left, Number right) { 2749 return multiply(new Integer (left.charValue()), right); 2750 } 2751 2752 2759 public static Number multiply(Number left, Character right) { 2760 return multiply(left, new Integer (right.charValue())); 2761 } 2762 2763 2770 public static Number multiply(Character left, Character right) { 2771 return multiply(new Integer (left.charValue()), right); 2772 } 2773 2774 2781 public static Number multiply(Number left, Number right) { 2784 return NumberMath.multiply(left, right); 2785 } 2786 2787 2794 public static Number power(Number self, Number exponent) { 2795 double answer = Math.pow(self.doubleValue(), exponent.doubleValue()); 2796 if (NumberMath.isFloatingPoint(self) || NumberMath.isFloatingPoint(exponent) || answer < 1) { 2797 return new Double (answer); 2798 } else if (NumberMath.isLong(self) || NumberMath.isLong(exponent) || answer > Integer.MAX_VALUE) { 2799 return new Long ((long) answer); 2800 } else { 2801 return new Integer ((int) answer); 2802 } 2803 } 2804 2805 2812 public static Number div(Character left, Number right) { 2813 return div(new Integer (left.charValue()), right); 2814 } 2815 2816 2823 public static Number div(Number left, Character right) { 2824 return div(left, new Integer (right.charValue())); 2825 } 2826 2827 2834 public static Number div(Character left, Character right) { 2835 return div(new Integer (left.charValue()), right); 2836 } 2837 2838 2845 public static Number div(Number left, Number right) { 2848 return NumberMath.divide(left, right); 2849 } 2850 2851 2858 public static Number intdiv(Character left, Number right) { 2859 return intdiv(new Integer (left.charValue()), right); 2860 } 2861 2862 2869 public static Number intdiv(Number left, Character right) { 2870 return intdiv(left, new Integer (right.charValue())); 2871 } 2872 2873 2880 public static Number intdiv(Character left, Character right) { 2881 return intdiv(new Integer (left.charValue()), right); 2882 } 2883 2884 2891 public static Number intdiv(Number left, Number right) { 2892 return NumberMath.intdiv(left, right); 2893 } 2894 2895 2902 public static Number or(Number left, Number right) { 2903 return NumberMath.or(left, right); 2904 } 2905 2906 2913 public static Number and(Number left, Number right) { 2914 return NumberMath.and(left, right); 2915 } 2916 2917 2924 public static Number mod(Number left, Number right) { 2925 return NumberMath.mod(left, right); 2926 } 2927 2928 2934 public static Number negate(Number left) { 2935 return NumberMath.negate(left); 2936 } 2937 2938 2939 2945 public static void times(Number self, Closure closure) { 2946 for (int i = 0, size = self.intValue(); i < size; i++) { 2947 closure.call(new Integer (i)); 2948 if (closure.getDirective() == Closure.DONE) { 2949 break; 2950 } 2951 } 2952 } 2953 2954 2961 public static void upto(Number self, Number to, Closure closure) { 2962 for (int i = self.intValue(), size = to.intValue(); i <= size; i++) { 2963 closure.call(new Integer (i)); 2964 } 2965 } 2966 2967 2975 public static void step(Number self, Number to, Number stepNumber, Closure closure) { 2976 for (int i = self.intValue(), size = to.intValue(), step = stepNumber.intValue(); i < size; i += step) { 2977 closure.call(new Integer (i)); 2978 } 2979 } 2980 2981 2987 public static int abs(Number number) { 2990 return Math.abs(number.intValue()); 2991 } 2992 2993 2999 public static long abs(Long number) { 3000 return Math.abs(number.longValue()); 3001 } 3002 3003 3009 public static float abs(Float number) { 3010 return Math.abs(number.floatValue()); 3011 } 3012 3013 3019 public static double abs(Double number) { 3020 return Math.abs(number.doubleValue()); 3021 } 3022 3023 3029 public static int round(Float number) { 3030 return Math.round(number.floatValue()); 3031 } 3032 3033 3039 public static long round(Double number) { 3040 return Math.round(number.doubleValue()); 3041 } 3042 3043 3049 public static Integer toInteger(String self) { 3050 return Integer.valueOf(self); 3051 } 3052 3053 3059 public static Long toLong(String self) { 3060 return Long.valueOf(self); 3061 } 3062 3063 3069 public static Float toFloat(String self) { 3070 return Float.valueOf(self); 3071 } 3072 3073 3079 public static Double toDouble(String self) { 3080 return Double.valueOf(self); 3081 } 3082 3083 3089 public static Integer toInteger(Number self) { 3090 return new Integer (self.intValue()); 3091 } 3092 3093 3096 3102 public static Date next(Date self) { 3103 return plus(self, 1); 3104 } 3105 3106 3112 public static Date previous(Date self) { 3113 return minus(self, 1); 3114 } 3115 3116 3123 public static Date plus(Date self, int days) { 3124 Calendar calendar = (Calendar) Calendar.getInstance().clone(); 3125 calendar.setTime(self); 3126 calendar.add(Calendar.DAY_OF_YEAR, days); 3127 return calendar.getTime(); 3128 } 3129 3130 3136 public static Date minus(Date self, int days) { 3137 return plus(self, -days); 3138 } 3139 3140 3143 3150 public static void eachLine(File self, Closure closure) throws IOException { 3151 eachLine(newReader(self), closure); 3152 } 3153 3154 3161 public static void eachLine(Reader self, Closure closure) throws IOException { 3162 BufferedReader br = null; 3163 3164 if (self instanceof BufferedReader) 3165 br = (BufferedReader) self; 3166 else 3167 br = new BufferedReader(self); 3168 3169 try { 3170 while (true) { 3171 String line = br.readLine(); 3172 if (line == null) { 3173 break; 3174 } else { 3175 closure.call(line); 3176 } 3177 } 3178 br.close(); 3179 } catch (IOException e) { 3180 if (self != null) { 3181 try { 3182 br.close(); 3183 } catch (Exception e2) { 3184 } 3186 throw e; 3187 } 3188 } 3189 } 3190 3191 3199 public static void splitEachLine(File self, String sep, Closure closure) throws IOException { 3200 splitEachLine(newReader(self), sep, closure); 3201 } 3202 3203 3211 public static void splitEachLine(Reader self, String sep, Closure closure) throws IOException { 3212 BufferedReader br = null; 3213 3214 if (self instanceof BufferedReader) 3215 br = (BufferedReader) self; 3216 else 3217 br = new BufferedReader(self); 3218 3219 List args = new ArrayList(); 3220 3221 try { 3222 while (true) { 3223 String line = br.readLine(); 3224 if (line == null) { 3225 break; 3226 } else { 3227 List vals = Arrays.asList(line.split(sep)); 3228 args.clear(); 3229 args.add(vals); 3230 closure.call(args); 3231 } 3232 } 3233 br.close(); 3234 } catch (IOException e) { 3235 if (self != null) { 3236 try { 3237 br.close(); 3238 } catch (Exception e2) { 3239 } 3241 throw e; 3242 } 3243 } 3244 } 3245 3246 3253 public static String readLine(Reader self) throws IOException { 3254 BufferedReader br = null; 3255 3256 if (self instanceof BufferedReader) { 3257 br = (BufferedReader) self; 3258 } else { 3259 br = new BufferedReader(self); 3260 } 3261 return br.readLine(); 3262 } 3263 3264 3271 public static String readLine(InputStream stream) throws IOException { 3272 return readLine(new InputStreamReader(stream)); 3273 } 3274 3275 3282 public static List readLines(File file) throws IOException { 3283 IteratorClosureAdapter closure = new IteratorClosureAdapter(file); 3284 eachLine(file, closure); 3285 return closure.asList(); 3286 } 3287 3288 3296 public static String getText(File file, String charset) throws IOException { 3297 BufferedReader reader = newReader(file, charset); 3298 return getText(reader); 3299 } 3300 3301 3308 public static String getText(File file) throws IOException { 3309 BufferedReader reader = newReader(file); 3310 return getText(reader); 3311 } 3312 3313 3320 public static String getText(URL url) throws IOException { 3321 return getText(url, CharsetToolkit.getDefaultSystemCharset().toString()); 3322 } 3323 3324 3332 public static String getText(URL url, String charset) throws IOException { 3333 BufferedReader reader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), charset)); 3334 return getText(reader); 3335 } 3336 3337 3344 public static String getText(InputStream is) throws IOException { 3345 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 3346 return getText(reader); 3347 } 3348 3349 3357 public static String getText(InputStream is, String charset) throws IOException { 3358 BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset)); 3359 return getText(reader); 3360 } 3361 3362 3369 public static String getText(Reader reader) throws IOException { 3370 BufferedReader bufferedReader = new BufferedReader(reader); 3371 return getText(bufferedReader); 3372 } 3373 3374 3381 public static String getText(BufferedReader reader) throws IOException { 3382 StringBuffer answer = new StringBuffer (); 3383 char[] charBuffer = new char[4096]; 3385 int nbCharRead = 0; 3386 while ((nbCharRead = reader.read(charBuffer)) != -1) { 3387 answer.append(charBuffer, 0, nbCharRead); 3389 } 3390 reader.close(); 3391 return answer.toString(); 3392 } 3393 3394 3401 public static void writeLine(BufferedWriter writer, String line) throws IOException { 3402 writer.write(line); 3403 writer.newLine(); 3404 } 3405 3406 3413 public static void write(File file, String text) throws IOException { 3414 BufferedWriter writer = newWriter(file); 3415 writer.write(text); 3416 writer.close(); 3417 } 3418 3419 3427 public static void write(File file, String text, String charset) throws IOException { 3428 BufferedWriter writer = newWriter(file, charset); 3429 writer.write(text); 3430 writer.close(); 3431 } 3432 3433 3440 public static void append(File file, String text) throws IOException { 3441 BufferedWriter writer = newWriter(file, true); 3442 writer.write(text); 3443 writer.close(); 3444 } 3445 3446 3454 public static void append(File file, String text, String charset) throws IOException { 3455 BufferedWriter writer = newWriter(file, charset, true); 3456 writer.write(text); 3457 writer.close(); 3458 } 3459 3460 3467 public static List readLines(Reader reader) throws IOException { 3468 IteratorClosureAdapter closure = new IteratorClosureAdapter(reader); 3469 eachLine(reader, closure); 3470 return closure.asList(); 3471 } 3472 3473 3479 public static void eachFile(File self, Closure closure) { 3480 File[] files = self.listFiles(); 3481 for (int i = 0; i < files.length; i++) { 3482 closure.call(files[i]); 3483 } 3484 } 3485 3486 3493 public static void eachFileRecurse(File self, Closure closure) { 3494 File[] files = self.listFiles(); 3495 for (int i = 0; i < files.length; i++) { 3496 if (files[i].isDirectory()) { 3497 closure.call(files[i]); 3498 eachFileRecurse(files[i], closure); 3499 } else { 3500 closure.call(files[i]); 3501 } 3502 } 3503 } 3504 3505 3512 public static BufferedReader newReader(File file) throws IOException { 3513 CharsetToolkit toolkit = new CharsetToolkit(file); 3514 return toolkit.getReader(); 3515 } 3516 3517 3526 public static BufferedReader newReader(File file, String charset) 3527 throws FileNotFoundException, UnsupportedEncodingException { 3528 return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); 3529 } 3530 3531 3537 public static BufferedReader newReader(final InputStream self) { 3538 return new BufferedReader(new InputStreamReader(self)); 3539 } 3540 3541 3548 public static void withReader(File file, Closure closure) throws IOException { 3549 withReader(newReader(file), closure); 3550 } 3551 3552 3559 public static BufferedOutputStream newOutputStream(File file) throws IOException { 3560 return new BufferedOutputStream(new FileOutputStream(file)); 3561 } 3562 3563 3570 public static void withOutputStream(File file, Closure closure) throws IOException { 3571 withStream(newOutputStream(file), closure); 3572 } 3573 3574 3581 public static void withInputStream(File file, Closure closure) throws IOException { 3582 withStream(newInputStream(file), closure); 3583 } 3584 3585 3592 public static BufferedWriter newWriter(File file) throws IOException { 3593 return new BufferedWriter(new FileWriter(file)); 3594 } 3595 3596 3604 public static BufferedWriter newWriter(File file, boolean append) throws IOException { 3605 return new BufferedWriter(new FileWriter(file, append)); 3606 } 3607 3608 3617 public static BufferedWriter newWriter(File file, String charset, boolean append) throws IOException { 3618 if (append) { 3619 return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), charset)); 3620 } else { 3621 FileOutputStream stream = new FileOutputStream(file); 3623 if ("UTF-16BE".equals(charset)) { 3624 writeUtf16Bom(stream, true); 3625 } else if ("UTF-16LE".equals(charset)) { 3626 writeUtf16Bom(stream, false); 3627 } 3628 return new BufferedWriter(new OutputStreamWriter(stream, charset)); 3629 } 3630 } 3631 3632 3640 public static BufferedWriter newWriter(File file, String charset) throws IOException { 3641 return newWriter(file, charset, false); 3642 } 3643 3644 3651 private static void writeUtf16Bom(FileOutputStream stream, boolean bigEndian) throws IOException { 3652 if (bigEndian) { 3653 stream.write(-2); 3654 stream.write(-1); 3655 } else { 3656 stream.write(-1); 3657 stream.write(-2); 3658 } 3659 } 3660 3661 3669 public static void withWriter(File file, Closure closure) throws IOException { 3670 withWriter(newWriter(file), closure); 3671 } 3672 3673 3682 public static void withWriter(File file, String charset, Closure closure) throws IOException { 3683 withWriter(newWriter(file, charset), closure); 3684 } 3685 3686 3695 public static void withWriterAppend(File file, String charset, Closure closure) throws IOException { 3696 withWriter(newWriter(file, charset, true), closure); 3697 } 3698 3699 3705 public static PrintWriter newPrintWriter(File file) throws IOException { 3706 return new PrintWriter(newWriter(file)); 3707 } 3708 3709 3717 public static PrintWriter newPrintWriter(File file, String charset) throws IOException { 3718 return new PrintWriter(newWriter(file, charset)); 3719 } 3720 3721 3728 public static void withPrintWriter(File file, Closure closure) throws IOException { 3729 withWriter(newPrintWriter(file), closure); 3730 } 3731 3732 3741 public static void withWriter(Writer writer, Closure closure) throws IOException { 3742 try { 3743 closure.call(writer); 3744 3745 Writer temp = writer; 3748 writer = null; 3749 temp.close(); 3750 } finally { 3751 if (writer != null) { 3752 try { 3753 writer.close(); 3754 } catch (IOException e) { 3755 log.warning("Caught exception closing writer: " + e); 3756 } 3757 } 3758 } 3759 } 3760 3761 3770 public static void withReader(Reader writer, Closure closure) throws IOException { 3771 try { 3772 closure.call(writer); 3773 3774 Reader temp = writer; 3777 writer = null; 3778 temp.close(); 3779 } finally { 3780 if (writer != null) { 3781 try { 3782 writer.close(); 3783 } catch (IOException e) { 3784 log.warning("Caught exception closing writer: " + e); 3785 } 3786 } 3787 } 3788 } 3789 3790 3799 public static void withStream(InputStream stream, Closure closure) throws IOException { 3800 try { 3801 closure.call(stream); 3802 3803 InputStream temp = stream; 3806 stream = null; 3807 temp.close(); 3808 } finally { 3809 if (stream != null) { 3810 try { 3811 stream.close(); 3812 } catch (IOException e) { 3813 log.warning("Caught exception closing stream: " + e); 3814 } 3815 } 3816 } 3817 } 3818 3819 3826 public static List readLines(InputStream stream) throws IOException { 3827 return readLines(new BufferedReader(new InputStreamReader(stream))); 3828 } 3829 3830 3837 public static void eachLine(InputStream stream, Closure closure) throws IOException { 3838 eachLine(new InputStreamReader(stream), closure); 3839 } 3840 3841 3848 public static void eachLine(URL url, Closure closure) throws IOException { 3849 eachLine(url.openConnection().getInputStream(), closure); 3850 } 3851 3852 3859 public static void withReader(URL url, Closure closure) throws IOException { 3860 withReader(url.openConnection().getInputStream(), closure); 3861 } 3862 3863 3870 public static void withReader(InputStream in, Closure closure) throws IOException { 3871 withReader(new InputStreamReader(in), closure); 3872 } 3873 3874 3883 public static void withWriter(OutputStream stream, Closure closure) throws IOException { 3884 withWriter(new OutputStreamWriter(stream), closure); 3885 } 3886 3887 3897 public static void withWriter(OutputStream stream, String charset, Closure closure) throws IOException { 3898 withWriter(new OutputStreamWriter(stream, charset), closure); 3899 } 3900 3901 3910 public static void withStream(OutputStream stream, Closure closure) throws IOException { 3911 try { 3912 closure.call(stream); 3913 3914 OutputStream temp = stream; 3917 stream = null; 3918 temp.close(); 3919 } finally { 3920 if (stream != null) { 3921 try { 3922 stream.close(); 3923 } catch (IOException e) { 3924 log.warning("Caught exception closing stream: " + e); 3925 } 3926 } 3927 } 3928 } 3929 3930 3937 public static BufferedInputStream newInputStream(File file) throws FileNotFoundException { 3938 return new BufferedInputStream(new FileInputStream(file)); 3939 } 3940 3941 3947 public static void eachByte(File self, Closure closure) throws IOException { 3948 BufferedInputStream is = newInputStream(self); 3949 eachByte(is, closure); 3950 } 3951 3952 3959 public static void eachByte(InputStream is, Closure closure) throws IOException { 3960 try { 3961 while (true) { 3962 int b = is.read(); 3963 if (b == -1) { 3964 break; 3965 } else { 3966 closure.call(new Byte ((byte) b)); 3967 } 3968 } 3969 is.close(); 3970 } catch (IOException e) { 3971 if (is != null) { 3972 try { 3973 is.close(); 3974 } catch (Exception e2) { 3975 } 3977 throw e; 3978 } 3979 } 3980 } 3981 3982 3989 public static void eachByte(URL url, Closure closure) throws IOException { 3990 InputStream is = url.openConnection().getInputStream(); 3991 eachByte(is, closure); 3992 } 3993 3994 4001 public static void transformChar(Reader reader, Writer writer, Closure closure) { 4002 int c; 4003 try { 4004 char[] chars = new char[1]; 4005 while ((c = reader.read()) != -1) { 4006 chars[0] = (char) c; 4007 writer.write((String ) closure.call(new String (chars))); 4008 } 4009 } catch (IOException e) { 4010 } 4011 } 4012 4013 4020 public static void transformLine(Reader reader, Writer writer, Closure closure) throws IOException { 4021 BufferedReader br = new BufferedReader(reader); 4022 BufferedWriter bw = new BufferedWriter(writer); 4023 String line; 4024 while ((line = br.readLine()) != null) { 4025 Object o = closure.call(line); 4026 if (o != null) { 4027 bw.write(o.toString()); 4028 bw.newLine(); 4029 } 4030 } 4031 } 4032 4033 4042 public static void filterLine(Reader reader, Writer writer, Closure closure) throws IOException { 4043 BufferedReader br = new BufferedReader(reader); 4044 BufferedWriter bw = new BufferedWriter(writer); 4045 String line; 4046 while ((line = br.readLine()) != null) { 4047 if (InvokerHelper.asBool(closure.call(line))) { 4048 bw.write(line); 4049 bw.newLine(); 4050 } 4051 } 4052 bw.flush(); 4053 } 4054 4055 4063 public static Writable filterLine(final File self, final Closure closure) throws IOException { 4064 return filterLine(newReader(self), closure); 4065 } 4066 4067 4076 public static void filterLine(final File self, final Writer writer, final Closure closure) throws IOException { 4077 filterLine(newReader(self), writer, closure); 4078 } 4079 4080 4087 public static Writable filterLine(Reader reader, final Closure closure) { 4088 final BufferedReader br = new BufferedReader(reader); 4089 return new Writable() { 4090 public Writer writeTo(Writer out) throws IOException { 4091 BufferedWriter bw = new BufferedWriter(out); 4092 String line; 4093 while ((line = br.readLine()) != null) { 4094 if (InvokerHelper.asBool(closure.call(line))) { 4095 bw.write(line); 4096 bw.newLine(); 4097 } 4098 } 4099 bw.flush(); 4100 return out; 4101 } 4102 4103 public String toString() { 4104 StringWriter buffer = new StringWriter(); 4105 try { 4106 writeTo(buffer); 4107 } catch (IOException e) { 4108 throw new RuntimeException (e); } 4110 return buffer.toString(); 4111 } 4112 }; 4113 } 4114 4115 4122 public static Writable filterLine(final InputStream self, final Closure predicate) { 4123 return filterLine(newReader(self), predicate); 4124 } 4125 4126 4134 public static void filterLine(final InputStream self, final Writer writer, final Closure predicate) 4135 throws IOException { 4136 filterLine(newReader(self), writer, predicate); 4137 } 4138 4139 4145 public static byte[] readBytes(File file) throws IOException { 4146 byte[] bytes = new byte[(int) file.length()]; 4147 FileInputStream fileInputStream = new FileInputStream(file); 4148 DataInputStream dis = new DataInputStream(fileInputStream); 4149 dis.readFully(bytes); 4150 dis.close(); 4151 return bytes; 4152 } 4153 4154 4155 4156 4159 4168 public static void withStreams(Socket socket, Closure closure) throws IOException { 4169 InputStream input = socket.getInputStream(); 4170 OutputStream output = socket.getOutputStream(); 4171 try { 4172 closure.call(new Object []{input, output}); 4173 } finally { 4174 try { 4175 input.close(); 4176 } catch (IOException e) { 4177 } 4179 try { 4180 output.close(); 4181 } catch (IOException e) { 4182 } 4184 } 4185 } 4186 4187 4195 public static Writer leftShift(Socket self, Object value) throws IOException { 4196 return leftShift(self.getOutputStream(), value); 4197 } 4198 4199 4207 public static OutputStream leftShift(Socket self, byte[] value) throws IOException { 4208 return leftShift(self.getOutputStream(), value); 4209 } 4210 4211 4219 public static Socket accept(ServerSocket serverSocket, final Closure closure) throws IOException { 4220 final Socket socket = serverSocket.accept(); 4221 new Thread (new Runnable () { 4222 public void run() { 4223 try { 4224 closure.call(socket); 4225 } finally { 4226 try { 4227 socket.close(); 4228 } catch (IOException e) { 4229 } 4231 } 4232 } 4233 }).start(); 4234 return socket; 4235 } 4236 4237 4238 4242 public static File asWritable(File file) { 4243 return new WritableFile(file); 4244 } 4245 4246 4251 public static File asWritable(File file, String encoding) { 4252 return new WritableFile(file, encoding); 4253 } 4254 4255 4261 public static List toList(String self) { 4262 int size = self.length(); 4263 List answer = new ArrayList(size); 4264 for (int i = 0; i < size; i++) { 4265 answer.add(self.substring(i, i + 1)); 4266 } 4267 return answer; 4268 } 4269 4270 4273 4279 public static InputStream getIn(Process self) { 4280 return self.getInputStream(); 4281 } 4282 4283 4290 public static String getText(Process self) throws IOException { 4291 return getText(new BufferedReader(new InputStreamReader(self.getInputStream()))); 4292 } 4293 4294 4300 public static InputStream getErr(Process self) { 4301 return self.getErrorStream(); 4302 } 4303 4304 4310 public static OutputStream getOut(Process self) { 4311 return self.getOutputStream(); 4312 } 4313 4314 4322 public static Writer leftShift(Process self, Object value) throws IOException { 4323 return leftShift(self.getOutputStream(), value); 4324 } 4325 4326 4334 public static OutputStream leftShift(Process self, byte[] value) throws IOException { 4335 return leftShift(self.getOutputStream(), value); 4336 } 4337 4338 4344 public static void waitForOrKill(Process self, long numberOfMillis) { 4345 ProcessRunner runnable = new ProcessRunner(self); 4346 Thread thread = new Thread (runnable); 4347 thread.start(); 4348 runnable.waitForOrKill(numberOfMillis); 4349 } 4350 4351 4361 public static void eachMatch(String str, String regex, Closure closure) { 4362 Pattern p = Pattern.compile(regex); 4363 Matcher m = p.matcher(str); 4364 while (m.find()) { 4365 int count = m.groupCount(); 4366 ArrayList groups = new ArrayList(); 4367 for (int i = 0; i <= count; i++) { 4368 groups.add(m.group(i)); 4369 } 4370 closure.call(groups); 4371 } 4372 } 4373 4374 public static void each(Matcher matcher, Closure closure) { 4375 Matcher m = matcher; 4376 while (m.find()) { 4377 int count = m.groupCount(); 4378 ArrayList groups = new ArrayList(); 4379 for (int i = 0; i <= count; i++) { 4380 groups.add(m.group(i)); 4381 } 4382 closure.call(groups); 4383 } 4384 } 4385 4386 4394 public static int findIndexOf(Object self, Closure closure) { 4395 int i = 0; 4396 for (Iterator iter = InvokerHelper.asIterator(self); iter.hasNext(); i++) { 4397 Object value = iter.next(); 4398 if (InvokerHelper.asBool(closure.call(value))) { 4399 break; 4400 } 4401 } 4402 return i; 4403 } 4404 4405 4410 protected static class ProcessRunner implements Runnable { 4411 Process process; 4412 private boolean finished; 4413 4414 public ProcessRunner(Process process) { 4415 this.process = process; 4416 } 4417 4418 public void run() { 4419 try { 4420 process.waitFor(); 4421 } catch (InterruptedException e) { 4422 } 4423 synchronized (this) { 4424 notifyAll(); 4425 finished = true; 4426 } 4427 } 4428 4429 public synchronized void waitForOrKill(long millis) { 4430 if (!finished) { 4431 try { 4432 wait(millis); 4433 } catch (InterruptedException e) { 4434 } 4435 if (!finished) { 4436 process.destroy(); 4437 } 4438 } 4439 } 4440 } 4441} 4442 | Popular Tags |