892fe8685f22955f1f20ea597d9440ab95223e18
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / ActionInfo.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.Objects;
15 import org.opendaylight.genius.utils.MoreObjects2;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
17
18 public class ActionInfo implements Serializable {
19
20     private static final long serialVersionUID = 1L;
21
22     private final ActionType m_actionType;
23     private final String[] m_asActionValues;
24     private final BigInteger[] m_aBigIntValues;
25     private final int m_actionKey;
26
27     public ActionInfo(ActionInfo action) {
28         super();
29         m_actionType = action.m_actionType;
30         m_actionKey = action.m_actionKey;
31         m_asActionValues = Arrays.copyOf(action.m_asActionValues, action.m_asActionValues.length);
32         m_aBigIntValues = null;
33     }
34
35     public ActionInfo(ActionType actionType, String[] asActionValues) {
36         m_actionType = actionType;
37         m_actionKey = 0;
38         m_asActionValues = asActionValues;
39         m_aBigIntValues = null;
40     }
41
42     public ActionInfo(ActionType actionType, String[] asActionValues, int actionKey) {
43         m_actionType = actionType;
44         m_actionKey = actionKey;
45         m_asActionValues = asActionValues;
46         m_aBigIntValues = null;
47     }
48
49     public ActionInfo(ActionType actionType, BigInteger[] aBigIntValues) {
50         m_actionType = actionType;
51         m_actionKey = 0;
52         m_aBigIntValues = aBigIntValues;
53         m_asActionValues = null;
54     }
55
56     public ActionInfo(ActionType actionType, BigInteger[] aBigIntValues, int actionKey) {
57         m_actionType = actionType;
58         m_actionKey = actionKey;
59         m_aBigIntValues = aBigIntValues;
60         m_asActionValues = null;
61     }
62
63     public int getActionKey() {
64         return m_actionKey;
65     }
66
67     public Action buildAction() {
68         return m_actionType.buildAction(getActionKey(), this);
69     }
70
71     public ActionType getActionType() {
72         return m_actionType;
73     }
74
75     public String[] getActionValues() {
76         return m_asActionValues;
77     }
78
79     public BigInteger[] getBigActionValues() {
80         return m_aBigIntValues;
81     }
82
83     @Override
84     public String toString() {
85         return MoreObjects.toStringHelper(this).omitNullValues().add("actionType", m_actionType)
86                 .add("actionValues", Arrays.deepToString(m_asActionValues))
87                 .add("bigActionValues", Arrays.deepToString(m_aBigIntValues))
88                 .add("actionKey", m_actionKey).toString();
89     }
90
91     @Override
92     public int hashCode() {
93         // BEWARE, Caveat Emptor: Array ([]) type fields must use
94         // Arrays.hashCode(). deepHashCode() would have to be used for nested
95         // arrays.
96         return Objects.hash(m_actionType, Arrays.hashCode(m_asActionValues), Arrays.hashCode(m_aBigIntValues),
97                 m_actionKey);
98     }
99
100     @Override
101     public boolean equals(Object obj) {
102         // BEWARE, Caveat Emptor: Array ([]) type fields must use
103         // Arrays.equals(). deepEquals() would have to be used for nested
104         // arrays. Use == only for primitive types; if ever changing
105         // those field types, must change to Objects.equals.
106         return MoreObjects2.equalsHelper(this, obj,
107             (self, other) -> Objects.equals(self.m_actionType, other.m_actionType)
108                           && Arrays.equals(self.m_asActionValues, other.m_asActionValues)
109                           && Arrays.equals(self.m_aBigIntValues, other.m_aBigIntValues)
110                           && self.m_actionKey == other.m_actionKey);
111     }
112 }