Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NumberRangeSequence |
|
| 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 floating point, such as [1.0 .. 2.0 BY .1]. | |
30 | * Range sequences should be constructed with the Sequences.range() factory method rather than with the | |
31 | * NumberRangeSequence constructor. Unlike integer range sequences, the step is required. | |
32 | * O(1) space and time construction costs. | |
33 | * | |
34 | * @author Brian Goetz | |
35 | */ | |
36 | 19918 | class NumberRangeSequence extends AbstractSequence<Double> implements Sequence<Double> { |
37 | ||
38 | private final double start, step; | |
39 | private final int size; | |
40 | ||
41 | ||
42 | public NumberRangeSequence(double start, double bound, double step, boolean exclusive) { | |
43 | 1055 | super(Double.class); |
44 | 1055 | this.start = start; |
45 | 1055 | this.step = step; |
46 | 1055 | if (bound == start) { |
47 | 5 | this.size = exclusive ? 0 : 1; |
48 | } | |
49 | else { | |
50 | ||
51 | 1050 | long size = ((bound < start && step > 0.0) || |
52 | (bound > start && step < 0.0))? | |
53 | 0 | |
54 | :Math.max(0, (((long) ((bound - start) / step)) + 1)); | |
55 | 1050 | if (exclusive) { |
56 | 21 | boolean tooBig = (step > 0) |
57 | ? (start + (size-1)*step >= bound) | |
58 | : (start + (size-1)*step <= bound); | |
59 | 21 | if (tooBig && size > 0) |
60 | 12 | --size; |
61 | } | |
62 | 1050 | if (size > Integer.MAX_VALUE || size < 0) |
63 | 1 | throw new IllegalArgumentException("Range sequence too big"); |
64 | else | |
65 | 1049 | this.size = (int) size; |
66 | } | |
67 | 1054 | } |
68 | ||
69 | public NumberRangeSequence(double start, double bound, double step) { | |
70 | 1032 | this(start, bound, step, false); |
71 | 1031 | } |
72 | ||
73 | @Override | |
74 | public int size() { | |
75 | 40945 | return size; |
76 | } | |
77 | ||
78 | @Override | |
79 | public Double get(int position) { | |
80 | 19918 | if (position < 0 || position >= size) |
81 | 0 | throw new IndexOutOfBoundsException(Integer.toString(position)); |
82 | else | |
83 | 19918 | return (start + position * step); |
84 | } | |
85 | ||
86 | @Override | |
87 | public void toArray(Object[] array, int destOffset) { | |
88 | 21 | int index = destOffset; |
89 | 99 | for (double value = start; index < destOffset+size; value += step, index++) |
90 | 78 | array[index] = value; |
91 | 21 | } |
92 | } |