Return all but the last element. trunc( x )==null if x is single-valued.
(InstanceScope scope, Object v)
| 989 | /** Return all but the last element. <code>trunc(<i>x</i>)==null</code> if <code><i>x</i></code> is single-valued. */ |
| 990 | |
| 991 | public Object trunc(InstanceScope scope, Object v) { |
| 992 | if ( v==null ) return null; |
| 993 | if ( v instanceof List ) { // optimize list case |
| 994 | List<?> elems = (List<?>)v; |
| 995 | if ( elems.size()<=1) return null; |
| 996 | return elems.subList(0, elems.size()-1); |
| 997 | } |
| 998 | v = convertAnythingIteratableToIterator(scope, v); |
| 999 | if ( v instanceof Iterator ) { |
| 1000 | List<Object> a = new ArrayList<Object>(); |
| 1001 | Iterator<?> it = (Iterator<?>)v; |
| 1002 | while ( it.hasNext() ) { |
| 1003 | Object o = it.next(); |
| 1004 | if ( it.hasNext() ) a.add(o); // only add if not last one |
| 1005 | } |
| 1006 | return a; |
| 1007 | } |
| 1008 | return null; // trunc(x)==null when x single-valued attribute |
| 1009 | } |
| 1010 | |
| 1011 | /** Return a new list without {@code null} values. */ |
| 1012 |