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