blob: 27a4f54883af8c833bd1c48dbc99d5852f0faf4d [file] [log] [blame]
lishuai91d986c2015-07-28 09:45:20 +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 */
16package org.onosproject.ovsdb.rfc.notation;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.Collection;
22import java.util.List;
23import java.util.Map;
24import java.util.Objects;
25
26import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
27import org.onosproject.ovsdb.rfc.schema.TableSchema;
28
29import com.fasterxml.jackson.annotation.JsonIgnore;
30import com.google.common.collect.Maps;
31
32/**
33 * Row is the basic element of the OpenVswitch's table.
34 */
35public 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) {
lishuai2f197432015-07-31 16:27:58 +080052 checkNotNull(tableSchema, "tableSchema cannot be null");
lishuai91d986c2015-07-28 09:45:20 +080053 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) {
lishuai2f197432015-07-31 16:27:58 +080063 checkNotNull(tableSchema, "tableSchema cannot be null");
64 checkNotNull(columns, "columns cannot be null");
lishuai91d986c2015-07-28 09:45:20 +080065 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}