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