blob: 00609602c6199c288624ee680e05bcbd2d15a8cd [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -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 */
16package org.onosproject.ovsdb.rfc.notation;
17
andreaed976a42015-10-05 14:38:25 -070018import com.google.common.collect.Maps;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070019
20import java.util.Collection;
21import java.util.Map;
22import java.util.Objects;
23
andreaed976a42015-10-05 14:38:25 -070024import static com.google.common.base.MoreObjects.toStringHelper;
25import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070026
27/**
28 * Row is the basic element of the OpenVswitch's table.
29 */
30public final class Row {
31 private String tableName;
andreaed976a42015-10-05 14:38:25 -070032 private UUID uuid;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070033 private Map<String, Column> columns;
34
35 /**
36 * Row constructor.
37 */
38 public Row() {
39 this.columns = Maps.newHashMap();
40 }
41
42 /**
43 * Row constructor.
andreaed976a42015-10-05 14:38:25 -070044 *
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070045 * @param tableName table name
46 */
andreaed976a42015-10-05 14:38:25 -070047 @Deprecated
48 private Row(String tableName) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070049 checkNotNull(tableName, "tableName cannot be null");
50 this.tableName = tableName;
51 this.columns = Maps.newHashMap();
52 }
53
54 /**
55 * Row constructor.
andreaed976a42015-10-05 14:38:25 -070056 *
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070057 * @param tableName table name
andreaed976a42015-10-05 14:38:25 -070058 * @param columns Map of Column entity
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070059 */
andreaed976a42015-10-05 14:38:25 -070060 public Row(String tableName, UUID uuid, Map<String, Column> columns) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070061 checkNotNull(tableName, "table name cannot be null");
andreaed976a42015-10-05 14:38:25 -070062 checkNotNull(uuid, "uuid cannot be null");
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070063 checkNotNull(columns, "columns cannot be null");
64 this.tableName = tableName;
andreaed976a42015-10-05 14:38:25 -070065 this.uuid = uuid;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070066 this.columns = columns;
67 }
68
69 /**
70 * Returns tableName.
andreaed976a42015-10-05 14:38:25 -070071 *
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070072 * @return tableName
73 */
74 public String tableName() {
75 return tableName;
76 }
77
78 /**
79 * Set tableName value.
andreaed976a42015-10-05 14:38:25 -070080 *
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070081 * @param tableName table name
82 */
83 public void setTableName(String tableName) {
84 this.tableName = tableName;
85 }
86
87 /**
andreaed976a42015-10-05 14:38:25 -070088 * Returns uuid.
89 *
90 * @return uuid
91 */
92 public UUID uuid() {
93 return uuid;
94 }
95
96 /**
97 * Sets uuid value.
98 *
99 * @param uuid new uuid
100 */
101 public void setUuid(UUID uuid) {
102 this.uuid = uuid;
103 }
104
105 /**
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700106 * Returns Column by ColumnSchema.
andreaed976a42015-10-05 14:38:25 -0700107 *
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700108 * @param columnName column name
109 * @return Column
110 */
111 public Column getColumn(String columnName) {
112 return columns.get(columnName);
113 }
114
115 /**
116 * Returns Collection of Column.
andreaed976a42015-10-05 14:38:25 -0700117 *
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700118 * @return Collection of Column
119 */
120 public Collection<Column> getColumns() {
121 return columns.values();
122 }
123
124 /**
125 * add Column.
andreaed976a42015-10-05 14:38:25 -0700126 *
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700127 * @param columnName column name
andreaed976a42015-10-05 14:38:25 -0700128 * @param data Column entity
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700129 */
130 public void addColumn(String columnName, Column data) {
131 this.columns.put(columnName, data);
132 }
133
134 @Override
135 public int hashCode() {
136 return Objects.hash(tableName, columns);
137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (this == obj) {
142 return true;
143 }
144 if (obj instanceof Row) {
145 final Row other = (Row) obj;
146 return Objects.equals(this.tableName, other.tableName)
147 && Objects.equals(this.columns, other.columns);
148 }
149 return false;
150 }
151
152 @Override
153 public String toString() {
154 return toStringHelper(this).add("tableName", tableName)
155 .add("columns", columns).toString();
156 }
157}