blob: 32a439662e671e51189c3b688d9960afa5ad8a9f [file] [log] [blame]
Bri Prebilic Coledea09742015-02-12 15:33:50 -08001/*
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
Simon Hunt44aa2f82015-04-30 15:01:35 -070017package org.onosproject.ui.table;
Bri Prebilic Coledea09742015-02-12 15:33:50 -080018
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
22import java.util.HashMap;
23import java.util.Map;
24
25
26/**
27 * Provides a partial implementation of {@link TableRow}.
28 */
29public abstract class AbstractTableRow implements TableRow {
30
31 private static final ObjectMapper MAPPER = new ObjectMapper();
32
Simon Hunt44aa2f82015-04-30 15:01:35 -070033 private final Map<String, String> cells = new HashMap<>();
Bri Prebilic Coledea09742015-02-12 15:33:50 -080034
35 @Override
36 public String get(String key) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070037 return cells.get(key);
Bri Prebilic Coledea09742015-02-12 15:33:50 -080038 }
39
40 @Override
41 public ObjectNode toJsonNode() {
42 ObjectNode result = MAPPER.createObjectNode();
43 for (String id : columnIds()) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070044 result.put(id, cells.get(id));
Bri Prebilic Coledea09742015-02-12 15:33:50 -080045 }
46 return result;
47 }
48
49 /**
50 * Subclasses must provide the list of column IDs.
51 *
52 * @return array of column IDs
53 */
54 protected abstract String[] columnIds();
55
56 /**
Simon Hunt44aa2f82015-04-30 15:01:35 -070057 * Add a column ID to cell value binding.
Bri Prebilic Coledea09742015-02-12 15:33:50 -080058 *
59 * @param id the column ID
60 * @param value the cell value
61 */
62 protected void add(String id, String value) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070063 cells.put(id, value);
64 }
65
66 /**
67 * Add a column ID to cell value binding.
68 * Note that value.toString() is invoked.
69 *
70 * @param id the column ID
71 * @param value the cell value
72 */
73 protected void add(String id, Object value) {
74 cells.put(id, value.toString());
Bri Prebilic Coledea09742015-02-12 15:33:50 -080075 }
76}