From 26d5d5c9c0667c57fce2e7802e60719e8175d2b3 Mon Sep 17 00:00:00 2001 From: sharwell Date: Thu, 10 Nov 2011 07:58:00 -0800 Subject: [PATCH] v4 tool: * StringBuilder instead of StringBuffer * Add Utils.setSize for List * Use Func1 "delegate" instead of reflection for Utils.apply (and rename apply to select to suggest a return value) [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9272] --- tool/src/org/antlr/v4/misc/Utils.java | 32 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/tool/src/org/antlr/v4/misc/Utils.java b/tool/src/org/antlr/v4/misc/Utils.java index f714fc5e7..80dd85ba9 100644 --- a/tool/src/org/antlr/v4/misc/Utils.java +++ b/tool/src/org/antlr/v4/misc/Utils.java @@ -45,6 +45,14 @@ public class Utils { boolean select(T t); } + public interface Func0 { + TResult exec(); + } + + public interface Func1 { + TResult exec(T1 arg1); + } + static Integer[] ints = new Integer[INTEGER_POOL_MAX_VALUE+1]; /** Integer objects are immutable so share all Integers with the @@ -103,7 +111,7 @@ public class Utils { uses regex (I only want to play with strings anyway). */ public static String replace(String src, String replacee, String replacer) { - StringBuffer result = new StringBuffer(src.length() + 50); + StringBuilder result = new StringBuilder(src.length() + 50); int startIndex = 0; int endIndex = src.indexOf(replacee); while(endIndex != -1) { @@ -122,7 +130,7 @@ public class Utils { String lines[] = s.split("\n"); Arrays.sort(lines); List linesL = Arrays.asList(lines); - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); for (String l : linesL) { buf.append(l); buf.append('\n'); @@ -164,17 +172,11 @@ public class Utils { /** apply methodName to list and return list of results. method has * no args. This pulls data out of a list essentially. */ - public static List apply(List list, String methodName) { + public static List select(List list, Func1 selector) { if ( list==null ) return null; List b = new ArrayList(); for (From f : list) { - try { - Method m = f.getClass().getMethod(methodName, (Class[])null); - b.add( (To)m.invoke(f, (Object[])null) ); - } - catch (Exception e) { - e.printStackTrace(System.err); - } + b.add(method.exec(f)); } return b; } @@ -202,4 +204,14 @@ public class Utils { return -1; } + public static void setSize(List list, int size) { + if (size < list.size()) { + list.subList(size, list.size()).clear(); + } else { + while (size > list.size()) { + list.add(null); + } + } + } + }