Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.ruby.runtime/src/com/oracle/truffle/ruby/runtime/core/range/FixnumRange.java @ 13514:0fbee3eb71f0
Ruby: import project.
author | Chris Seaton <chris.seaton@oracle.com> |
---|---|
date | Mon, 06 Jan 2014 17:12:09 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
13513:64a23ce736a0 | 13514:0fbee3eb71f0 |
---|---|
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.range; | |
11 | |
12 import com.oracle.truffle.ruby.runtime.core.*; | |
13 import com.oracle.truffle.ruby.runtime.core.array.*; | |
14 | |
15 /** | |
16 * A range that has {@code Fixnum} begin and end. | |
17 */ | |
18 public class FixnumRange extends RubyRange { | |
19 | |
20 private final int begin; | |
21 private final int end; | |
22 private final boolean excludeEnd; | |
23 | |
24 public FixnumRange(RubyClass rangeClass, int begin, int end, boolean excludeEnd) { | |
25 super(rangeClass); | |
26 this.begin = begin; | |
27 this.end = end; | |
28 this.excludeEnd = excludeEnd; | |
29 } | |
30 | |
31 @Override | |
32 public String toString() { | |
33 if (excludeEnd) { | |
34 return begin + "..." + end; | |
35 } else { | |
36 return begin + ".." + end; | |
37 } | |
38 } | |
39 | |
40 @Override | |
41 public RubyArray toArray() { | |
42 final int length = getLength(); | |
43 | |
44 if (length < 0) { | |
45 return new RubyArray(getRubyClass().getContext().getCoreLibrary().getArrayClass()); | |
46 } else { | |
47 final int[] values = new int[length]; | |
48 | |
49 for (int n = 0; n < length; n++) { | |
50 values[n] = begin + n; | |
51 } | |
52 | |
53 return new RubyArray(getRubyClass().getContext().getCoreLibrary().getArrayClass(), new FixnumArrayStore(values)); | |
54 } | |
55 } | |
56 | |
57 private int getLength() { | |
58 if (excludeEnd) { | |
59 return end - begin; | |
60 } else { | |
61 return end - begin + 1; | |
62 } | |
63 } | |
64 | |
65 public final int getBegin() { | |
66 return begin; | |
67 } | |
68 | |
69 public final int getEnd() { | |
70 return end; | |
71 } | |
72 | |
73 public final int getInclusiveEnd() { | |
74 if (excludeEnd) { | |
75 return end - 1; | |
76 } else { | |
77 return end; | |
78 } | |
79 } | |
80 | |
81 public final int getExclusiveEnd() { | |
82 if (excludeEnd) { | |
83 return end; | |
84 } else { | |
85 return end + 1; | |
86 } | |
87 } | |
88 | |
89 @Override | |
90 public boolean doesExcludeEnd() { | |
91 return excludeEnd; | |
92 } | |
93 | |
94 @Override | |
95 public int hashCode() { | |
96 final int prime = 31; | |
97 int result = 1; | |
98 result = prime * result + begin; | |
99 result = prime * result + end; | |
100 result = prime * result + (excludeEnd ? 1231 : 1237); | |
101 return result; | |
102 } | |
103 | |
104 @Override | |
105 public boolean equals(Object obj) { | |
106 if (this == obj) { | |
107 return true; | |
108 } | |
109 if (obj == null) { | |
110 return false; | |
111 } | |
112 if (!(obj instanceof FixnumRange)) { | |
113 return false; | |
114 } | |
115 FixnumRange other = (FixnumRange) obj; | |
116 if (begin != other.begin) { | |
117 return false; | |
118 } | |
119 if (end != other.end) { | |
120 return false; | |
121 } | |
122 if (excludeEnd != other.excludeEnd) { | |
123 return false; | |
124 } | |
125 return true; | |
126 } | |
127 | |
128 } |