From ce114bc8872ad00540fd92b35bde89ec53a411af Mon Sep 17 00:00:00 2001 From: "Thomas E. Enebo" Date: Sun, 10 Nov 2024 15:45:53 -0600 Subject: [PATCH] Some small refactoring while looking for example snippet code of new API --- core/src/main/java/org/jruby/RubyRange.java | 80 ++++++++----------- core/src/main/java/org/jruby/api/Convert.java | 21 +++++ 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/core/src/main/java/org/jruby/RubyRange.java b/core/src/main/java/org/jruby/RubyRange.java index a1f1c1792b8..fa2b015976d 100644 --- a/core/src/main/java/org/jruby/RubyRange.java +++ b/core/src/main/java/org/jruby/RubyRange.java @@ -373,11 +373,9 @@ private static RubyString inspectValue(final ThreadContext context, IRubyObject } private static IRubyObject inspectValueRecursive(ThreadContext context, IRubyObject state, IRubyObject obj, boolean recur) { - if (recur) { - return RubyString.newString(context.runtime, ((RubyRange) obj).isExclusive ? "(... ... ...)" : "(... .. ...)"); - } else { - return inspect(context, obj); - } + return recur ? + RubyString.newString(context.runtime, ((RubyRange) obj).isExclusive ? "(... ... ...)" : "(... .. ...)") : + inspect(context, obj); } private static final byte[] DOTDOTDOT = new byte[]{'.', '.', '.'}; @@ -468,10 +466,10 @@ public IRubyObject call(ThreadContext context, IRubyObject[] args, Block origina @Override public void doCall(ThreadContext context, IRubyObject arg) { - if (iter instanceof RubyFixnum) { - iter = RubyFixnum.newFixnum(context.runtime, ((RubyFixnum) iter).getLongValue() - 1); - } else if (iter instanceof RubyInteger) { - iter = ((RubyInteger) iter).op_minus(context, 1); + if (iter instanceof RubyFixnum iterFixnum) { + iter = RubyFixnum.newFixnum(context.runtime, iterFixnum.getLongValue() - 1); + } else if (iter instanceof RubyInteger iterInteger) { + iter = iterInteger.op_minus(context, 1); } else { iter = iter.callMethod(context, "-", one(context)); } @@ -616,8 +614,8 @@ public IRubyObject each(final ThreadContext context, final Block block) { if (!block.isGiven()) { return enumeratorizeWithSize(context, this, "each", RubyRange::size); } - if (begin instanceof RubyFixnum && isEndless) { - ((RubyFixnum) begin).step(context, IRubyObject.NULL_ARRAY, block); + if (begin instanceof RubyFixnum begFixnum && isEndless) { + begFixnum.step(context, IRubyObject.NULL_ARRAY, block); } else if (begin instanceof RubyFixnum && end instanceof RubyFixnum) { fixnumEach(context, block); } else if (begin instanceof RubySymbol && end instanceof RubySymbol) { @@ -670,17 +668,15 @@ public IRubyObject reverse_each(ThreadContext context, Block block) { if (end.isNil()) throw typeError(context, "can't iterate from ", end, ""); - if (beg instanceof RubyFixnum && end instanceof RubyFixnum) { + if (beg instanceof RubyFixnum && end instanceof RubyFixnum endFixnum) { if (excl) { - RubyFixnum endFixnum = (RubyFixnum) end; if (endFixnum.getLongValue() == RubyFixnum.MIN) return this; end = endFixnum.op_minus(context, 1); } reverseEachFixnum(context, beg, (RubyInteger) end, block); - } else if ((beg.isNil() || beg instanceof RubyInteger) && end instanceof RubyInteger) { - RubyInteger endInteger = (RubyInteger) end; + } else if ((beg.isNil() || beg instanceof RubyInteger) && end instanceof RubyInteger endInteger) { if (excl) { endInteger = (RubyInteger) endInteger.op_minus(context, 1); } @@ -1010,8 +1006,8 @@ private IRubyObject includeCommon(ThreadContext context, final IRubyObject val, || !TypeConverter.convertToTypeWithCheck(context, begin, runtime.getInteger(), to_int_checked).isNil() || !TypeConverter.convertToTypeWithCheck(context, end, runtime.getInteger(), to_int_checked).isNil()) { return asBoolean(context, rangeIncludes(context, val)); - } else if ((begin instanceof RubyString) || (end instanceof RubyString)) { - if ((begin instanceof RubyString) && (end instanceof RubyString)) { + } else if (begin instanceof RubyString || end instanceof RubyString) { + if (begin instanceof RubyString && end instanceof RubyString) { if (useStringCover) { return cover_p(context, val); } else { @@ -1055,11 +1051,8 @@ public IRubyObject eqq_p(ThreadContext context, IRubyObject obj) { @JRubyMethod(name = "cover?") public RubyBoolean cover_p(ThreadContext context, IRubyObject obj) { - if (obj instanceof RubyRange) { - return asBoolean(context, coverRange(context, (RubyRange) obj)); - } - - return asBoolean(context, rangeIncludes(context, obj)); + return asBoolean(context, + obj instanceof RubyRange range ? coverRange(context, range) : rangeIncludes(context, obj)); } // MRI: r_cover_p @@ -1118,9 +1111,8 @@ public IRubyObject max(ThreadContext context, Block block) { } int cmp = isBeginless ? -1 : RubyComparable.cmpint(context, invokedynamic(context, begin, MethodNames.OP_CMP, end), begin, end); - if (cmp > 0) { - return context.nil; - } + if (cmp > 0) return context.nil; + if (isExclusive) { if (!(end instanceof RubyInteger)) throw typeError(context, "cannot exclude non Integer end value"); @@ -1189,13 +1181,11 @@ public IRubyObject call(ThreadContext ctx, IRubyObject larg, Block blk) { @JRubyMethod public IRubyObject count(ThreadContext context, Block block) { - if (isBeginless || isEndless) return RubyFloat.newFloat(context.runtime, RubyFloat.INFINITY); + if (isBeginless || isEndless) return asFloat(context, RubyFloat.INFINITY); if (begin instanceof RubyInteger) { IRubyObject size = size(context); - if (!size.isNil()) { - return size; - } + if (!size.isNil()) return size; } return RubyEnumerable.count(context, this, block); @@ -1223,8 +1213,8 @@ public IRubyObject end(ThreadContext context) { public IRubyObject last(ThreadContext context, IRubyObject arg) { if (isEndless) throw context.runtime.newRangeError("cannot get the last element of endless range"); - if ((begin instanceof RubyInteger) && (end instanceof RubyInteger) - && this.getMetaClass().checkMethodBasicDefinition("each")) { + if (begin instanceof RubyInteger && end instanceof RubyInteger + && getMetaClass().checkMethodBasicDefinition("each")) { return intRangeLast(context, arg); } @@ -1304,9 +1294,9 @@ public void marshalTo(Ruby runtime, Object obj, RubyClass type, marshalStream.registerLinkTarget(range); List> attrs = range.getMarshalVariableList(); - attrs.add(new VariableEntry("excl", range.isExclusive ? runtime.getTrue() : runtime.getFalse())); - attrs.add(new VariableEntry("begin", range.begin)); - attrs.add(new VariableEntry("end", range.end)); + attrs.add(new VariableEntry<>("excl", range.isExclusive ? runtime.getTrue() : runtime.getFalse())); + attrs.add(new VariableEntry<>("begin", range.begin)); + attrs.add(new VariableEntry<>("end", range.end)); marshalStream.dumpVariables(attrs); } @@ -1461,13 +1451,13 @@ IRubyObject getRange(ThreadContext context) { // MRI: rb_range_values public static RangeLike rangeValues(ThreadContext context, IRubyObject range) { - if (range instanceof RubyRange) { - RubyRange vrange = (RubyRange) range; + if (range instanceof RubyRange vrange) { return new RangeLike(vrange.begin(context), vrange.end(context), vrange.isExcludeEnd()); } else if (range instanceof RubyArithmeticSequence) { return null; } else if (range.respondsTo("begin") && range.respondsTo("end") && range.respondsTo("exclude_end?")) { - return new RangeLike(Helpers.invoke(context, range, "begin"), Helpers.invoke(context, range, "end"), Helpers.invoke(context, range, "exclude_end?").isTrue()); + return new RangeLike(Helpers.invoke(context, range, "begin"), Helpers.invoke(context, range, "end"), + Helpers.invoke(context, range, "exclude_end?").isTrue()); } return null; @@ -1476,25 +1466,19 @@ public static RangeLike rangeValues(ThreadContext context, IRubyObject range) { public static class BSearch { @JRubyMethod(meta = true) public static IRubyObject double_to_long_bits(ThreadContext context, IRubyObject bsearch, IRubyObject flote) { - long longBits; - - if (flote instanceof RubyFixnum) { - longBits = Double.doubleToLongBits(((RubyFixnum) flote).getDoubleValue()); - } else { - longBits = Double.doubleToLongBits(((RubyFloat) flote).getDoubleValue()); - } - - return asFixnum(context, longBits); + return flote instanceof RubyFixnum value ? + asFixnum(context, Double.doubleToLongBits(value.getDoubleValue())) : + asFixnum(context, Double.doubleToLongBits(((RubyFloat) flote).getDoubleValue())); } @JRubyMethod(meta = true) public static IRubyObject long_bits_to_double(ThreadContext context, IRubyObject bsearch, IRubyObject fixnum) { - return context.runtime.newFloat(Double.longBitsToDouble(((RubyFixnum) fixnum).getLongValue())); + return asFloat(context, Double.longBitsToDouble(((RubyFixnum) fixnum).getLongValue())); } @JRubyMethod(meta = true) public static IRubyObject abs(ThreadContext context, IRubyObject bsearch, IRubyObject flote) { - return context.runtime.newFloat(Math.abs(((RubyFloat) flote).getDoubleValue())); + return asFloat(context, Math.abs(((RubyFloat) flote).getDoubleValue())); } } diff --git a/core/src/main/java/org/jruby/api/Convert.java b/core/src/main/java/org/jruby/api/Convert.java index 2f92ec92d25..02cb986eec2 100644 --- a/core/src/main/java/org/jruby/api/Convert.java +++ b/core/src/main/java/org/jruby/api/Convert.java @@ -6,6 +6,7 @@ import org.jruby.RubyClass; import org.jruby.RubyFile; import org.jruby.RubyFixnum; +import org.jruby.RubyFloat; import org.jruby.RubyHash; import org.jruby.RubyInteger; import org.jruby.RubyModule; @@ -364,6 +365,26 @@ public static RubyFixnum asFixnum(ThreadContext context, int value) { return RubyFixnum.newFixnum(context.runtime, value); } + /** + * Create a Ruby Float from a java double. + * @param context the current thread context + * @param value the double value + * @return the Ruby Float + */ + public static RubyFloat asFloat(ThreadContext context, double value) { + return RubyFloat.newFloat(context.runtime, value); + } + + /** + * Create a Ruby Float from a java long. + * @param context the current thread context + * @param value the long value + * @return the Ruby Float + */ + public static RubyFloat asFloat(ThreadContext context, long value) { + return RubyFloat.newFloat(context.runtime, value); + } + /** * Create a Ruby String from a java String. * @param context the current thread context