blob: 7815ee31836d84fb2fb9ed8c5429e52ad414a14f [file] [log] [blame]
lishuai91d986c2015-07-28 09:45:20 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
lishuai91d986c2015-07-28 09:45:20 +08003 *
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.Objects;
22
23import org.onosproject.ovsdb.rfc.notation.json.MutationSerializer;
24
25import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26
27/**
28 * Mutation is s 3-element JSON array of the form [column, mutator, value] that
29 * represents a change to a column value.
30 */
31@JsonSerialize(using = MutationSerializer.class)
32public final class Mutation {
33 /**
34 * Mutator must be "+=", "-=", "*=", "/=", or (integer only) "%=". The value
35 * of column is changed to the sum, difference, product, quotient, or
36 * remainder, respectively, of column and value.
37 */
38 public enum Mutator {
39 SUM("+="), DIFFERENCE("-="), PRODUCT("*="), QUOTIENT("/="),
40 REMAINDER("%="), INSERT("insert"), DELETE("delete");
41
42 private final String mutator;
43
44 private Mutator(String mutator) {
45 this.mutator = mutator;
46 }
47
48 /**
49 * Returns the mutator for Mutator.
50 * @return the mutator
51 */
52 public String mutator() {
53 return mutator;
54 }
55 }
56
57 private final String column;
58 private final Mutator mutator;
59 private final Object value;
60
61 /**
62 * Mutation constructor.
63 * @param column the column name
64 * @param mutator Mutator
65 * @param value column data
66 */
67 public Mutation(String column, Mutator mutator, Object value) {
lishuai2f197432015-07-31 16:27:58 +080068 checkNotNull(column, "column cannot be null");
69 checkNotNull(mutator, "mutator cannot be null");
70 checkNotNull(value, "value cannot be null");
lishuai91d986c2015-07-28 09:45:20 +080071 this.column = column;
72 this.mutator = mutator;
73 this.value = value;
74 }
75
76 /**
77 * Returns column name.
78 * @return column name
79 */
80 public String getColumn() {
81 return column;
82 }
83
84 /**
85 * Returns Mutator.
86 * @return Mutator
87 */
88 public Mutator getMutator() {
89 return mutator;
90 }
91
92 /**
93 * Returns column data.
94 * @return column data
95 */
96 public Object getValue() {
97 return value;
98 }
99
100 @Override
101 public int hashCode() {
102 return Objects.hash(column, mutator, value);
103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) {
108 return true;
109 }
110 if (obj instanceof Mutation) {
111 final Mutation other = (Mutation) obj;
112 return Objects.equals(this.column, other.column)
113 && Objects.equals(this.mutator, other.mutator)
114 && Objects.equals(this.value, other.value);
115 }
116 return false;
117 }
118
119 @Override
120 public String toString() {
121 return toStringHelper(this).add("column", column)
122 .add("mutator", mutator).add("value", value).toString();
123 }
124}