Fix ALOTOF Checkstyle violation, and switch over to enforcement.
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / notation / Row.java
1 /*
2  * Copyright (c) 2014, 2015 EBay Software Foundation and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.lib.notation;
10
11 import com.fasterxml.jackson.annotation.JsonIgnore;
12 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
13 import com.google.common.collect.Maps;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Map;
17 import org.opendaylight.ovsdb.lib.notation.json.RowSerializer;
18 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
19 import org.opendaylight.ovsdb.lib.schema.TableSchema;
20
21 @JsonSerialize(using = RowSerializer.class)
22 public class Row<E extends TableSchema<E>> {
23     @JsonIgnore
24     private TableSchema<E> tableSchema;
25     protected Map<String, Column<E, ?>> columns;
26
27     public Row() {
28         this.columns = Maps.newHashMap();
29     }
30
31     public Row(TableSchema<E> tableSchema) {
32         this.tableSchema = tableSchema;
33         this.columns = Maps.newHashMap();
34     }
35
36     public Row(TableSchema<E> tableSchema, List<Column<E, ?>> columns) {
37         this.tableSchema = tableSchema;
38         this.columns = Maps.newHashMap();
39         for (Column<E, ?> column : columns) {
40             this.columns.put(column.getSchema().getName(), column);
41         }
42     }
43
44     public <D> Column<E, D> getColumn(ColumnSchema<E, D> schema) {
45         return (Column<E, D>) columns.get(schema.getName());
46     }
47
48     public Collection<Column<E, ?>> getColumns() {
49         return columns.values();
50     }
51
52     public void addColumn(String columnName, Column<E, ?> data) {
53         this.columns.put(columnName, data);
54     }
55
56     public TableSchema<E> getTableSchema() {
57         return tableSchema;
58     }
59
60     public void setTableSchema(TableSchema<E> tableSchema) {
61         this.tableSchema = tableSchema;
62     }
63
64     @Override
65     public String toString() {
66         return "Row [columns=" + columns + "]";
67     }
68
69     /**
70      * The hashCode method for Row object should be used with caution.
71      * This method will use all the columns in the row to calculate the hashKey.
72      * Hence using this method on a partial Row will return a different hashKey
73      * and will not work in most of the use-cases this method might be used.
74      */
75     @Override
76     public int hashCode() {
77         final int prime = 31;
78         int result = 1;
79         result = prime * result + ((columns == null) ? 0 : columns.hashCode());
80         return result;
81     }
82
83     /**
84      * The equals method for Row object should be used with caution.
85      * This method will compare all the columns in the row being compared.
86      * Hence using this method to compare a partial Row will return false
87      * and will not work in most of the use-cases this method might be used.
88      */
89     @Override
90     public boolean equals(Object obj) {
91         if (this == obj) {
92             return true;
93         }
94         if (obj == null) {
95             return false;
96         }
97         if (getClass() != obj.getClass()) {
98             return false;
99         }
100         Row other = (Row) obj;
101         if (columns == null) {
102             if (other.columns != null) {
103                 return false;
104             }
105         } else if (!columns.equals(other.columns)) {
106             return false;
107         }
108         return true;
109     }
110 }