Bri Prebilic Cole | dea0974 | 2015-02-12 15:33:50 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 17 | package org.onosproject.ui.impl; |
Bri Prebilic Cole | dea0974 | 2015-02-12 15:33:50 -0800 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Provides a partial implementation of {@link TableRow}. |
| 28 | */ |
| 29 | public abstract class AbstractTableRow implements TableRow { |
| 30 | |
| 31 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 32 | |
| 33 | private final Map<String, String> data = new HashMap<>(); |
| 34 | |
| 35 | @Override |
| 36 | public String get(String key) { |
| 37 | return data.get(key); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public ObjectNode toJsonNode() { |
| 42 | ObjectNode result = MAPPER.createObjectNode(); |
| 43 | for (String id : columnIds()) { |
| 44 | result.put(id, data.get(id)); |
| 45 | } |
| 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 | /** |
| 57 | * Add a column ID to value binding. |
| 58 | * |
| 59 | * @param id the column ID |
| 60 | * @param value the cell value |
| 61 | */ |
| 62 | protected void add(String id, String value) { |
| 63 | data.put(id, value); |
| 64 | } |
| 65 | } |