forked from jasder/antlr
v4 tool:
* StringBuilder instead of StringBuffer * Add Utils.setSize for List<?> * Use Func1<T, TResult> "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]
This commit is contained in:
parent
3a8ab7f859
commit
26d5d5c9c0
|
@ -45,6 +45,14 @@ public class Utils {
|
||||||
boolean select(T t);
|
boolean select(T t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface Func0<TResult> {
|
||||||
|
TResult exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Func1<T1, TResult> {
|
||||||
|
TResult exec(T1 arg1);
|
||||||
|
}
|
||||||
|
|
||||||
static Integer[] ints = new Integer[INTEGER_POOL_MAX_VALUE+1];
|
static Integer[] ints = new Integer[INTEGER_POOL_MAX_VALUE+1];
|
||||||
|
|
||||||
/** Integer objects are immutable so share all Integers with the
|
/** 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).
|
uses regex (I only want to play with strings anyway).
|
||||||
*/
|
*/
|
||||||
public static String replace(String src, String replacee, String replacer) {
|
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 startIndex = 0;
|
||||||
int endIndex = src.indexOf(replacee);
|
int endIndex = src.indexOf(replacee);
|
||||||
while(endIndex != -1) {
|
while(endIndex != -1) {
|
||||||
|
@ -122,7 +130,7 @@ public class Utils {
|
||||||
String lines[] = s.split("\n");
|
String lines[] = s.split("\n");
|
||||||
Arrays.sort(lines);
|
Arrays.sort(lines);
|
||||||
List<String> linesL = Arrays.asList(lines);
|
List<String> linesL = Arrays.asList(lines);
|
||||||
StringBuffer buf = new StringBuffer();
|
StringBuilder buf = new StringBuilder();
|
||||||
for (String l : linesL) {
|
for (String l : linesL) {
|
||||||
buf.append(l);
|
buf.append(l);
|
||||||
buf.append('\n');
|
buf.append('\n');
|
||||||
|
@ -164,17 +172,11 @@ public class Utils {
|
||||||
/** apply methodName to list and return list of results. method has
|
/** apply methodName to list and return list of results. method has
|
||||||
* no args. This pulls data out of a list essentially.
|
* no args. This pulls data out of a list essentially.
|
||||||
*/
|
*/
|
||||||
public static <From,To> List<To> apply(List<From> list, String methodName) {
|
public static <From,To> List<To> select(List<From> list, Func1<From, To> selector) {
|
||||||
if ( list==null ) return null;
|
if ( list==null ) return null;
|
||||||
List<To> b = new ArrayList<To>();
|
List<To> b = new ArrayList<To>();
|
||||||
for (From f : list) {
|
for (From f : list) {
|
||||||
try {
|
b.add(method.exec(f));
|
||||||
Method m = f.getClass().getMethod(methodName, (Class[])null);
|
|
||||||
b.add( (To)m.invoke(f, (Object[])null) );
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
e.printStackTrace(System.err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
@ -202,4 +204,14 @@ public class Utils {
|
||||||
return -1;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue