lishuai | 91d986c | 2015-07-28 09:45:20 +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 | package org.onosproject.ovsdb.rfc.notation; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Collection; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import org.onosproject.ovsdb.rfc.schema.ColumnSchema; |
| 27 | import org.onosproject.ovsdb.rfc.schema.TableSchema; |
| 28 | |
| 29 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 30 | import com.google.common.collect.Maps; |
| 31 | |
| 32 | /** |
| 33 | * Row is the basic element of the OpenVswitch's table. |
| 34 | */ |
| 35 | public final class Row { |
| 36 | @JsonIgnore |
| 37 | private TableSchema tableSchema; |
| 38 | private Map<String, Column> columns; |
| 39 | |
| 40 | /** |
| 41 | * Row constructor. |
| 42 | */ |
| 43 | public Row() { |
| 44 | this.columns = Maps.newHashMap(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Row constructor. |
| 49 | * @param tableSchema TableSchema entity |
| 50 | */ |
| 51 | public Row(TableSchema tableSchema) { |
lishuai | 2f19743 | 2015-07-31 16:27:58 +0800 | [diff] [blame] | 52 | checkNotNull(tableSchema, "tableSchema cannot be null"); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 53 | this.tableSchema = tableSchema; |
| 54 | this.columns = Maps.newHashMap(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Row constructor. |
| 59 | * @param tableSchema TableSchema entity |
| 60 | * @param columns List of Column entity |
| 61 | */ |
| 62 | public Row(TableSchema tableSchema, List<Column> columns) { |
lishuai | 2f19743 | 2015-07-31 16:27:58 +0800 | [diff] [blame] | 63 | checkNotNull(tableSchema, "tableSchema cannot be null"); |
| 64 | checkNotNull(columns, "columns cannot be null"); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 65 | this.tableSchema = tableSchema; |
| 66 | this.columns = Maps.newHashMap(); |
| 67 | for (Column column : columns) { |
| 68 | this.columns.put(column.schema().name(), column); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns tableSchema. |
| 74 | * @return tableSchema |
| 75 | */ |
| 76 | public TableSchema getTableSchema() { |
| 77 | return tableSchema; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Set tableSchema value. |
| 82 | * @param tableSchema TableSchema entity |
| 83 | */ |
| 84 | public void setTableSchema(TableSchema tableSchema) { |
| 85 | this.tableSchema = tableSchema; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns Column by ColumnSchema. |
| 90 | * @param schema ColumnSchema entity |
| 91 | * @return Column |
| 92 | */ |
| 93 | public Column getColumn(ColumnSchema schema) { |
| 94 | return (Column) columns.get(schema.name()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns Collection of Column. |
| 99 | * @return Collection of Column |
| 100 | */ |
| 101 | public Collection<Column> getColumns() { |
| 102 | return columns.values(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * add Column. |
| 107 | * @param columnName column name |
| 108 | * @param data Column entity |
| 109 | */ |
| 110 | public void addColumn(String columnName, Column data) { |
| 111 | this.columns.put(columnName, data); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
| 116 | return Objects.hash(tableSchema, columns); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean equals(Object obj) { |
| 121 | if (this == obj) { |
| 122 | return true; |
| 123 | } |
| 124 | if (obj instanceof Row) { |
| 125 | final Row other = (Row) obj; |
| 126 | return Objects.equals(this.tableSchema, other.tableSchema) |
| 127 | && Objects.equals(this.columns, other.columns); |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return toStringHelper(this).add("tableSchema", tableSchema).add("columns", columns).toString(); |
| 135 | } |
| 136 | } |