-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.java
More file actions
56 lines (46 loc) · 1.71 KB
/
Copy pathRange.java
File metadata and controls
56 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.List;
/** Records information about a range / subsequence in the price data. */
public class Range {
/** Returns a range consisting of the given index only. */
public static Range fromOneIndex(int index, List<Integer> prices) {
return new Range(index, prices.get(index));
}
public final int firstIndex;
public final int lastIndex;
public final int lowPrice;
public final int highPrice;
/** Creates a range consisting of only one price. */
private Range(int index, int price) {
firstIndex = lastIndex = index;
lowPrice = highPrice = price;
}
/** Creates a range consisting of any number of consecutive prices. */
private Range(int firstIndex, int lastIndex, int lowPrice, int highPrice) {
assert firstIndex <= lastIndex;
assert lowPrice <= highPrice;
this.firstIndex = firstIndex;
this.lastIndex = lastIndex;
this.lowPrice = lowPrice;
this.highPrice = highPrice;
}
/**
* Returns a range consisting of this one followed by the other. This
* requires that the other range must immediate follows this one.
*/
public Range concat(Range other) {
assert this.lastIndex + 1 == other.firstIndex;
return new Range(firstIndex, other.lastIndex,
Math.min(this.lowPrice, other.lowPrice),
Math.max(this.highPrice, other.highPrice));
}
/** Returns the length of the range. */
public int length() { return lastIndex + 1 - firstIndex; }
/**
* Determines whether the percent change between the low and high price is
* less than or equal to the given amount.
*/
public boolean percentChangeAtMost(double pctChanged) {
// highPrice / lowPrice <= 1 + pctChanged/100
return highPrice <= lowPrice * (1 + pctChanged/100);
}
}