blob: 3ff78d9e5a5386476d36379d9cafc7d2c6754626 [file] [log] [blame]
Simon Hunte9828152015-05-01 17:54:25 -07001/*
2 * Copyright 2015 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
17package org.onosproject.ui.table;
18
19import com.google.common.collect.Sets;
20
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26import java.util.Set;
27
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * A model of table data.
33 */
34public class TableModel {
35
36 private static final CellFormatter DEF_FMT = new DefaultCellFormatter();
37
38 private final String[] columnIds;
39 private final Set<String> idSet;
40 private final Map<String, CellFormatter> formatters = new HashMap<>();
41 private final List<Row> rows = new ArrayList<>();
42
43
44 /**
45 * Constructs a table (devoid of data) with the given column IDs.
46 *
47 * @param columnIds column identifiers
48 */
49 public TableModel(String... columnIds) {
50 checkNotNull(columnIds, "columnIds cannot be null");
51 checkArgument(columnIds.length > 0, "must be at least one column");
52
53 idSet = Sets.newHashSet(columnIds);
54 if (idSet.size() != columnIds.length) {
55 throw new IllegalArgumentException("duplicate column ID(s) detected");
56 }
57
58 this.columnIds = Arrays.copyOf(columnIds, columnIds.length);
59 }
60
61 private void checkId(String id) {
62 checkNotNull(id, "must provide a column ID");
63 if (!idSet.contains(id)) {
64 throw new IllegalArgumentException("unknown column id: " + id);
65 }
66 }
67
68 /**
69 * Returns the number of rows in this table model.
70 *
71 * @return number of rows
72 */
73 public int rowCount() {
74 return rows.size();
75 }
76
77 /**
78 * Returns the number of columns in this table model.
79 *
80 * @return number of columns
81 */
82 public int columnCount() {
83 return columnIds.length;
84 }
85
86 /**
87 * Returns the {@link TableRow} representation of the rows in this table.
88 *
89 * @return formatted table rows
90 */
91 public TableRow[] getTableRows() {
92 return new TableRow[0];
93 }
94
95 /**
96 * Returns the raw {@link Row} representation of the rows in this table.
97 *
98 * @return raw table rows
99 */
100 public Row[] getRows() {
101 return rows.toArray(new Row[rows.size()]);
102 }
103
104 /**
105 * Sets a cell formatter for the specified column.
106 *
107 * @param columnId column identifier
108 * @param formatter formatter to use
109 */
110 public void setFormatter(String columnId, CellFormatter formatter) {
111 checkNotNull(formatter, "must provide a formatter");
112 checkId(columnId);
113 formatters.put(columnId, formatter);
114 }
115
116 /**
117 * Returns the cell formatter to use on values in the specified column.
118 *
119 * @param columnId column identifier
120 * @return an appropriate cell formatter
121 */
122 public CellFormatter getFormatter(String columnId) {
123 checkId(columnId);
124 CellFormatter fmt = formatters.get(columnId);
125 return fmt == null ? DEF_FMT : fmt;
126 }
127
128 /**
129 * Adds a row to the table model.
130 *
131 * @return the row, for chaining
132 */
133 public Row addRow() {
134 Row r = new Row();
135 rows.add(r);
136 return r;
137 }
138
139 /**
140 * Model of a row.
141 */
142 public class Row {
143 private final Map<String, Object> cells = new HashMap<>();
144
145 /**
146 * Sets the cell value for the given column of this row.
147 *
148 * @param columnId column identifier
149 * @param value value to set
150 * @return self, for chaining
151 */
152 public Row cell(String columnId, Object value) {
153 checkNotNull(value, "Must supply some value");
154 checkId(columnId);
155 cells.put(columnId, value);
156 return this;
157 }
158
159 /**
160 * Returns the value of the cell in the given column for this row.
161 *
162 * @param columnId column identifier
163 * @return cell value
164 */
165 public Object get(String columnId) {
166 return cells.get(columnId);
167 }
168 }
169}