Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IntRangeSequence |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 | * | |
5 | * This code is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 only, as | |
7 | * published by the Free Software Foundation. Sun designates this | |
8 | * particular file as subject to the "Classpath" exception as provided | |
9 | * by Sun in the LICENSE file that accompanied this code. | |
10 | * | |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * version 2 for more details (a copy is included in the LICENSE file that | |
15 | * accompanied this code). | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License version | |
18 | * 2 along with this work; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | * | |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
23 | * have any questions. | |
24 | */ | |
25 | ||
26 | package com.sun.javafx.runtime.sequence; | |
27 | ||
28 | /** | |
29 | * Special case implementation for sequences that are ranges of integers, such as [1..10]. Range sequences should | |
30 | * be constructed with the Sequences.range() factory method rather than with the IntRangeSequence constructor. An | |
31 | * optional "step" allows sequences to vary by more than one; [ 1..5 STEP 2 ] is [ 1, 3, 5 ]. | |
32 | * O(1) space and time construction costs. | |
33 | * | |
34 | * @author Brian Goetz | |
35 | */ | |
36 | 722540 | class IntRangeSequence extends AbstractSequence<Integer> implements Sequence<Integer> { |
37 | ||
38 | private final int start, step, size; | |
39 | ||
40 | ||
41 | public IntRangeSequence(int start, int bound, int step, boolean exclusive) { | |
42 | 2751 | super(Integer.class); |
43 | 2751 | this.start = start; |
44 | 2751 | this.step = step; |
45 | 2751 | if (Math.abs((long) start - (long) bound) + ((long) (exclusive ? 0 : 1)) > Integer.MAX_VALUE) |
46 | 3 | throw new IllegalArgumentException("Range sequence too big"); |
47 | 2748 | if (bound == start) { |
48 | 208 | this.size = exclusive ? 0 : 1; |
49 | } | |
50 | else { | |
51 | 2540 | int size = Math.max(0, ((bound - start) / step) + 1); |
52 | 2540 | if (exclusive) { |
53 | 132 | boolean tooBig = (step > 0) |
54 | ? (start + (size-1)*step >= bound) | |
55 | : (start + (size-1)*step <= bound); | |
56 | 132 | if (tooBig && size > 0) |
57 | 118 | --size; |
58 | } | |
59 | 2540 | this.size = (int) size; |
60 | } | |
61 | 2748 | } |
62 | ||
63 | public IntRangeSequence(int start, int bound, int step) { | |
64 | 118 | this(start, bound, step, false); |
65 | 118 | } |
66 | ||
67 | public IntRangeSequence(int start, int bound) { | |
68 | 2476 | this(start, bound, 1, false); |
69 | 2473 | } |
70 | ||
71 | public IntRangeSequence(int start, int bound, boolean exclusive) { | |
72 | 116 | this(start, bound, 1, exclusive); |
73 | 116 | } |
74 | ||
75 | @Override | |
76 | public int size() { | |
77 | 1441946 | return size; |
78 | } | |
79 | ||
80 | @Override | |
81 | public Integer get(int position) { | |
82 | 722540 | if (position < 0 || position >= size) |
83 | 70 | throw new IndexOutOfBoundsException(Integer.toString(position)); |
84 | else | |
85 | 722470 | return (start + position * step); |
86 | } | |
87 | ||
88 | @Override | |
89 | public void toArray(Object[] array, int destOffset) { | |
90 | 1625 | for (int value = start, index = destOffset; index < destOffset+size; value += step, index++) |
91 | 1376 | array[index] = value; |
92 | 249 | } |
93 | } |