blob: 6122b7980ca4477e340fa37f84eea022589d0a02 [file] [log] [blame]
Madan Jampani346d4f52015-05-04 11:09:39 -07001/*
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 Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Madan Jampani346d4f52015-05-04 11:09:39 -070017
Madan Jampani7804c992015-07-20 13:20:19 -070018import java.util.function.Function;
19
20import org.onosproject.store.service.MapEvent;
21import org.onosproject.store.service.Versioned;
22
Madan Jampani346d4f52015-05-04 11:09:39 -070023/**
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 Jampani7804c992015-07-20 13:20:19 -070031public class UpdateResult<K, V> {
Madan Jampani346d4f52015-05-04 11:09:39 -070032
33 private final boolean updated;
Madan Jampani7804c992015-07-20 13:20:19 -070034 private final String mapName;
35 private final K key;
36 private final Versioned<V> oldValue;
37 private final Versioned<V> newValue;
Madan Jampani346d4f52015-05-04 11:09:39 -070038
Madan Jampani7804c992015-07-20 13:20:19 -070039 public UpdateResult(boolean updated, String mapName, K key, Versioned<V> oldValue, Versioned<V> newValue) {
Madan Jampani346d4f52015-05-04 11:09:39 -070040 this.updated = updated;
Madan Jampani7804c992015-07-20 13:20:19 -070041 this.mapName = mapName;
42 this.key = key;
Madan Jampani346d4f52015-05-04 11:09:39 -070043 this.oldValue = oldValue;
44 this.newValue = newValue;
45 }
46
47 public boolean updated() {
48 return updated;
49 }
50
Madan Jampani7804c992015-07-20 13:20:19 -070051 public String mapName() {
52 return mapName;
53 }
54
55 public K key() {
56 return key;
57 }
58
59 public Versioned<V> oldValue() {
Madan Jampani346d4f52015-05-04 11:09:39 -070060 return oldValue;
61 }
62
Madan Jampani7804c992015-07-20 13:20:19 -070063 public Versioned<V> newValue() {
Madan Jampani346d4f52015-05-04 11:09:39 -070064 return newValue;
65 }
Madan Jampani7804c992015-07-20 13:20:19 -070066
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 {
Madan Jampanif95290a2016-01-27 21:06:11 -080079 return new MapEvent<>(mapName(), key(), newValue, oldValue);
Madan Jampani7804c992015-07-20 13:20:19 -070080 }
81 }
Madan Jampani346d4f52015-05-04 11:09:39 -070082}