blob: a546aedf56cbc79a75c11b6d987cb17603c4fea3 [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.ConditionSerializer;
24
25import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26
27/**
28 * Condition is a 3-element JSON array of the form [column, function, value]
29 * that represents a test on a column value.
30 */
31@JsonSerialize(using = ConditionSerializer.class)
32public final class Condition {
33 /**
34 * Function of Notation. Refer to RFC 7047 Section 5.1.
35 */
36 public enum Function {
37 LESS_THAN("<"), LESS_THAN_OR_EQUALS("<="), EQUALS("=="),
38 NOT_EQUALS("!="), GREATER_THAN(">"), GREATER_THAN_OR_EQUALS(">="),
39 INCLUDES("includes"), EXCLUDES("excludes");
40
41 private final String function;
42
43 private Function(String function) {
44 this.function = function;
45 }
46
47 /**
48 * Returns the function for Function.
49 * @return the function
50 */
51 public String function() {
52 return function;
53 }
54 }
55
56 private final String column;
57 private final Function function;
58 private final Object value;
59
60 /**
61 * Constructs a Condition object.
62 * @param column the column name
63 * @param function Function
64 * @param value column data
65 */
66 public Condition(String column, Function function, Object value) {
lishuai2f197432015-07-31 16:27:58 +080067 checkNotNull(column, "column cannot be null");
68 checkNotNull(function, "function cannot be null");
69 checkNotNull(value, "value cannot be null");
lishuai91d986c2015-07-28 09:45:20 +080070 this.column = column;
71 this.function = function;
72 this.value = value;
73 }
74
75 /**
76 * Returns column name.
77 * @return column name
78 */
79 public String getColumn() {
80 return column;
81 }
82
83 /**
84 * Returns Function.
85 * @return Function
86 */
87 public Function getFunction() {
88 return function;
89 }
90
91 /**
92 * Returns column data.
93 * @return column data
94 */
95 public Object getValue() {
96 return value;
97 }
98
99 @Override
100 public int hashCode() {
101 return Objects.hash(column, function, value);
102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109 if (obj instanceof Condition) {
110 final Condition other = (Condition) obj;
111 return Objects.equals(this.column, other.column)
112 && Objects.equals(this.function, other.function)
113 && Objects.equals(this.value, other.value);
114 }
115 return false;
116 }
117
118 @Override
119 public String toString() {
120 return toStringHelper(this).add("column", column)
121 .add("function", function).add("value", value).toString();
122 }
123}