private final IRubyObject at(ThreadContext context, int i) { final Ruby runtime = context.runtime; final Object array = getObject(); final int length = Array.getLength(array); if ( i < 0 ) i = i + length; if ( i >= 0 && i < length ) { return ArrayUtils.arefDirect(runtime, array, converter, i); } return context.nil; }
public IRubyObject asetDirect(Ruby runtime, int intIndex, IRubyObject value) { return ArrayUtils.asetDirect(runtime, getValue(), javaConverter, intIndex, value); }
/** * The slow version for when concatenating a Java array of a different type. * * @param context * @param original * @param additional * @return */ public IRubyObject concatArrays(ThreadContext context, JavaArray original, IRubyObject additional) { return ArrayUtils.concatArraysDirect(context, original.getValue(), additional); }
public static void setWithExceptionHandlingDirect(Ruby runtime, Object array, int index, Object javaValue) { try { Array.set(array, index, javaValue); } catch (IndexOutOfBoundsException e) { throw mapIndexOutOfBoundsException(runtime, array, index); } catch (ArrayStoreException e) { throw mapArrayStoreException(runtime, array, javaValue.getClass()); } catch (IllegalArgumentException e) { throw mapIllegalArgumentException(runtime, array, javaValue.getClass()); } }
private IRubyObject arrayRange(final ThreadContext context, final IRubyObject rFirst, final IRubyObject rLength) { final Object array = getObject(); final int arrayLength = Array.getLength( array ); if ( rFirst instanceof RubyFixnum && rLength instanceof RubyFixnum ) { int first = (int) ((RubyFixnum) rFirst).getLongValue(); int length = (int) ((RubyFixnum) rLength).getLongValue(); if ( length > arrayLength ) { throw context.runtime.newIndexError("length specifed is longer than array"); } if ( length <= 0 ) { return ArrayUtils.emptyJavaArrayDirect(context, array.getClass().getComponentType()); } first = first >= 0 ? first : arrayLength + first; return ArrayUtils.javaArraySubarrayDirect(context, array, first, length); } throw context.runtime.newTypeError("only Fixnum ranges supported"); }
public static IRubyObject emptyJavaArrayDirect(ThreadContext context, Class componentType) { Ruby runtime = context.runtime; return newProxiedArray(runtime, componentType, 0); }
public final Object javaArrayFromRubyArrayDirect(ThreadContext context, RubyArray fromArray) { final Ruby runtime = context.runtime; final Class<?> type = javaClass(); final Object newArray = Array.newInstance(type, fromArray.size()); if ( type.isArray() ) { // if it's an array of arrays, recurse with the component type for ( int i = 0; i < fromArray.size(); i++ ) { final Class<?> nestedType = type.getComponentType(); final IRubyObject element = fromArray.eltInternal(i); final Object nestedArray; if ( element instanceof RubyArray ) { // recurse JavaClass componentType = JavaClass.get(runtime, nestedType); nestedArray = componentType.javaArrayFromRubyArrayDirect(context, element); } else if ( type.isInstance(element) ) { nestedArray = element; } else { // still try (nested) toJava conversion : nestedArray = element.toJava(type); } ArrayUtils.setWithExceptionHandlingDirect(runtime, newArray, i, nestedArray); } } else { ArrayUtils.copyDataToJavaArrayDirect(fromArray, newArray); } return newArray; }
public final void fillWithExceptionHandling(int start, int end, Object javaValue) { final Ruby runtime = getRuntime(); final Object array = getValue(); for (int i = start; i < end; i++) { ArrayUtils.setWithExceptionHandlingDirect(runtime, array, i, javaValue); } } }
@Deprecated public static void copyDataToJavaArrayDirect(ThreadContext context, final RubyArray rubyArray, final Object javaArray) { copyDataToJavaArrayDirect(rubyArray, javaArray); }
public IRubyObject emptyJavaArray(ThreadContext context) { return ArrayUtils.emptyJavaArrayDirect(context, javaClass()); }
public IRubyObject javaArraySubarray(ThreadContext context, JavaArray fromArray, int index, int size) { return ArrayUtils.javaArraySubarrayDirect(context, getValue(), index, size); }
private IRubyObject arrayRange(final ThreadContext context, final IRubyObject rFirst, final IRubyObject rLength) { final Object array = getObject(); final int arrayLength = Array.getLength( array ); if ( rFirst instanceof RubyFixnum && rLength instanceof RubyFixnum ) { int first = (int) ((RubyFixnum) rFirst).getLongValue(); int length = (int) ((RubyFixnum) rLength).getLongValue(); if ( length > arrayLength ) { throw context.runtime.newIndexError("length specifed is longer than array"); } if ( length <= 0 ) { return ArrayUtils.emptyJavaArrayDirect(context, array.getClass().getComponentType()); } first = first >= 0 ? first : arrayLength + first; return ArrayUtils.javaArraySubarrayDirect(context, array, first, length); } throw context.runtime.newTypeError("only Fixnum ranges supported"); }
public static IRubyObject emptyJavaArrayDirect(ThreadContext context, Class componentType) { Ruby runtime = context.runtime; return newProxiedArray(runtime, componentType, 0); }
public final Object javaArrayFromRubyArrayDirect(ThreadContext context, RubyArray fromArray) { final Ruby runtime = context.runtime; final Class<?> type = javaClass(); final Object newArray = Array.newInstance(type, fromArray.size()); if ( type.isArray() ) { // if it's an array of arrays, recurse with the component type for ( int i = 0; i < fromArray.size(); i++ ) { final Class<?> nestedType = type.getComponentType(); final IRubyObject element = fromArray.eltInternal(i); final Object nestedArray; if ( element instanceof RubyArray ) { // recurse JavaClass componentType = JavaClass.get(runtime, nestedType); nestedArray = componentType.javaArrayFromRubyArrayDirect(context, element); } else if ( type.isInstance(element) ) { nestedArray = element; } else { // still try (nested) toJava conversion : nestedArray = element.toJava(type); } ArrayUtils.setWithExceptionHandlingDirect(runtime, newArray, i, nestedArray); } } else { ArrayUtils.copyDataToJavaArrayDirect(fromArray, newArray); } return newArray; }
public final void fillWithExceptionHandling(int start, int end, Object javaValue) { final Ruby runtime = getRuntime(); final Object array = getValue(); for (int i = start; i < end; i++) { ArrayUtils.setWithExceptionHandlingDirect(runtime, array, i, javaValue); } } }
public static void setWithExceptionHandlingDirect(Ruby runtime, Object array, int index, Object javaValue) { try { Array.set(array, index, javaValue); } catch (IndexOutOfBoundsException e) { throw mapIndexOutOfBoundsException(runtime, array, index); } catch (ArrayStoreException e) { throw mapArrayStoreException(runtime, array, javaValue.getClass()); } catch (IllegalArgumentException e) { throw mapIllegalArgumentException(runtime, array, javaValue.getClass()); } }
@Deprecated public static void copyDataToJavaArrayDirect(ThreadContext context, final RubyArray rubyArray, final Object javaArray) { copyDataToJavaArrayDirect(rubyArray, javaArray); }
public IRubyObject emptyJavaArray(ThreadContext context) { return ArrayUtils.emptyJavaArrayDirect(context, javaClass()); }
public IRubyObject javaArraySubarray(ThreadContext context, JavaArray fromArray, int index, int size) { return ArrayUtils.javaArraySubarrayDirect(context, getValue(), index, size); }
public IRubyObject getRange(ThreadContext context, IRubyObject firstObj, IRubyObject lengthObj) { if (firstObj instanceof RubyFixnum && lengthObj instanceof RubyFixnum) { int first = (int)((RubyFixnum)firstObj).getLongValue(); int length = (int)((RubyFixnum)lengthObj).getLongValue(); if (length > Array.getLength(getObject())) { throw context.runtime.newIndexError("length specifed is longer than array"); } first = first >= 0 ? first : Array.getLength(getObject()) + first; if (length <= 0) { return ArrayUtils.emptyJavaArrayDirect(context, getObject().getClass().getComponentType()); } return ArrayUtils.javaArraySubarrayDirect(context, getObject(), first, length); } else { throw context.runtime.newTypeError("only Fixnum ranges supported"); } }