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.objects.*;
|
|
15
|
|
16 /**
|
|
17 * Represents the Ruby {@code Fixnum} class.
|
|
18 */
|
|
19 public class RubyFixnum extends RubyObject implements Unboxable {
|
|
20
|
|
21 public static final int MIN_VALUE = Integer.MIN_VALUE;
|
|
22 public static final int MAX_VALUE = Integer.MAX_VALUE;
|
|
23
|
|
24 public static final BigInteger MIN_VALUE_BIG = BigInteger.valueOf(MIN_VALUE);
|
|
25 public static final BigInteger MAX_VALUE_BIG = BigInteger.valueOf(MAX_VALUE);
|
|
26
|
|
27 public static final int SIZE = Integer.SIZE;
|
|
28
|
|
29 private final int value;
|
|
30
|
|
31 public RubyFixnum(RubyClass fixnumClass, int value) {
|
|
32 super(fixnumClass);
|
|
33 this.value = value;
|
|
34 }
|
|
35
|
|
36 public int getValue() {
|
|
37 return value;
|
|
38 }
|
|
39
|
|
40 @Override
|
|
41 public String toString() {
|
|
42 return Integer.toString(value);
|
|
43 }
|
|
44
|
|
45 @Override
|
|
46 public boolean equals(Object other) {
|
|
47 if (other instanceof Integer) {
|
|
48 return value == (int) other;
|
|
49 } else if (other instanceof RubyFixnum) {
|
|
50 return value == ((RubyFixnum) other).value;
|
|
51 } else if (other instanceof BigInteger) {
|
|
52 return ((BigInteger) other).equals(value);
|
|
53 } else if (other instanceof RubyBignum) {
|
|
54 return ((RubyBignum) other).getValue().equals(value);
|
|
55 } else if (other instanceof Double) {
|
|
56 return value == (double) other;
|
|
57 } else if (other instanceof RubyFloat) {
|
|
58 return value == ((RubyFloat) other).getValue();
|
|
59 } else {
|
|
60 return super.equals(other);
|
|
61 }
|
|
62 }
|
|
63
|
|
64 @Override
|
|
65 public int hashCode() {
|
|
66 throw new UnsupportedOperationException();
|
|
67 }
|
|
68
|
|
69 public Object unbox() {
|
|
70 return value;
|
|
71 }
|
|
72
|
|
73 }
|