Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -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 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 18 | import java.util.function.Function; |
| 19 | |
| 20 | import org.onosproject.store.service.MapEvent; |
| 21 | import org.onosproject.store.service.Versioned; |
| 22 | |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Result of a update operation. |
| 25 | * <p> |
| 26 | * Both old and new values are accessible along with a flag that indicates if the |
| 27 | * the value was updated. If flag is false, oldValue and newValue both |
| 28 | * point to the same unmodified value. |
| 29 | * @param <V> result type |
| 30 | */ |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 31 | public class UpdateResult<K, V> { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 32 | |
| 33 | private final boolean updated; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 34 | private final String mapName; |
| 35 | private final K key; |
| 36 | private final Versioned<V> oldValue; |
| 37 | private final Versioned<V> newValue; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 38 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 39 | public UpdateResult(boolean updated, String mapName, K key, Versioned<V> oldValue, Versioned<V> newValue) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 40 | this.updated = updated; |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 41 | this.mapName = mapName; |
| 42 | this.key = key; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 43 | this.oldValue = oldValue; |
| 44 | this.newValue = newValue; |
| 45 | } |
| 46 | |
| 47 | public boolean updated() { |
| 48 | return updated; |
| 49 | } |
| 50 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 51 | public String mapName() { |
| 52 | return mapName; |
| 53 | } |
| 54 | |
| 55 | public K key() { |
| 56 | return key; |
| 57 | } |
| 58 | |
| 59 | public Versioned<V> oldValue() { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 60 | return oldValue; |
| 61 | } |
| 62 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 63 | public Versioned<V> newValue() { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 64 | return newValue; |
| 65 | } |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 66 | |
| 67 | public <K1, V1> UpdateResult<K1, V1> map(Function<K, K1> keyTransform, Function<V, V1> valueMapper) { |
| 68 | return new UpdateResult<>(updated, |
| 69 | mapName, |
| 70 | keyTransform.apply(key), |
| 71 | oldValue == null ? null : oldValue.map(valueMapper), |
| 72 | newValue == null ? null : newValue.map(valueMapper)); |
| 73 | } |
| 74 | |
| 75 | public MapEvent<K, V> toMapEvent() { |
| 76 | if (!updated) { |
| 77 | return null; |
| 78 | } else { |
| 79 | MapEvent.Type eventType = oldValue == null ? |
| 80 | MapEvent.Type.INSERT : newValue == null ? MapEvent.Type.REMOVE : MapEvent.Type.UPDATE; |
| 81 | Versioned<V> eventValue = eventType == MapEvent.Type.REMOVE ? oldValue : newValue; |
| 82 | return new MapEvent<>(mapName(), eventType, key(), eventValue); |
| 83 | } |
| 84 | } |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 85 | } |