public static List createRange(Object from, Object to, boolean inclusive) throws Throwable { if (from instanceof Integer && to instanceof Integer) { int ifrom = (Integer) from; int ito = (Integer) to; if (inclusive || ifrom != ito) { return new IntRange(inclusive, ifrom, ito); } // else fall through for EmptyRange } if (!inclusive && compareEqual(from, to)) { return new EmptyRange((Comparable) from); } if (from instanceof Number && to instanceof Number) { return new NumberRange(comparableNumber((Number) from), comparableNumber((Number) to), inclusive); } if (!inclusive) { if (compareGreaterThan(from, to)) { to = invokeMethod0(ScriptBytecodeAdapter.class, to, "next"); } else { to = invokeMethod0(ScriptBytecodeAdapter.class, to, "previous"); } } return new ObjectRange((Comparable) from, (Comparable) to); }
/** * A helper method to allow lists to work with subscript operators. * <pre class="groovyTestCase">def list = ["a", true] * list[1..<1] = 5 * assert list == ["a", 5, true]</pre> * * @param self a List * @param range the (in this case empty) subset of the list to set * @param value the values to put at the given sublist or a Collection of values * @since 1.0 */ public static void putAt(List self, EmptyRange range, Object value) { RangeInfo info = subListBorders(self.size(), range); List sublist = self.subList(info.from, info.to); sublist.clear(); if (value instanceof Collection) { Collection col = (Collection) value; if (col.isEmpty()) return; sublist.addAll(col); } else { sublist.add(value); } }
protected static RangeInfo subListBorders(int size, EmptyRange range) { int from = normaliseIndex(DefaultTypeTransformation.intUnbox(range.getFrom()), size); return new RangeInfo(from, from, false); }
@Deprecated public static CharSequence getAt(CharSequence text, EmptyRange range) { return StringGroovyMethods.getAt(text, range); }
@Deprecated public static String getAt(String text, EmptyRange range) { return StringGroovyMethods.getAt(text, range); }
@Deprecated public static void putAt(StringBuffer self, EmptyRange range, Object value) { StringGroovyMethods.putAt(self, range, value); }
/** * @deprecated Use the CharSequence version * @see #getAt(CharSequence, groovy.lang.EmptyRange) */ @Deprecated public static String getAt(String text, EmptyRange range) { return getAt((CharSequence) text, range); }
/** * Support the range subscript operator for an eager or lazy List. * <pre class="groovyTestCase">def list = [true, 1, 3.4].withDefault{ 42 } * assert list[0..<0] == []</pre> * * @param self a ListWithDefault * @param range a Range indicating the items to get * * @return a new list instance based on range borders * */ public static <T> List<T> getAt(ListWithDefault<T> self, EmptyRange range) { return ListWithDefault.newInstance(new ArrayList<T>(), self.isLazyDefaultValues(), self.getInitClosure()); }
/** * Support the range subscript operator for a List. * <pre class="groovyTestCase">def list = [true, 1, 3.4] * assert list[0..<0] == []</pre> * * @param self a List * @param range a Range indicating the items to get * @return a new list instance based on range borders * * @since 1.0 */ public static <T> List<T> getAt(List<T> self, EmptyRange range) { return createSimilarList(self, 0); }
/** * * @param array an Array of Objects * @param range an EmptyRange * @return an empty Range * @since 1.5.0 */ public static <T> List<T> getAt(T[] array, EmptyRange range) { return new ArrayList<T>(); }
/** * A helper method to allow lists to work with subscript operators. * <pre class="groovyTestCase">def list = ["a", true] * list[1..<1] = [4, 3, 2] * assert list == ["a", 4, 3, 2, true]</pre> * * @param self a List * @param range the (in this case empty) subset of the list to set * @param value the Collection of values * @since 1.0 * @see #putAt(java.util.List, groovy.lang.EmptyRange, java.lang.Object) */ public static void putAt(List self, EmptyRange range, Collection value) { putAt(self, range, (Object)value); }
/** * Support the range subscript operator for CharSequence or StringBuffer with EmptyRange * * @param text a CharSequence * @param range an EmptyRange * @return the empty String * @since 1.5.0 */ public static String getAt(CharSequence text, EmptyRange range) { return ""; }
/** * Support the range subscript operator for StringBuffer. * * @param self a StringBuffer * @param range a Range * @param value the object that's toString() will be inserted * @since 1.0 */ public static void putAt(StringBuffer self, EmptyRange range, Object value) { RangeInfo info = subListBorders(self.length(), range); self.replace(info.from, info.to, value.toString()); }
/** * Support the range subscript operator for StringBuilder. * * @param self a StringBuilder * @param range a Range * @param value the object that's toString() will be inserted */ public static void putAt(StringBuilder self, EmptyRange range, Object value) { RangeInfo info = subListBorders(self.length(), range); self.replace(info.from, info.to, value.toString()); }