lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.ovsdb.rfc.notation; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import org.onosproject.ovsdb.rfc.notation.json.ConditionSerializer; |
| 24 | |
| 25 | import 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) |
| 32 | public 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) { |
| 67 | checkNotNull(column, "column is not null"); |
| 68 | checkNotNull(function, "function is not null"); |
| 69 | checkNotNull(value, "value is not null"); |
| 70 | 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 | } |