Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [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.store.consistent.impl; |
| 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 | |
| 31 | private final boolean matchAny; |
| 32 | private final T value; |
| 33 | |
| 34 | /** |
| 35 | * Returns a Match that matches any value. |
| 36 | * @param <T> match type |
| 37 | * @return new instance |
| 38 | */ |
| 39 | public static <T> Match<T> any() { |
| 40 | return new Match<>(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns a Match that matches null values. |
| 45 | * @param <T> match type |
| 46 | * @return new instance |
| 47 | */ |
| 48 | public static <T> Match<T> ifNull() { |
| 49 | return ifValue(null); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns a Match that matches only specified value. |
| 54 | * @param value value to match |
| 55 | * @param <T> match type |
| 56 | * @return new instance |
| 57 | */ |
| 58 | public static <T> Match<T> ifValue(T value) { |
| 59 | return new Match<>(value); |
| 60 | } |
| 61 | |
| 62 | private Match() { |
| 63 | matchAny = true; |
| 64 | value = null; |
| 65 | } |
| 66 | |
| 67 | private Match(T value) { |
| 68 | matchAny = false; |
| 69 | this.value = value; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Maps this instance to a Match of another type. |
| 74 | * @param mapper transformation function |
| 75 | * @param <V> new match type |
| 76 | * @return new instance |
| 77 | */ |
| 78 | public <V> Match<V> map(Function<T, V> mapper) { |
| 79 | if (matchAny) { |
| 80 | return any(); |
| 81 | } else if (value == null) { |
| 82 | return ifNull(); |
| 83 | } else { |
| 84 | return ifValue(mapper.apply(value)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Checks if this instance matches specified value. |
| 90 | * @param other other value |
| 91 | * @return true if matches; false otherwise |
| 92 | */ |
| 93 | public boolean matches(T other) { |
| 94 | if (matchAny) { |
| 95 | return true; |
| 96 | } else if (other == null) { |
| 97 | return value == null; |
| 98 | } else { |
| 99 | if (value instanceof byte[]) { |
| 100 | return Arrays.equals((byte[]) value, (byte[]) other); |
| 101 | } |
| 102 | return Objects.equals(value, other); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Override |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 107 | public int hashCode() { |
| 108 | return Objects.hash(matchAny, value); |
| 109 | } |
| 110 | |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 111 | @SuppressWarnings("unchecked") |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public boolean equals(Object other) { |
| 114 | if (!(other instanceof Match)) { |
| 115 | return false; |
| 116 | } |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 117 | Match<T> that = (Match<T>) other; |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 118 | return Objects.equals(this.matchAny, that.matchAny) && |
| 119 | Objects.equals(this.value, that.value); |
| 120 | } |
| 121 | |
| 122 | @Override |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 123 | public String toString() { |
| 124 | return toStringHelper(this) |
| 125 | .add("matchAny", matchAny) |
| 126 | .add("value", value) |
| 127 | .toString(); |
| 128 | } |
| 129 | } |