blob: 63235d69fb73282111b6852e1f157c25b0b9ac35 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani5e5b3d62016-02-01 16:03:33 -08003 *
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import java.util.function.Function;
19
20import org.onosproject.store.service.MapEvent;
21import org.onosproject.store.service.Versioned;
22
23import com.google.common.base.MoreObjects;
24
25/**
26 * Result of a map entry update operation.
27 * <p>
28 * Both old and new values are accessible along with a flag that indicates if the
29 * the value was updated. If flag is false, oldValue and newValue both
30 * point to the same unmodified value.
Madan Jampanicadd70b2016-02-08 13:45:43 -080031 *
32 * @param <K> key type
Madan Jampani5e5b3d62016-02-01 16:03:33 -080033 * @param <V> result type
34 */
35public class MapEntryUpdateResult<K, V> {
36
37 public enum Status {
38
39 /**
40 * Indicates a successful update.
41 */
42 OK,
43
44 /**
45 * Indicates a noop i.e. existing and new value are both null.
46 */
47 NOOP,
48
49 /**
50 * Indicates a failed update due to a write lock.
51 */
52 WRITE_LOCK,
53
54 /**
55 * Indicates a failed update due to a precondition check failure.
56 */
57 PRECONDITION_FAILED
58 }
59
60 private final String mapName;
61 private Status status;
62 private final K key;
63 private final Versioned<V> oldValue;
64 private final Versioned<V> newValue;
65
66 public MapEntryUpdateResult(Status status, String mapName, K key, Versioned<V> oldValue, Versioned<V> newValue) {
67 this.status = status;
68 this.mapName = mapName;
69 this.key = key;
70 this.oldValue = oldValue;
71 this.newValue = newValue;
72 }
73
74 /**
75 * Returns {@code true} if the update was successful.
76 * @return {@code true} if yes, {@code false} otherwise
77 */
78 public boolean updated() {
79 return status == Status.OK;
80 }
81
82 /**
83 * Returns the map name.
84 * @return map name
85 */
86 public String mapName() {
87 return mapName;
88 }
89
90 /**
91 * Returns the update status.
92 * @return update status
93 */
94 public Status status() {
95 return status;
96 }
97
98 /**
99 * Returns the map key.
100 * @return key
101 */
102 public K key() {
103 return key;
104 }
105
106 /**
107 * Returns the old value.
108 * @return the previous value associated with key if updated was successful, otherwise current value
109 */
110 public Versioned<V> oldValue() {
111 return oldValue;
112 }
113
114 /**
115 * Returns the new value after update.
116 * @return if updated was unsuccessful, this is same as old value
117 */
118 public Versioned<V> newValue() {
119 return newValue;
120 }
121
122 /**
123 * Maps to another instance with different key and value types.
124 * @param keyTransform transformer to use for transcoding keys
125 * @param valueMapper mapper to use for transcoding values
126 * @return new instance
Madan Jampanicadd70b2016-02-08 13:45:43 -0800127 * @param <K1> key type of returned {@code MapEntryUpdateResult}
128 * @param <V1> value type of returned {@code MapEntryUpdateResult}
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800129 */
130 public <K1, V1> MapEntryUpdateResult<K1, V1> map(Function<K, K1> keyTransform, Function<V, V1> valueMapper) {
131 return new MapEntryUpdateResult<>(status,
132 mapName,
133 keyTransform.apply(key),
134 oldValue == null ? null : oldValue.map(valueMapper),
135 newValue == null ? null : newValue.map(valueMapper));
136 }
137
138 /**
139 * Return the map event that will be generated as a result of this update.
140 * @return map event. if update was unsuccessful, this returns {@code null}
141 */
142 public MapEvent<K, V> toMapEvent() {
143 if (!updated()) {
144 return null;
145 } else {
146 return new MapEvent<>(mapName(), key(), newValue, oldValue);
147 }
148 }
149
150 @Override
151 public String toString() {
152 return MoreObjects.toStringHelper(MapEntryUpdateResult.class)
153 .add("mapName", mapName)
154 .add("status", status)
155 .add("key", key)
156 .add("oldValue", oldValue)
157 .add("newValue", newValue)
158 .toString();
159 }
160}