Return all but the last element. trunc( x )==null if x is single-valued.
(InstanceScope scope, Object v)
| 1012 | /** Return all but the last element. <code>trunc(<i>x</i>)==null</code> if <code><i>x</i></code> is single-valued. */ |
| 1013 | |
| 1014 | public Object trunc(InstanceScope scope, Object v) { |
| 1015 | if ( v==null ) return null; |
| 1016 | if ( v instanceof List ) { // optimize list case |
| 1017 | List<?> elems = (List<?>)v; |
| 1018 | if ( elems.size()<=1) return null; |
| 1019 | return elems.subList(0, elems.size()-1); |
| 1020 | } |
| 1021 | v = convertAnythingIteratableToIterator(scope, v); |
| 1022 | if ( v instanceof Iterator ) { |
| 1023 | List<Object> a = new ArrayList<Object>(); |
| 1024 | Iterator<?> it = (Iterator<?>)v; |
| 1025 | while ( it.hasNext() ) { |
| 1026 | Object o = it.next(); |
| 1027 | if ( it.hasNext() ) a.add(o); // only add if not last one |
| 1028 | } |
| 1029 | return a; |
| 1030 | } |
| 1031 | return null; // trunc(x)==null when x single-valued attribute |
| 1032 | } |
| 1033 | |
| 1034 | /** Return a new list without {@code null} values. */ |
| 1035 |