13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.runtime.core;
|
|
11
|
|
12 import java.math.*;
|
|
13
|
|
14 import com.oracle.truffle.ruby.runtime.*;
|
|
15 import com.oracle.truffle.ruby.runtime.core.array.*;
|
|
16 import com.oracle.truffle.ruby.runtime.objects.*;
|
|
17
|
|
18 /**
|
|
19 * Represents the Ruby {@code Bignum} class.
|
|
20 */
|
|
21 public class RubyBignum extends RubyObject implements Unboxable {
|
|
22
|
|
23 private final BigInteger value;
|
|
24
|
|
25 public RubyBignum(RubyClass bignumClass, BigInteger value) {
|
|
26 super(bignumClass);
|
|
27
|
|
28 assert value != null;
|
|
29
|
|
30 this.value = value;
|
|
31 }
|
|
32
|
|
33 public BigInteger getValue() {
|
|
34 return value;
|
|
35 }
|
|
36
|
|
37 public Object unbox() {
|
|
38 return value;
|
|
39 }
|
|
40
|
|
41 public static RubyArray divMod(RubyContext context, BigInteger a, BigInteger b) {
|
|
42 final BigInteger[] quotientRemainder = a.divideAndRemainder(b);
|
|
43
|
|
44 final Object quotient = GeneralConversions.fixnumOrBignum(quotientRemainder[0]);
|
|
45 final Object remainder = GeneralConversions.fixnumOrBignum(quotientRemainder[1]);
|
|
46
|
|
47 final ObjectImmutablePairArrayStore store = new ObjectImmutablePairArrayStore(quotient, remainder);
|
|
48 return new RubyArray(context.getCoreLibrary().getArrayClass(), store);
|
|
49 }
|
|
50
|
|
51 @Override
|
|
52 public int hashCode() {
|
|
53 return value.hashCode();
|
|
54 }
|
|
55
|
|
56 @Override
|
|
57 public boolean equals(Object obj) {
|
|
58 if (this == obj) {
|
|
59 return true;
|
|
60 }
|
|
61 if (obj == null) {
|
|
62 return false;
|
|
63 }
|
|
64 if (!(obj instanceof RubyBignum)) {
|
|
65 return false;
|
|
66 }
|
|
67 RubyBignum other = (RubyBignum) obj;
|
|
68 if (value == null) {
|
|
69 if (other.value != null) {
|
|
70 return false;
|
|
71 }
|
|
72 } else if (!value.equals(other.value)) {
|
|
73 return false;
|
|
74 }
|
|
75 return true;
|
|
76 }
|
|
77
|
|
78 @Override
|
|
79 public String toString() {
|
|
80 return value.toString();
|
|
81 }
|
|
82
|
|
83 }
|