-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockParser.java
112 lines (99 loc) · 3.79 KB
/
StockParser.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.schedulers.TestScheduler;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class StockParser {
public static void main(String[] args) throws IOException {
parseFile("IBM.json");
}
public static void parseFile(String filename) throws IOException {
Scanner scanner = new Scanner(new File(filename));
//Scheduler scheduler = Schedulers.newThread();
TestScheduler scheduler = Schedulers.test();
Observable<Stock> stocks = Observable.create(new rx.Observable.OnSubscribe<Stock>() {
@Override
public void call(Subscriber<? super Stock> subscriber) {
scheduler.schedule(new Action1<Scheduler.Inner>() {
@Override
public void call(Scheduler.Inner inner) {
try {
if (scanner.hasNextLine()) {
Stock stock = parseLine(scanner.nextLine());
subscriber.onNext(stock);
} else {
subscriber.onCompleted();
}
} catch(Throwable t) {
subscriber.onError(t);
}
final Action1<Scheduler.Inner> innerAction = this;
inner.schedule(new Action1<Scheduler.Inner>() {
@Override
public void call(Scheduler.Inner inner) {
innerAction.call(inner);
}
}, 1, TimeUnit.SECONDS);
}
});
}
});
Subscription subscription = stocks.timestamp(scheduler).subscribe(System.out::println);
while (!subscription.isUnsubscribed()) {
System.in.read();
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
}
}
public static Stock parseLine(String line) {
line = line.replace("{", "").replace("}", "").replace("\"", "");
String[] parts = line.split(",");
String date = "";
double open = 0, high = 0, low = 0, close = 0;
int volume = 0;
for (String part : parts) {
String[] keyValue = part.split(":",2);
String key = keyValue[0];
String value = keyValue[1];
if (key.equals("Date")) {
date = value;
} else if (key.equals("Open")) {
open = Double.parseDouble(value);
} else if (key.equals("High")) {
high = Double.parseDouble(value);
} else if (key.equals("Low")) {
low = Double.parseDouble(value);
} else if (key.equals("Close")) {
close = Double.parseDouble(value);
} else if (key.equals("Volume")) {
volume = Integer.parseInt(value);
}
}
return new Stock(date, open, high, low, close, volume);
}
}
class Stock {
public String date;
public double open;
public double high;
public double low;
public double close;
public int volume;
public Stock(String date, double open, double high, double low, double close, int volume) {
this.date = date;
this.open = open;
this.high = high;
this.low = low;
this.close = close;
this.volume = volume;
}
@Override
public String toString() {
return "date: " + date + " open: " + open + " high: " + high + " low: " + low + " close: " + close + " volume: " + volume;
}
}