Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 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 | */ |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 16 | package org.onlab.util; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | |
| 20 | import java.util.Arrays; |
| 21 | import java.util.Objects; |
| 22 | import java.util.function.Function; |
| 23 | |
| 24 | /** |
| 25 | * Utility class for checking matching values. |
| 26 | * |
| 27 | * @param <T> type of value |
| 28 | */ |
| 29 | public final class Match<T> { |
| 30 | |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 31 | public static final Match ANY = new Match<>(); |
| 32 | public static final Match NULL = new Match<>(null, false); |
| 33 | public static final Match NOT_NULL = new Match<>(null, true); |
| 34 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 35 | private final boolean matchAny; |
| 36 | private final T value; |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 37 | private final boolean negation; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 40 | * Returns a Match that matches any value including null. |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 41 | * @param <T> match type |
| 42 | * @return new instance |
| 43 | */ |
| 44 | public static <T> Match<T> any() { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 45 | return ANY; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns a Match that matches null values. |
| 50 | * @param <T> match type |
| 51 | * @return new instance |
| 52 | */ |
| 53 | public static <T> Match<T> ifNull() { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 54 | return NULL; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 58 | * Returns a Match that matches all non-null values. |
| 59 | * @param <T> match type |
| 60 | * @return new instance |
| 61 | */ |
| 62 | public static <T> Match<T> ifNotNull() { |
| 63 | return NOT_NULL; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns a Match that only matches the specified value. |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 68 | * @param value value to match |
| 69 | * @param <T> match type |
| 70 | * @return new instance |
| 71 | */ |
| 72 | public static <T> Match<T> ifValue(T value) { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 73 | return new Match<>(value, false); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns a Match that matches any value except the specified value. |
| 78 | * @param value value to not match |
| 79 | * @param <T> match type |
| 80 | * @return new instance |
| 81 | */ |
| 82 | public static <T> Match<T> ifNotValue(T value) { |
| 83 | return new Match<>(value, true); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private Match() { |
| 87 | matchAny = true; |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 88 | negation = false; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 89 | value = null; |
| 90 | } |
| 91 | |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 92 | private Match(T value, boolean negation) { |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 93 | matchAny = false; |
| 94 | this.value = value; |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 95 | this.negation = negation; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Maps this instance to a Match of another type. |
| 100 | * @param mapper transformation function |
| 101 | * @param <V> new match type |
| 102 | * @return new instance |
| 103 | */ |
| 104 | public <V> Match<V> map(Function<T, V> mapper) { |
| 105 | if (matchAny) { |
| 106 | return any(); |
| 107 | } else if (value == null) { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 108 | return negation ? ifNotNull() : ifNull(); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 109 | } else { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 110 | return negation ? ifNotValue(mapper.apply(value)) : ifValue(mapper.apply(value)); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Checks if this instance matches specified value. |
| 116 | * @param other other value |
| 117 | * @return true if matches; false otherwise |
| 118 | */ |
| 119 | public boolean matches(T other) { |
| 120 | if (matchAny) { |
| 121 | return true; |
| 122 | } else if (other == null) { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 123 | return negation ? value != null : value == null; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 124 | } else { |
| 125 | if (value instanceof byte[]) { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 126 | boolean equal = Arrays.equals((byte[]) value, (byte[]) other); |
| 127 | return negation ? !equal : equal; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 128 | } |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 129 | return negation ? !Objects.equals(value, other) : Objects.equals(value, other); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | @Override |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 134 | public int hashCode() { |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 135 | return Objects.hash(matchAny, value, negation); |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public boolean equals(Object other) { |
| 140 | if (!(other instanceof Match)) { |
| 141 | return false; |
| 142 | } |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 143 | Match<T> that = (Match<T>) other; |
Madan Jampani | 329dd1b | 2016-03-22 15:03:53 -0700 | [diff] [blame] | 144 | return this.matchAny == that.matchAny && |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 145 | Objects.equals(this.value, that.value) && |
Madan Jampani | 329dd1b | 2016-03-22 15:03:53 -0700 | [diff] [blame] | 146 | this.negation == that.negation; |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 150 | public String toString() { |
| 151 | return toStringHelper(this) |
| 152 | .add("matchAny", matchAny) |
Madan Jampani | f2f086c | 2016-01-13 16:15:39 -0800 | [diff] [blame] | 153 | .add("negation", negation) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 154 | .add("value", value) |
| 155 | .toString(); |
| 156 | } |
| 157 | } |