Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 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.table; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 20 | import org.onosproject.ui.table.cell.DefaultCellComparator; |
| 21 | import org.onosproject.ui.table.cell.DefaultCellFormatter; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Arrays; |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 25 | import java.util.Collection; |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 26 | import java.util.Collections; |
| 27 | import java.util.Comparator; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.List; |
| 30 | import java.util.Map; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkArgument; |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /** |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 37 | * A simple model of table data. |
| 38 | * <p> |
| 39 | * Note that this is not a full MVC type model; the expected usage pattern |
| 40 | * is to create an empty table, add rows (by consulting the business model), |
| 41 | * sort rows (based on client request parameters), and finally produce the |
| 42 | * sorted list of rows. |
| 43 | * <p> |
| 44 | * The table also provides a mechanism for defining how cell values for a |
| 45 | * particular column should be formatted into strings, to help facilitate |
| 46 | * the encoding of the table data into a JSON structure. |
Simon Hunt | 27bf079 | 2015-05-07 10:50:29 -0700 | [diff] [blame] | 47 | * <p> |
| 48 | * Note that it is expected that all values for a particular column will |
| 49 | * be the same class. |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 50 | */ |
| 51 | public class TableModel { |
| 52 | |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 53 | private static final String DESC = "desc"; |
| 54 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 55 | private static final CellComparator DEF_CMP = DefaultCellComparator.INSTANCE; |
| 56 | private static final CellFormatter DEF_FMT = DefaultCellFormatter.INSTANCE; |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 57 | private static final String EMPTY = ""; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 58 | |
| 59 | private final String[] columnIds; |
| 60 | private final Set<String> idSet; |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 61 | private final Map<String, CellComparator> comparators = new HashMap<>(); |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 62 | private final Map<String, CellFormatter> formatters = new HashMap<>(); |
| 63 | private final List<Row> rows = new ArrayList<>(); |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 64 | private final Map<String, Annot> annotations = new HashMap<>(); |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Constructs a table (devoid of data) with the given column IDs. |
| 68 | * |
| 69 | * @param columnIds column identifiers |
| 70 | */ |
| 71 | public TableModel(String... columnIds) { |
| 72 | checkNotNull(columnIds, "columnIds cannot be null"); |
| 73 | checkArgument(columnIds.length > 0, "must be at least one column"); |
| 74 | |
| 75 | idSet = Sets.newHashSet(columnIds); |
| 76 | if (idSet.size() != columnIds.length) { |
| 77 | throw new IllegalArgumentException("duplicate column ID(s) detected"); |
| 78 | } |
| 79 | |
| 80 | this.columnIds = Arrays.copyOf(columnIds, columnIds.length); |
| 81 | } |
| 82 | |
| 83 | private void checkId(String id) { |
| 84 | checkNotNull(id, "must provide a column ID"); |
| 85 | if (!idSet.contains(id)) { |
| 86 | throw new IllegalArgumentException("unknown column id: " + id); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the number of rows in this table model. |
| 92 | * |
| 93 | * @return number of rows |
| 94 | */ |
| 95 | public int rowCount() { |
| 96 | return rows.size(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the number of columns in this table model. |
| 101 | * |
| 102 | * @return number of columns |
| 103 | */ |
| 104 | public int columnCount() { |
| 105 | return columnIds.length; |
| 106 | } |
| 107 | |
| 108 | /** |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 109 | * Returns the array of column IDs for this table model. |
| 110 | * <p> |
| 111 | * Implementation note: we are knowingly passing you a reference to |
| 112 | * our internal array to avoid copying. Don't mess with it. It's your |
| 113 | * table you'll break if you do! |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 114 | * |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 115 | * @return the column identifiers |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 116 | */ |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 117 | public String[] getColumnIds() { |
| 118 | return columnIds; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the raw {@link Row} representation of the rows in this table. |
| 123 | * |
| 124 | * @return raw table rows |
| 125 | */ |
| 126 | public Row[] getRows() { |
| 127 | return rows.toArray(new Row[rows.size()]); |
| 128 | } |
| 129 | |
| 130 | /** |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 131 | * Inserts a new annotation. |
| 132 | * |
| 133 | * @param key key of annotation |
| 134 | * @param value value of annotation |
| 135 | */ |
| 136 | public void addAnnotation(String key, Object value) { |
| 137 | Annot annot = new Annot(key, value); |
| 138 | annotations.put(key, annot); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the annotations in this table. |
| 143 | * |
| 144 | * @return annotations |
| 145 | */ |
| 146 | public Collection<Annot> getAnnotations() { |
| 147 | Collection<Annot> annots = new ArrayList<>(annotations.size()); |
| 148 | annotations.forEach((k, v) -> annots.add(v)); |
| 149 | return annots; |
| 150 | } |
| 151 | |
| 152 | /** |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 153 | * Sets a cell comparator for the specified column. |
| 154 | * |
| 155 | * @param columnId column identifier |
| 156 | * @param comparator comparator to use |
| 157 | */ |
| 158 | public void setComparator(String columnId, CellComparator comparator) { |
| 159 | checkNotNull(comparator, "must provide a comparator"); |
| 160 | checkId(columnId); |
| 161 | comparators.put(columnId, comparator); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns the cell comparator to use on values in the specified column. |
| 166 | * |
| 167 | * @param columnId column identifier |
| 168 | * @return an appropriate cell comparator |
| 169 | */ |
| 170 | private CellComparator getComparator(String columnId) { |
| 171 | checkId(columnId); |
| 172 | CellComparator cmp = comparators.get(columnId); |
| 173 | return cmp == null ? DEF_CMP : cmp; |
| 174 | } |
| 175 | |
| 176 | /** |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 177 | * Sets a cell formatter for the specified column. |
| 178 | * |
| 179 | * @param columnId column identifier |
| 180 | * @param formatter formatter to use |
| 181 | */ |
| 182 | public void setFormatter(String columnId, CellFormatter formatter) { |
| 183 | checkNotNull(formatter, "must provide a formatter"); |
| 184 | checkId(columnId); |
| 185 | formatters.put(columnId, formatter); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns the cell formatter to use on values in the specified column. |
| 190 | * |
| 191 | * @param columnId column identifier |
| 192 | * @return an appropriate cell formatter |
| 193 | */ |
| 194 | public CellFormatter getFormatter(String columnId) { |
| 195 | checkId(columnId); |
| 196 | CellFormatter fmt = formatters.get(columnId); |
| 197 | return fmt == null ? DEF_FMT : fmt; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Adds a row to the table model. |
| 202 | * |
| 203 | * @return the row, for chaining |
| 204 | */ |
| 205 | public Row addRow() { |
| 206 | Row r = new Row(); |
| 207 | rows.add(r); |
| 208 | return r; |
| 209 | } |
| 210 | |
| 211 | /** |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 212 | * Sorts the table rows based on the specified columns, in the |
| 213 | * specified directions. The second column is optional, and can be |
| 214 | * disregarded by passing null into id2 and dir2. |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 215 | * |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 216 | * @param id1 first column identifier |
| 217 | * @param dir1 first column sort direction |
| 218 | * @param id2 second column identifier (may be null) |
| 219 | * @param dir2 second column sort direction (may be null) |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 220 | */ |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 221 | public void sort(String id1, SortDir dir1, String id2, SortDir dir2) { |
| 222 | Collections.sort(rows, new RowComparator(id1, dir1, id2, dir2)); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 225 | private boolean nullOrEmpty(String s) { |
| 226 | return s == null || EMPTY.equals(s.trim()); |
| 227 | } |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 228 | |
| 229 | /** Designates sorting direction. */ |
| 230 | public enum SortDir { |
| 231 | /** Designates an ascending sort. */ |
| 232 | ASC, |
| 233 | /** Designates a descending sort. */ |
| 234 | DESC |
| 235 | } |
| 236 | |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 237 | |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 238 | /** |
| 239 | * Row comparator. |
| 240 | */ |
| 241 | private class RowComparator implements Comparator<Row> { |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 242 | private final String id1; |
| 243 | private final SortDir dir1; |
| 244 | private final String id2; |
| 245 | private final SortDir dir2; |
| 246 | private final CellComparator cc1; |
| 247 | private final CellComparator cc2; |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Constructs a row comparator based on the specified |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 251 | * column identifiers and sort directions. Note that id2 and dir2 may |
| 252 | * be null. |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 253 | * |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 254 | * @param id1 first column identifier |
| 255 | * @param dir1 first column sort direction |
| 256 | * @param id2 second column identifier |
| 257 | * @param dir2 second column sort direction |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 258 | */ |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 259 | public RowComparator(String id1, SortDir dir1, String id2, SortDir dir2) { |
| 260 | this.id1 = id1; |
| 261 | this.dir1 = dir1; |
| 262 | this.id2 = id2; |
| 263 | this.dir2 = dir2; |
| 264 | cc1 = getComparator(id1); |
| 265 | cc2 = nullOrEmpty(id2) ? null : getComparator(id2); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public int compare(Row a, Row b) { |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 270 | Object cellA = a.get(id1); |
| 271 | Object cellB = b.get(id1); |
| 272 | int result = cc1.compare(cellA, cellB); |
| 273 | result = dir1 == SortDir.ASC ? result : -result; |
| 274 | |
| 275 | if (result == 0 && cc2 != null) { |
| 276 | cellA = a.get(id2); |
| 277 | cellB = b.get(id2); |
| 278 | result = cc2.compare(cellA, cellB); |
| 279 | result = dir2 == SortDir.ASC ? result : -result; |
| 280 | } |
| 281 | return result; |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 286 | * Model of an annotation. |
| 287 | */ |
| 288 | public class Annot { |
| 289 | private final String key; |
| 290 | private final Object value; |
| 291 | |
| 292 | /** |
| 293 | * Constructs an annotation with the given key and value. |
| 294 | * |
| 295 | * @param key the key |
| 296 | * @param value the value |
| 297 | */ |
| 298 | public Annot(String key, Object value) { |
| 299 | this.key = key; |
| 300 | this.value = value; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Returns the annotation's key. |
| 305 | * |
| 306 | * @return key |
| 307 | */ |
| 308 | public String key() { |
| 309 | return key; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Returns the annotation's value. |
| 314 | * |
| 315 | * @return value |
| 316 | */ |
| 317 | public Object value() { |
| 318 | return value; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Returns the value as a string. |
| 323 | * This default implementation uses the value's toString() method. |
| 324 | * |
| 325 | * @return the value as a string |
| 326 | */ |
| 327 | public String valueAsString() { |
| 328 | return value.toString(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 333 | * Model of a row. |
| 334 | */ |
| 335 | public class Row { |
| 336 | private final Map<String, Object> cells = new HashMap<>(); |
| 337 | |
| 338 | /** |
| 339 | * Sets the cell value for the given column of this row. |
| 340 | * |
| 341 | * @param columnId column identifier |
| 342 | * @param value value to set |
| 343 | * @return self, for chaining |
| 344 | */ |
| 345 | public Row cell(String columnId, Object value) { |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 346 | checkId(columnId); |
| 347 | cells.put(columnId, value); |
| 348 | return this; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Returns the value of the cell in the given column for this row. |
| 353 | * |
| 354 | * @param columnId column identifier |
| 355 | * @return cell value |
| 356 | */ |
| 357 | public Object get(String columnId) { |
| 358 | return cells.get(columnId); |
| 359 | } |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Returns the value of the cell as a string, using the |
| 363 | * formatter appropriate for the column. |
| 364 | * |
| 365 | * @param columnId column identifier |
| 366 | * @return formatted cell value |
| 367 | */ |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 368 | String getAsString(String columnId) { |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 369 | return getFormatter(columnId).format(get(columnId)); |
| 370 | } |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 371 | |
| 372 | /** |
| 373 | * Returns the row as an array of formatted strings. |
| 374 | * |
| 375 | * @return the formatted row data |
| 376 | */ |
| 377 | public String[] getAsFormattedStrings() { |
| 378 | List<String> formatted = new ArrayList<>(columnCount()); |
| 379 | for (String c : columnIds) { |
| 380 | formatted.add(getAsString(c)); |
| 381 | } |
| 382 | return formatted.toArray(new String[formatted.size()]); |
| 383 | } |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 384 | } |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 385 | |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 386 | /** |
| 387 | * Returns the appropriate sort direction for the given string. |
| 388 | * <p> |
| 389 | * The expected strings are "asc" for {@link SortDir#ASC ascending} and |
| 390 | * "desc" for {@link SortDir#DESC descending}. Any other value will |
| 391 | * default to ascending. |
| 392 | * |
| 393 | * @param s sort direction string encoding |
| 394 | * @return sort direction |
| 395 | */ |
| 396 | public static SortDir sortDir(String s) { |
| 397 | return !DESC.equals(s) ? SortDir.ASC : SortDir.DESC; |
| 398 | } |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 399 | } |