Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 3 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
| 19 | import java.util.function.Function; |
| 20 | |
| 21 | import org.onosproject.store.service.MapEvent; |
| 22 | import org.onosproject.store.service.Versioned; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | |
| 26 | /** |
| 27 | * Result of a map entry update operation. |
| 28 | * <p> |
| 29 | * Both old and new values are accessible along with a flag that indicates if the |
| 30 | * the value was updated. If flag is false, oldValue and newValue both |
| 31 | * point to the same unmodified value. |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 32 | * |
| 33 | * @param <K> key type |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 34 | * @param <V> result type |
| 35 | */ |
| 36 | public class MapEntryUpdateResult<K, V> { |
| 37 | |
| 38 | public enum Status { |
| 39 | |
| 40 | /** |
| 41 | * Indicates a successful update. |
| 42 | */ |
| 43 | OK, |
| 44 | |
| 45 | /** |
| 46 | * Indicates a noop i.e. existing and new value are both null. |
| 47 | */ |
| 48 | NOOP, |
| 49 | |
| 50 | /** |
| 51 | * Indicates a failed update due to a write lock. |
| 52 | */ |
| 53 | WRITE_LOCK, |
| 54 | |
| 55 | /** |
| 56 | * Indicates a failed update due to a precondition check failure. |
| 57 | */ |
| 58 | PRECONDITION_FAILED |
| 59 | } |
| 60 | |
| 61 | private final String mapName; |
| 62 | private Status status; |
| 63 | private final K key; |
| 64 | private final Versioned<V> oldValue; |
| 65 | private final Versioned<V> newValue; |
| 66 | |
| 67 | public MapEntryUpdateResult(Status status, String mapName, K key, Versioned<V> oldValue, Versioned<V> newValue) { |
| 68 | this.status = status; |
| 69 | this.mapName = mapName; |
| 70 | this.key = key; |
| 71 | this.oldValue = oldValue; |
| 72 | this.newValue = newValue; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns {@code true} if the update was successful. |
| 77 | * @return {@code true} if yes, {@code false} otherwise |
| 78 | */ |
| 79 | public boolean updated() { |
| 80 | return status == Status.OK; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the map name. |
| 85 | * @return map name |
| 86 | */ |
| 87 | public String mapName() { |
| 88 | return mapName; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the update status. |
| 93 | * @return update status |
| 94 | */ |
| 95 | public Status status() { |
| 96 | return status; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the map key. |
| 101 | * @return key |
| 102 | */ |
| 103 | public K key() { |
| 104 | return key; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the old value. |
| 109 | * @return the previous value associated with key if updated was successful, otherwise current value |
| 110 | */ |
| 111 | public Versioned<V> oldValue() { |
| 112 | return oldValue; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns the new value after update. |
| 117 | * @return if updated was unsuccessful, this is same as old value |
| 118 | */ |
| 119 | public Versioned<V> newValue() { |
| 120 | return newValue; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Maps to another instance with different key and value types. |
| 125 | * @param keyTransform transformer to use for transcoding keys |
| 126 | * @param valueMapper mapper to use for transcoding values |
| 127 | * @return new instance |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 128 | * @param <K1> key type of returned {@code MapEntryUpdateResult} |
| 129 | * @param <V1> value type of returned {@code MapEntryUpdateResult} |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 130 | */ |
| 131 | public <K1, V1> MapEntryUpdateResult<K1, V1> map(Function<K, K1> keyTransform, Function<V, V1> valueMapper) { |
| 132 | return new MapEntryUpdateResult<>(status, |
| 133 | mapName, |
| 134 | keyTransform.apply(key), |
| 135 | oldValue == null ? null : oldValue.map(valueMapper), |
| 136 | newValue == null ? null : newValue.map(valueMapper)); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Return the map event that will be generated as a result of this update. |
| 141 | * @return map event. if update was unsuccessful, this returns {@code null} |
| 142 | */ |
| 143 | public MapEvent<K, V> toMapEvent() { |
| 144 | if (!updated()) { |
| 145 | return null; |
| 146 | } else { |
| 147 | return new MapEvent<>(mapName(), key(), newValue, oldValue); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(MapEntryUpdateResult.class) |
| 154 | .add("mapName", mapName) |
| 155 | .add("status", status) |
| 156 | .add("key", key) |
| 157 | .add("oldValue", oldValue) |
| 158 | .add("newValue", newValue) |
| 159 | .toString(); |
| 160 | } |
| 161 | } |