Committed by
Gerrit Code Review
[ONOS-3949] Initial implementation of a Chart Model for Web GUI
Change-Id: I11e2fc38e25d7c05f5233b2ad0dbca8b82b11b02
Showing
3 changed files
with
277 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.ui.chart; | ||
18 | + | ||
19 | +import com.google.common.collect.Maps; | ||
20 | + | ||
21 | +import java.util.Arrays; | ||
22 | +import java.util.Map; | ||
23 | + | ||
24 | +import static com.google.common.base.Preconditions.checkArgument; | ||
25 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
26 | + | ||
27 | +/** | ||
28 | + * A simple model of chart data. | ||
29 | + * | ||
30 | + * <p> | ||
31 | + * Note that this is not a full MVC type model; the expected usage pattern | ||
32 | + * is to create an empty chart, add data points (by consulting the business model), | ||
33 | + * and produce the list of data points which contain a label and a set of data | ||
34 | + * values for all serials. | ||
35 | + */ | ||
36 | +public class ChartModel { | ||
37 | + | ||
38 | + // key is series name, value is series index | ||
39 | + private final Map<String, Integer> seriesMap; | ||
40 | + private final DataPoint[] dataPoints; | ||
41 | + | ||
42 | + /** | ||
43 | + * Constructs a chart model with initialized series set. | ||
44 | + * | ||
45 | + * @param series a set of series | ||
46 | + */ | ||
47 | + public ChartModel(int size, String... series) { | ||
48 | + checkNotNull(series, "series cannot be null"); | ||
49 | + checkArgument(series.length > 0, "must be at least one series"); | ||
50 | + seriesMap = Maps.newConcurrentMap(); | ||
51 | + | ||
52 | + for (int index = 0; index < series.length; index++) { | ||
53 | + seriesMap.put(series[index], index); | ||
54 | + } | ||
55 | + | ||
56 | + checkArgument(size > 0, "must have at least one data point"); | ||
57 | + dataPoints = new DataPoint[size]; | ||
58 | + } | ||
59 | + | ||
60 | + private void checkDataPoint(DataPoint dataPoint) { | ||
61 | + checkArgument(dataPoint.getSize() == seriesCount(), | ||
62 | + "data size should be equal to number of series"); | ||
63 | + } | ||
64 | + | ||
65 | + /** | ||
66 | + * Returns the number of series in this chart model. | ||
67 | + * | ||
68 | + * @return number of series | ||
69 | + */ | ||
70 | + public int seriesCount() { | ||
71 | + return seriesMap.size(); | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Shifts all of the data points to the left, | ||
76 | + * and adds a new data point to the tail of the array. | ||
77 | + * | ||
78 | + * @param label label name | ||
79 | + * @param values a set of data values | ||
80 | + */ | ||
81 | + public void addDataPoint(String label, Double[] values) { | ||
82 | + DataPoint dp = new DataPoint(label, values); | ||
83 | + checkDataPoint(dp); | ||
84 | + | ||
85 | + for (int index = 1; index < dataPoints.length; index++) { | ||
86 | + dataPoints[index - 1] = dataPoints[index]; | ||
87 | + } | ||
88 | + dataPoints[dataPoints.length - 1] = dp; | ||
89 | + } | ||
90 | + | ||
91 | + /** | ||
92 | + * Returns all of series. | ||
93 | + * | ||
94 | + * @return an array of series | ||
95 | + */ | ||
96 | + public String[] getSeries() { | ||
97 | + return seriesMap.keySet().toArray(new String[seriesMap.size()]); | ||
98 | + } | ||
99 | + | ||
100 | + /** | ||
101 | + * Returns all of data points. | ||
102 | + * | ||
103 | + * @return an array of data points | ||
104 | + */ | ||
105 | + public DataPoint[] getDataPoints() { | ||
106 | + return Arrays.copyOf(dataPoints, dataPoints.length); | ||
107 | + } | ||
108 | + | ||
109 | + /** | ||
110 | + * Returns the last element inside all of data points. | ||
111 | + * | ||
112 | + * @return data point | ||
113 | + */ | ||
114 | + public DataPoint getLastDataPoint() { | ||
115 | + return dataPoints[dataPoints.length - 1]; | ||
116 | + } | ||
117 | + | ||
118 | + /** | ||
119 | + * A class of data point. | ||
120 | + */ | ||
121 | + public class DataPoint { | ||
122 | + // values for all series | ||
123 | + private final Double[] values; | ||
124 | + private final String label; | ||
125 | + | ||
126 | + /** | ||
127 | + * Constructs a data point. | ||
128 | + * | ||
129 | + * @param label label name | ||
130 | + * @param values a set of data values for all series | ||
131 | + */ | ||
132 | + public DataPoint(String label, Double[] values) { | ||
133 | + this.label = label; | ||
134 | + this.values = values; | ||
135 | + } | ||
136 | + | ||
137 | + /** | ||
138 | + * Returns the label name of this data point. | ||
139 | + * | ||
140 | + * @return label name | ||
141 | + */ | ||
142 | + public String getLabel() { | ||
143 | + return label; | ||
144 | + } | ||
145 | + | ||
146 | + /** | ||
147 | + * Returns the size of data point. | ||
148 | + * This should be identical to the size of series. | ||
149 | + * | ||
150 | + * @return size of data point | ||
151 | + */ | ||
152 | + public int getSize() { | ||
153 | + return values.length; | ||
154 | + } | ||
155 | + | ||
156 | + /** | ||
157 | + * Returns the value of the data point of the given series. | ||
158 | + * | ||
159 | + * @param series series name | ||
160 | + * @return data value of a specific series | ||
161 | + */ | ||
162 | + public Double getValue(String series) { | ||
163 | + return values[seriesMap.get(series)]; | ||
164 | + } | ||
165 | + } | ||
166 | +} |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +/** | ||
18 | + * Facilities for creating chart models of data for the GUI. | ||
19 | + */ | ||
20 | +package org.onosproject.ui.chart; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.ui.chart; | ||
17 | + | ||
18 | +import org.junit.Test; | ||
19 | + | ||
20 | +import static org.hamcrest.Matchers.is; | ||
21 | +import static org.junit.Assert.assertArrayEquals; | ||
22 | +import static org.junit.Assert.assertEquals; | ||
23 | +import static org.junit.Assert.assertThat; | ||
24 | + | ||
25 | +/** | ||
26 | + * Unit tests for {@link ChartModel}. | ||
27 | + */ | ||
28 | +public class ChartModelTest { | ||
29 | + | ||
30 | + private static final String FOO = "foo"; | ||
31 | + private static final String BAR = "bar"; | ||
32 | + private static final String ZOO = "zoo"; | ||
33 | + | ||
34 | + private static final Double[] VALUES1 = {0D, 1D, 2D}; | ||
35 | + private static final Double[] VALUES2 = {3D, 4D, 5D}; | ||
36 | + private static final Double[] VALUES3 = {6D, 7D, 8D}; | ||
37 | + | ||
38 | + private static final String[] SERIES = {FOO, BAR, ZOO}; | ||
39 | + | ||
40 | + private ChartModel cm; | ||
41 | + | ||
42 | + @Test(expected = NullPointerException.class) | ||
43 | + public void guardAgainstNullSeries() { | ||
44 | + cm = new ChartModel(1, null); | ||
45 | + } | ||
46 | + | ||
47 | + @Test(expected = IllegalArgumentException.class) | ||
48 | + public void guardAgainstWrongDpNumber() { | ||
49 | + cm = new ChartModel(0, FOO); | ||
50 | + } | ||
51 | + | ||
52 | + @Test | ||
53 | + public void testSeriesCount() { | ||
54 | + cm = new ChartModel(1, FOO, BAR, ZOO); | ||
55 | + assertEquals("Wrong series count", 3, cm.seriesCount()); | ||
56 | + } | ||
57 | + | ||
58 | + @Test | ||
59 | + public void testAddDataPoint() { | ||
60 | + cm = new ChartModel(2, FOO, BAR, ZOO); | ||
61 | + | ||
62 | + cm.addDataPoint("1", VALUES1); | ||
63 | + cm.addDataPoint("2", VALUES2); | ||
64 | + | ||
65 | + assertEquals("Wrong result", "1", cm.getDataPoints()[0].getLabel()); | ||
66 | + assertEquals("Wrong result", "2", cm.getDataPoints()[1].getLabel()); | ||
67 | + | ||
68 | + cm.addDataPoint("3", VALUES3); | ||
69 | + | ||
70 | + assertEquals("Wrong result", "2", cm.getDataPoints()[0].getLabel()); | ||
71 | + assertEquals("Wrong result", "3", cm.getDataPoints()[1].getLabel()); | ||
72 | + } | ||
73 | + | ||
74 | + @Test | ||
75 | + public void testGetData() { | ||
76 | + cm = new ChartModel(2, FOO, BAR, ZOO); | ||
77 | + | ||
78 | + cm.addDataPoint("1", VALUES1); | ||
79 | + assertThat(cm.getLastDataPoint().getValue(ZOO), is(2D)); | ||
80 | + | ||
81 | + cm.addDataPoint("2", VALUES2); | ||
82 | + assertThat(cm.getLastDataPoint().getValue(BAR), is(4D)); | ||
83 | + } | ||
84 | + | ||
85 | + @Test | ||
86 | + public void testGetSeries() { | ||
87 | + cm = new ChartModel(1, FOO, BAR, ZOO); | ||
88 | + | ||
89 | + assertArrayEquals("series", SERIES, cm.getSeries()); | ||
90 | + } | ||
91 | +} |
-
Please register or login to post a comment