Skip to content

Commit 4471d99

Browse files
committed
refactoring eql and ==
1 parent 037fb72 commit 4471d99

File tree

3 files changed

+703
-682
lines changed

3 files changed

+703
-682
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
**v1.1.2** Refactor `runner.rb` to `runner.rb`, `args.rb` and `installer.rb`. The `Installer` classes have the role of installing `jruby-complete`, the examples and providing `setup check` functionality. Refactored and improved default `config.yml` tool, all should make it easier for `collaborators/successors` to follow the code.
2+
**v1.1.2** Refactor `runner.rb` to `runner.rb`, `args.rb` and `installer.rb`. The `Installer` classes have the role of installing `jruby-complete`, the examples and providing `setup check` functionality. Refactored and improved default `config.yml` tool, all should make it easier for `collaborators/successors` to follow the code. Refactored `Vec2D` and `Vec3D` `==` and `eql?` methods.
33

44
**v1.1.1** Even more `data_path` fixes in examples, update to jruby-complete-9.1.2.0
55

src/monkstone/vecmath/vec2/Vec2.java

+23-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import org.jruby.Ruby;
2424
import org.jruby.RubyArray;
25-
import org.jruby.RubyBoolean;
2625
import org.jruby.RubyClass;
2726
import org.jruby.RubyObject;
2827
import org.jruby.RubySymbol;
@@ -654,12 +653,16 @@ public int hashCode() {
654653
*/
655654
@Override
656655
public boolean equals(Object obj) {
656+
if (obj == this){
657+
return true;
658+
}
657659
if (obj instanceof Vec2) {
658660
final Vec2 other = (Vec2) obj;
659-
if (!((Double) this.jx).equals(other.jx)) {
660-
return false;
661-
}
662-
return ((Double) this.jy).equals(other.jy);
661+
if ((Double.compare(jx,(Double)other.jx) == 0)
662+
&& (Double.compare(jy,(Double)other.jy) == 0))
663+
{
664+
return true;
665+
}
663666
}
664667
return false;
665668
}
@@ -673,14 +676,18 @@ public boolean equals(Object obj) {
673676
@JRubyMethod(name = "eql?", required = 1)
674677
public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
675678
Ruby runtime = context.getRuntime();
679+
if (other == this){
680+
return runtime.newBoolean(true);
681+
}
676682
if (other instanceof Vec2) {
677683
Vec2 v = (Vec2) other.toJava(Vec2.class);
678-
if (!((Double) this.jx).equals(v.jx)) {
679-
return RubyBoolean.newBoolean(runtime, false);
680-
}
681-
return RubyBoolean.newBoolean(runtime, ((Double) this.jy).equals(v.jy));
684+
if ((Double.compare(jx,(Double)v.jx) == 0)
685+
&& (Double.compare(jy,(Double)v.jy) == 0))
686+
{
687+
return runtime.newBoolean(true);
688+
}
682689
}
683-
return RubyBoolean.newBoolean(runtime, false);
690+
return runtime.newBoolean(false);
684691
}
685692

686693
/**
@@ -694,16 +701,18 @@ public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
694701
@Override
695702
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
696703
Ruby runtime = context.getRuntime();
704+
if (other == this){
705+
return runtime.newBoolean(true);
706+
}
697707
if (other instanceof Vec2) {
698708
Vec2 v = (Vec2) other.toJava(Vec2.class);
699709
double diff = jx - v.jx;
700710
if ((diff < 0 ? -diff : diff) > Vec2.EPSILON) {
701-
return RubyBoolean.newBoolean(runtime, false);
711+
return runtime.newBoolean(false);
702712
}
703713
diff = jy - v.jy;
704-
boolean result = ((diff < 0 ? -diff : diff) < Vec2.EPSILON);
705-
return RubyBoolean.newBoolean(runtime, result);
714+
return runtime.newBoolean((diff < 0 ? -diff : diff) < Vec2.EPSILON);
706715
}
707-
return RubyBoolean.newBoolean(runtime, false);
716+
return runtime.newBoolean(false);
708717
}
709718
}

0 commit comments

Comments
 (0)