Skip to content

Commit 8a4ef7e

Browse files
author
maccaf
committed
Implemented history data editor
1 parent 4085fab commit 8a4ef7e

34 files changed

+2889
-8
lines changed

org.eclipsetrader.core/src/org/eclipsetrader/core/feed/History.java

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,145 @@ protected IStoreObject[] updateStoreObjects() {
364364
return history;
365365
}
366366

367+
/* (non-Javadoc)
368+
* @see org.eclipsetrader.core.feed.IHistory#getDay(java.util.Date)
369+
*/
370+
@Override
371+
public IHistory[] getDay(Date date) {
372+
Calendar c = Calendar.getInstance();
373+
c.setTime(date);
374+
c.set(Calendar.HOUR_OF_DAY, 0);
375+
c.set(Calendar.MINUTE, 0);
376+
c.set(Calendar.SECOND, 0);
377+
c.set(Calendar.MILLISECOND, 0);
378+
date = c.getTime();
379+
380+
IStore dayStore = null;
381+
IStoreProperties dayProperties = null;
382+
383+
if (store != null) {
384+
IStore[] childStores = store.fetchChilds(null);
385+
if (childStores != null) {
386+
for (int i = 0; i < childStores.length; i++) {
387+
IStoreProperties childProperties = childStores[i].fetchProperties(null);
388+
389+
Date barsDate = (Date) childProperties.getProperty(IPropertyConstants.BARS_DATE);
390+
if (date.equals(barsDate)) {
391+
dayStore = childStores[i];
392+
dayProperties = childProperties;
393+
break;
394+
}
395+
}
396+
}
397+
}
398+
399+
if (dayStore == null || dayProperties == null) {
400+
return new IHistory[0];
401+
}
402+
403+
List<IHistory> l = new ArrayList<IHistory>();
404+
405+
String[] propertyNames = dayProperties.getPropertyNames();
406+
for (int i = 0; i < propertyNames.length; i++) {
407+
Object value = dayProperties.getProperty(propertyNames[i]);
408+
if (!(value instanceof IOHLC[])) {
409+
continue;
410+
}
411+
TimeSpan timeSpan = TimeSpan.fromString(propertyNames[i]);
412+
if (timeSpan != null) {
413+
Key key = new Key(date, date, timeSpan);
414+
415+
WeakReference<HistoryDay> reference = historyMap.get(key);
416+
HistoryDay history = reference != null ? reference.get() : null;
417+
if (history == null) {
418+
IStore[] storeList = new IStore[] {
419+
dayStore,
420+
};
421+
IStoreProperties[] propertiesList = new IStoreProperties[] {
422+
dayProperties
423+
};
424+
history = createHistoryDay(storeList, propertiesList, timeSpan);
425+
historyMap.put(key, new WeakReference<HistoryDay>(history));
426+
}
427+
l.add(history);
428+
}
429+
}
430+
431+
return l.toArray(new IHistory[l.size()]);
432+
}
433+
434+
@SuppressWarnings("unchecked")
435+
private HistoryDay createHistoryDay(IStore[] storeList, IStoreProperties[] propertiesList, TimeSpan timeSpan) {
436+
HistoryDay history = new HistoryDay(security, timeSpan, storeList, propertiesList) {
437+
438+
@Override
439+
protected IStoreObject[] updateStoreObjects() {
440+
IStoreObject[] storeObject = super.updateStoreObjects();
441+
442+
Set<Entry<Key, WeakReference<HistoryDay>>> set = historyMap.entrySet();
443+
Entry<Key, WeakReference<HistoryDay>>[] entry = set.toArray(new Entry[set.size()]);
444+
445+
Set<Key> updatedElements = new HashSet<Key>();
446+
447+
for (int ii = 0; ii < storeObject.length; ii++) {
448+
TimeSpan timeSpan = (TimeSpan) storeObject[ii].getStoreProperties().getProperty(IPropertyConstants.TIME_SPAN);
449+
Date barsDate = (Date) storeObject[ii].getStoreProperties().getProperty(IPropertyConstants.BARS_DATE);
450+
for (int i = 0; i < entry.length; i++) {
451+
HistoryDay element = entry[i].getValue().get();
452+
Key key = entry[i].getKey();
453+
if (element != null && element != this && element.getTimeSpan().equals(timeSpan)) {
454+
if (!entry[i].getKey().isInRange(barsDate)) {
455+
continue;
456+
}
457+
updatedElements.add(key);
458+
}
459+
}
460+
}
461+
462+
for (Key key : updatedElements) {
463+
HistoryDay element = historyMap.get(key).get();
464+
if (element == null) {
465+
continue;
466+
}
467+
468+
Map<Date, IStore> storeList = new HashMap<Date, IStore>();
469+
Map<Date, IStoreProperties> propertyList = new HashMap<Date, IStoreProperties>();
470+
471+
IStore[] childStores = store != null ? store.fetchChilds(null) : null;
472+
if (childStores != null) {
473+
for (IStore childStore : childStores) {
474+
IStoreProperties properties = childStore.fetchProperties(null);
475+
476+
Date barsDate = (Date) properties.getProperty(IPropertyConstants.BARS_DATE);
477+
if (!key.isInRange(barsDate)) {
478+
continue;
479+
}
480+
481+
storeList.put(barsDate, childStore);
482+
propertyList.put(barsDate, properties);
483+
}
484+
}
485+
for (int i = 0; i < storeObject.length; i++) {
486+
Date barsDate = (Date) storeObject[i].getStoreProperties().getProperty(IPropertyConstants.BARS_DATE);
487+
if (!key.isInRange(barsDate)) {
488+
continue;
489+
}
490+
491+
storeList.put(barsDate, storeObject[i].getStore());
492+
propertyList.put(barsDate, storeObject[i].getStoreProperties());
493+
}
494+
495+
Collection<IStore> s = storeList.values();
496+
Collection<IStoreProperties> p = propertyList.values();
497+
element.setStoreProperties(s.toArray(new IStore[s.size()]), p.toArray(new IStoreProperties[p.size()]));
498+
}
499+
500+
return storeObject;
501+
}
502+
};
503+
return history;
504+
}
505+
367506
/* (non-Javadoc)
368507
* @see org.eclipsetrader.core.feed.IHistory#getTimeSpan()
369508
*/
@@ -426,7 +565,9 @@ public IOHLC[] getAdjustedOHLC() {
426565
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
427566
*/
428567
@Override
429-
@SuppressWarnings("unchecked")
568+
@SuppressWarnings({
569+
"unchecked", "rawtypes"
570+
})
430571
public Object getAdapter(Class adapter) {
431572
if (adapter.isAssignableFrom(getClass())) {
432573
return this;

org.eclipsetrader.core/src/org/eclipsetrader/core/feed/HistoryDay.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ public IHistory getSubset(Date first, Date last, TimeSpan aggregation) {
300300
return null;
301301
}
302302

303+
/* (non-Javadoc)
304+
* @see org.eclipsetrader.core.feed.IHistory#getDay(java.util.Date)
305+
*/
306+
@Override
307+
public IHistory[] getDay(Date date) {
308+
return null;
309+
}
310+
303311
/* (non-Javadoc)
304312
* @see org.eclipsetrader.core.feed.IHistory#getTimeSpan()
305313
*/

org.eclipsetrader.core/src/org/eclipsetrader/core/feed/IHistory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ public interface IHistory extends IAdaptable {
3939
public ISplit[] getSplits();
4040

4141
public IOHLC[] getAdjustedOHLC();
42+
43+
public IHistory[] getDay(Date date);
4244
}

org.eclipsetrader.core/src/org/eclipsetrader/core/feed/TimeSpan.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ else if (units.ordinal() == timeSpan.units.ordinal()) {
131131
return false;
132132
}
133133

134+
public String getDescription() {
135+
return NLS.bind("{0} {1}", new Object[] {
136+
String.valueOf(length), units.toString()
137+
});
138+
}
139+
134140
/* (non-Javadoc)
135141
* @see java.lang.Object#toString()
136142
*/
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2004-2011 Marco Maccaferri and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Marco Maccaferri - initial API and implementation
10+
*/
11+
12+
package org.eclipsetrader.ui.internal.charts.views;
13+
14+
import java.util.Calendar;
15+
import java.util.Date;
16+
17+
import junit.framework.TestCase;
18+
import junit.framework.TestResult;
19+
20+
import org.eclipse.core.databinding.observable.Realm;
21+
import org.eclipse.jface.databinding.swt.SWTObservables;
22+
import org.eclipse.swt.widgets.Display;
23+
import org.eclipsetrader.core.feed.IOHLC;
24+
import org.eclipsetrader.core.feed.OHLC;
25+
import org.eclipsetrader.core.feed.TimeSpan;
26+
27+
public class HistoryDataEditorModelTest extends TestCase {
28+
29+
@Override
30+
public void run(final TestResult result) {
31+
Display display = Display.getDefault();
32+
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
33+
34+
@Override
35+
public void run() {
36+
HistoryDataEditorModelTest.super.run(result);
37+
}
38+
});
39+
}
40+
41+
public void testInitialSettings() throws Exception {
42+
HistoryDataEditorModel model = new HistoryDataEditorModel(TimeSpan.days(1));
43+
assertEquals(1, model.getList().size());
44+
assertTrue(((HistoryDataElement) model.getList().get(0)).isEmpty());
45+
}
46+
47+
public void testSet() throws Exception {
48+
IOHLC[] bars = new IOHLC[] {
49+
new OHLC(getTime(2007, Calendar.NOVEMBER, 13), 200.0, 210.0, 190.0, 195.0, 100000L),
50+
new OHLC(getTime(2007, Calendar.NOVEMBER, 12), 100.0, 110.0, 90.0, 95.0, 100000L),
51+
new OHLC(getTime(2007, Calendar.NOVEMBER, 11), 400.0, 410.0, 390.0, 395.0, 100000L),
52+
};
53+
HistoryDataEditorModel model = new HistoryDataEditorModel(TimeSpan.days(1));
54+
55+
model.set(bars);
56+
57+
assertEquals(4, model.getList().size());
58+
}
59+
60+
public void testAddNewEmptyElementWhenLastIsEdited() throws Exception {
61+
HistoryDataEditorModel model = new HistoryDataEditorModel(TimeSpan.days(1));
62+
63+
HistoryDataElement lastElement = model.getLastElement();
64+
lastElement.setDate(getTime(2007, Calendar.NOVEMBER, 14));
65+
66+
assertEquals(2, model.getList().size());
67+
assertNotSame(lastElement, model.getLastElement());
68+
assertNotNull(model.getLastElement());
69+
}
70+
71+
public void testDontAddNewEmptyElementTwice() throws Exception {
72+
HistoryDataEditorModel model = new HistoryDataEditorModel(TimeSpan.days(1));
73+
74+
HistoryDataElement lastElement = model.getLastElement();
75+
lastElement.setDate(getTime(2007, Calendar.NOVEMBER, 14));
76+
lastElement.setClose(1.0);
77+
78+
assertEquals(2, model.getList().size());
79+
}
80+
81+
public void testSetAndAddNewElement() throws Exception {
82+
IOHLC[] bars = new IOHLC[] {
83+
new OHLC(getTime(2007, Calendar.NOVEMBER, 13), 200.0, 210.0, 190.0, 195.0, 100000L),
84+
new OHLC(getTime(2007, Calendar.NOVEMBER, 12), 100.0, 110.0, 90.0, 95.0, 100000L),
85+
new OHLC(getTime(2007, Calendar.NOVEMBER, 11), 400.0, 410.0, 390.0, 395.0, 100000L),
86+
};
87+
HistoryDataEditorModel model = new HistoryDataEditorModel(TimeSpan.days(1));
88+
89+
model.set(bars);
90+
91+
HistoryDataElement lastElement = model.getLastElement();
92+
lastElement.setDate(getTime(2007, Calendar.NOVEMBER, 14));
93+
94+
assertEquals(5, model.getList().size());
95+
}
96+
97+
public void testToOHLC() throws Exception {
98+
IOHLC[] bars = new IOHLC[] {
99+
new OHLC(getTime(2007, Calendar.NOVEMBER, 13), 200.0, 210.0, 190.0, 195.0, 100000L),
100+
new OHLC(getTime(2007, Calendar.NOVEMBER, 12), 100.0, 110.0, 90.0, 95.0, 100000L),
101+
new OHLC(getTime(2007, Calendar.NOVEMBER, 11), 400.0, 410.0, 390.0, 395.0, 100000L),
102+
};
103+
HistoryDataEditorModel model = new HistoryDataEditorModel(TimeSpan.days(1));
104+
105+
model.set(bars);
106+
107+
IOHLC[] newBars = model.toOHLC();
108+
109+
assertEquals(bars.length, newBars.length);
110+
}
111+
112+
private Date getTime(int year, int month, int day) {
113+
Calendar date = Calendar.getInstance();
114+
date.set(year, month, day, 0, 0, 0);
115+
date.set(Calendar.MILLISECOND, 0);
116+
return date.getTime();
117+
}
118+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2004-2011 Marco Maccaferri and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Marco Maccaferri - initial API and implementation
10+
*/
11+
12+
package org.eclipsetrader.ui.internal.charts.views;
13+
14+
import java.util.Calendar;
15+
import java.util.Date;
16+
17+
import junit.framework.TestCase;
18+
19+
import org.eclipsetrader.core.feed.OHLC;
20+
21+
public class HistoryDataElementTest extends TestCase {
22+
23+
public void testIsEmpty() throws Exception {
24+
HistoryDataElement element = new HistoryDataElement();
25+
26+
assertTrue(element.isEmpty());
27+
28+
element.setClose(1.0);
29+
30+
assertFalse(element.isEmpty());
31+
}
32+
33+
public void testIsValid() throws Exception {
34+
HistoryDataElement element = new HistoryDataElement();
35+
36+
assertFalse(element.isValid());
37+
38+
element.setDate(new Date());
39+
assertFalse(element.isValid());
40+
41+
element.setOpen(1.0);
42+
assertFalse(element.isValid());
43+
44+
element.setHigh(1.0);
45+
assertFalse(element.isValid());
46+
47+
element.setLow(1.0);
48+
assertFalse(element.isValid());
49+
50+
element.setClose(1.0);
51+
assertFalse(element.isValid());
52+
53+
element.setVolume(1L);
54+
assertTrue(element.isValid());
55+
}
56+
57+
public void testEquals() throws Exception {
58+
HistoryDataElement element = new HistoryDataElement(new OHLC(getTime(2007, Calendar.NOVEMBER, 13), 200.0, 210.0, 190.0, 195.0, 100000L));
59+
60+
HistoryDataElement otherElement = new HistoryDataElement();
61+
assertFalse(element.equals(otherElement));
62+
63+
otherElement.setDate(getTime(2007, Calendar.NOVEMBER, 13));
64+
assertFalse(element.equals(otherElement));
65+
66+
otherElement.setOpen(200.0);
67+
assertFalse(element.equals(otherElement));
68+
69+
otherElement.setHigh(210.0);
70+
assertFalse(element.equals(otherElement));
71+
72+
otherElement.setLow(190.0);
73+
assertFalse(element.equals(otherElement));
74+
75+
otherElement.setClose(195.0);
76+
assertFalse(element.equals(otherElement));
77+
78+
otherElement.setVolume(100000L);
79+
assertTrue(element.equals(otherElement));
80+
}
81+
82+
private Date getTime(int year, int month, int day) {
83+
Calendar date = Calendar.getInstance();
84+
date.set(year, month, day, 0, 0, 0);
85+
date.set(Calendar.MILLISECOND, 0);
86+
return date.getTime();
87+
}
88+
}

0 commit comments

Comments
 (0)