Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 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 | * |
Ray Milkey | d7909ca | 2016-03-04 08:46:13 -0800 | [diff] [blame] | 45 | * @param size datapoints size |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 46 | * @param series a set of series |
| 47 | */ |
| 48 | public ChartModel(int size, String... series) { |
| 49 | checkNotNull(series, "series cannot be null"); |
| 50 | checkArgument(series.length > 0, "must be at least one series"); |
| 51 | seriesMap = Maps.newConcurrentMap(); |
| 52 | |
| 53 | for (int index = 0; index < series.length; index++) { |
| 54 | seriesMap.put(series[index], index); |
| 55 | } |
| 56 | |
| 57 | checkArgument(size > 0, "must have at least one data point"); |
| 58 | dataPoints = new DataPoint[size]; |
| 59 | } |
| 60 | |
| 61 | private void checkDataPoint(DataPoint dataPoint) { |
| 62 | checkArgument(dataPoint.getSize() == seriesCount(), |
| 63 | "data size should be equal to number of series"); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the number of series in this chart model. |
| 68 | * |
| 69 | * @return number of series |
| 70 | */ |
| 71 | public int seriesCount() { |
| 72 | return seriesMap.size(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Shifts all of the data points to the left, |
| 77 | * and adds a new data point to the tail of the array. |
| 78 | * |
| 79 | * @param label label name |
| 80 | * @param values a set of data values |
| 81 | */ |
| 82 | public void addDataPoint(String label, Double[] values) { |
| 83 | DataPoint dp = new DataPoint(label, values); |
| 84 | checkDataPoint(dp); |
| 85 | |
| 86 | for (int index = 1; index < dataPoints.length; index++) { |
| 87 | dataPoints[index - 1] = dataPoints[index]; |
| 88 | } |
| 89 | dataPoints[dataPoints.length - 1] = dp; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns all of series. |
| 94 | * |
| 95 | * @return an array of series |
| 96 | */ |
| 97 | public String[] getSeries() { |
| 98 | return seriesMap.keySet().toArray(new String[seriesMap.size()]); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns all of data points. |
| 103 | * |
| 104 | * @return an array of data points |
| 105 | */ |
| 106 | public DataPoint[] getDataPoints() { |
| 107 | return Arrays.copyOf(dataPoints, dataPoints.length); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the last element inside all of data points. |
| 112 | * |
| 113 | * @return data point |
| 114 | */ |
| 115 | public DataPoint getLastDataPoint() { |
| 116 | return dataPoints[dataPoints.length - 1]; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * A class of data point. |
| 121 | */ |
| 122 | public class DataPoint { |
| 123 | // values for all series |
| 124 | private final Double[] values; |
| 125 | private final String label; |
| 126 | |
| 127 | /** |
| 128 | * Constructs a data point. |
| 129 | * |
| 130 | * @param label label name |
| 131 | * @param values a set of data values for all series |
| 132 | */ |
| 133 | public DataPoint(String label, Double[] values) { |
| 134 | this.label = label; |
| 135 | this.values = values; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns the label name of this data point. |
| 140 | * |
| 141 | * @return label name |
| 142 | */ |
| 143 | public String getLabel() { |
| 144 | return label; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the size of data point. |
| 149 | * This should be identical to the size of series. |
| 150 | * |
| 151 | * @return size of data point |
| 152 | */ |
| 153 | public int getSize() { |
| 154 | return values.length; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns the value of the data point of the given series. |
| 159 | * |
| 160 | * @param series series name |
| 161 | * @return data value of a specific series |
| 162 | */ |
| 163 | public Double getValue(String series) { |
| 164 | return values[seriesMap.get(series)]; |
| 165 | } |
| 166 | } |
| 167 | } |