MCPcopy Create free account
hub / github.com/antlr/codebuff / trunc

Method trunc

output/java/1.4.14/Interpreter.java:1038–1056  ·  view source on GitHub ↗

Return all but the last element. trunc( x )==null if x is single-valued.

(InstanceScope scope, Object v)

Source from the content-addressed store, hash-verified

1036 /** Return all but the last element. <code>trunc(<i>x</i>)==null</code> if <code><i>x</i></code> is single-valued. */
1037
1038 public Object trunc(InstanceScope scope, Object v) {
1039 if ( v==null ) return null;
1040 if ( v instanceof List) { // optimize list case
1041 List<?> elems = (List<?>)v;
1042 if ( elems.size()<= 1) return null;
1043 return elems.subList(0, elems.size()-1);
1044 }
1045 v = convertAnythingIteratableToIterator(scope, v);
1046 if ( v instanceof Iterator) {
1047 List<Object> a = new ArrayList<Object>();
1048 Iterator<?> it = (Iterator<?>)v;
1049 while ( it.hasNext() ) {
1050 Object o = it.next();
1051 if ( it.hasNext() ) a.add(o); // only add if not last one
1052 }
1053 return a;
1054 }
1055 return null; // trunc(x)==null when x single-valued attribute
1056 }
1057
1058 /** Return a new list without {@code null} values. */
1059

Callers 1

_execMethod · 0.95

Calls 6

sizeMethod · 0.65
nextMethod · 0.65
addMethod · 0.65
subListMethod · 0.45
hasNextMethod · 0.45

Tested by

no test coverage detected