Merge "Make local variables creation and assignment in a single statement. Some other...
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / MatchInfo.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.genius.mdsalutil;
9
10 import com.google.common.base.MoreObjects;
11 import java.io.Serializable;
12 import java.math.BigInteger;
13 import java.util.Arrays;
14 import java.util.Map;
15 import java.util.Objects;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
17 import org.opendaylight.yangtools.util.EvenMoreObjects;
18
19 public class MatchInfo implements Serializable, MatchInfoBase {
20     private static final long serialVersionUID = 1L;
21
22     private final MatchFieldType m_matchField;
23     private final long[] m_alMatchValues;
24     private final BigInteger[] m_aBigIntValues;
25     private final String[] m_asMatchValues;
26
27     public MatchInfo(MatchFieldType matchField, long[] alMatchValues) {
28         m_matchField = matchField;
29         m_alMatchValues = alMatchValues;
30         m_aBigIntValues = null;
31         m_asMatchValues = null;
32     }
33
34     public MatchInfo(MatchFieldType matchField, BigInteger[] alBigMatchValues) {
35         m_matchField = matchField;
36         m_alMatchValues = null;
37         m_aBigIntValues = alBigMatchValues;
38         m_asMatchValues = null;
39     }
40
41     public MatchInfo(MatchFieldType matchField, String[] alStringMatchValues) {
42         m_matchField = matchField;
43         m_alMatchValues = null;
44         m_aBigIntValues = null;
45         m_asMatchValues = alStringMatchValues;
46     }
47
48     @Override
49     public void createInnerMatchBuilder(Map<Class<?>, Object> mapMatchBuilder) {
50         m_matchField.createInnerMatchBuilder(this, mapMatchBuilder);
51     }
52
53     @Override
54     public void setMatch(MatchBuilder matchBuilder, Map<Class<?>, Object> mapMatchBuilder) {
55         m_matchField.setMatch(matchBuilder, this, mapMatchBuilder);
56     }
57
58     public MatchFieldType getMatchField() {
59         return m_matchField;
60     }
61
62     public long[] getMatchValues() {
63         return m_alMatchValues;
64     }
65
66     public BigInteger[] getBigMatchValues() {
67         return m_aBigIntValues;
68     }
69
70     public String[] getStringMatchValues() {
71         return m_asMatchValues;
72     }
73
74     @Override
75     public String toString() {
76         return MoreObjects.toStringHelper(this).omitNullValues().add("matchField", m_matchField)
77                 .add("matchValues", Arrays.toString(m_alMatchValues))
78                 .add("bigMatchValues", Arrays.deepToString(m_aBigIntValues))
79                 .add("stringMatchValues", Arrays.deepToString(m_asMatchValues)).toString();
80     }
81
82     @Override
83     public int hashCode() {
84         // BEWARE, Caveat Emptor: Array ([]) type fields must use
85         // Arrays.hashCode(). deepHashCode() would have to be used for nested
86         // arrays.
87         return Objects.hash(m_matchField, Arrays.hashCode(m_alMatchValues),
88                 Arrays.hashCode(m_aBigIntValues), Arrays.hashCode(m_asMatchValues));
89     }
90
91     @Override
92     public boolean equals(Object obj) {
93         // BEWARE, Caveat Emptor: Array ([]) type fields must use
94         // Arrays.equals(). deepEquals() would have to be used for nested
95         // arrays. Use == only for primitive types; if ever changing
96         // those field types, must change to Objects.equals.
97         return EvenMoreObjects.equalsHelper(this, obj,
98             (self, other) -> Objects.equals(self.m_matchField, other.m_matchField)
99                           && Arrays.equals(self.m_alMatchValues, other.m_alMatchValues)
100                           && Arrays.equals(self.m_aBigIntValues, other.m_aBigIntValues)
101                           && Arrays.equals(self.m_asMatchValues, other.m_asMatchValues));
102     }
103
104 }