blob: dc80c56794cac72802a73e264e1c5fbff090dcf7 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
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
17package org.onosproject.store.primitives.resources.impl;
18
19import static com.google.common.base.Preconditions.checkArgument;
20import static com.google.common.base.Preconditions.checkNotNull;
21import static com.google.common.base.Preconditions.checkState;
22
23import com.google.common.base.MoreObjects;
24
25/**
26 * Map update operation.
27 *
28 * @param <K> map key type
29 * @param <V> map value type
30 *
31 */
32public final class MapUpdate<K, V> {
33
34 /**
35 * Type of database update operation.
36 */
37 public enum Type {
38 /**
39 * Insert/Update entry without any checks.
40 */
41 PUT,
42 /**
43 * Insert an entry iff there is no existing entry for that key.
44 */
45 PUT_IF_ABSENT,
46
47 /**
48 * Update entry if the current version matches specified version.
49 */
50 PUT_IF_VERSION_MATCH,
51
52 /**
53 * Update entry if the current value matches specified value.
54 */
55 PUT_IF_VALUE_MATCH,
56
57 /**
58 * Remove entry without any checks.
59 */
60 REMOVE,
61
62 /**
63 * Remove entry if the current version matches specified version.
64 */
65 REMOVE_IF_VERSION_MATCH,
66
67 /**
68 * Remove entry if the current value matches specified value.
69 */
70 REMOVE_IF_VALUE_MATCH,
71 }
72
Madan Jampanicadd70b2016-02-08 13:45:43 -080073 private String mapName;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080074 private Type type;
75 private K key;
76 private V value;
77 private V currentValue;
78 private long currentVersion = -1;
79
80 /**
Madan Jampanicadd70b2016-02-08 13:45:43 -080081 * Returns the name of the map.
82 *
83 * @return map name
84 */
85 public String mapName() {
86 return mapName;
87 }
88
89 /**
Madan Jampani5e5b3d62016-02-01 16:03:33 -080090 * Returns the type of update operation.
91 * @return type of update.
92 */
93 public Type type() {
94 return type;
95 }
96
97 /**
98 * Returns the item key being updated.
99 * @return item key
100 */
101 public K key() {
102 return key;
103 }
104
105 /**
106 * Returns the new value.
107 * @return item's target value.
108 */
109 public V value() {
110 return value;
111 }
112
113 /**
114 * Returns the expected current value for the key.
115 * @return current value in database.
116 */
117 public V currentValue() {
118 return currentValue;
119 }
120
121 /**
122 * Returns the expected current version in the database for the key.
123 * @return expected version.
124 */
125 public long currentVersion() {
126 return currentVersion;
127 }
128
129 @Override
130 public String toString() {
131 return MoreObjects.toStringHelper(this)
Madan Jampanicadd70b2016-02-08 13:45:43 -0800132 .add("mapName", mapName)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800133 .add("type", type)
134 .add("key", key)
135 .add("value", value)
136 .add("currentValue", currentValue)
137 .add("currentVersion", currentVersion)
138 .toString();
139 }
140
141 /**
142 * Creates a new builder instance.
143 *
144 * @param <K> key type
145 * @param <V> value type
146 * @return builder.
147 */
148 public static <K, V> Builder<K, V> newBuilder() {
149 return new Builder<>();
150 }
151
152 /**
153 * MapUpdate builder.
154 *
155 * @param <K> key type
156 * @param <V> value type
157 */
158 public static final class Builder<K, V> {
159
160 private MapUpdate<K, V> update = new MapUpdate<>();
161
162 public MapUpdate<K, V> build() {
163 validateInputs();
164 return update;
165 }
166
Madan Jampanicadd70b2016-02-08 13:45:43 -0800167 public Builder<K, V> withMapName(String name) {
168 update.mapName = checkNotNull(name, "name cannot be null");
169 return this;
170 }
171
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800172 public Builder<K, V> withType(Type type) {
173 update.type = checkNotNull(type, "type cannot be null");
174 return this;
175 }
176
177 public Builder<K, V> withKey(K key) {
178 update.key = checkNotNull(key, "key cannot be null");
179 return this;
180 }
181
182 public Builder<K, V> withCurrentValue(V value) {
183 update.currentValue = checkNotNull(value, "currentValue cannot be null");
184 return this;
185 }
186
187 public Builder<K, V> withValue(V value) {
188 update.value = checkNotNull(value, "value cannot be null");
189 return this;
190 }
191
192 public Builder<K, V> withCurrentVersion(long version) {
193 checkArgument(version >= 0, "version cannot be negative");
194 update.currentVersion = version;
195 return this;
196 }
197
198 private void validateInputs() {
199 checkNotNull(update.type, "type must be specified");
200 checkNotNull(update.key, "key must be specified");
201 switch (update.type) {
202 case PUT:
203 case PUT_IF_ABSENT:
204 checkNotNull(update.value, "value must be specified.");
205 break;
206 case PUT_IF_VERSION_MATCH:
207 checkNotNull(update.value, "value must be specified.");
208 checkState(update.currentVersion >= 0, "current version must be specified");
209 break;
210 case PUT_IF_VALUE_MATCH:
211 checkNotNull(update.value, "value must be specified.");
212 checkNotNull(update.currentValue, "currentValue must be specified.");
213 break;
214 case REMOVE:
215 break;
216 case REMOVE_IF_VERSION_MATCH:
217 checkState(update.currentVersion >= 0, "current version must be specified");
218 break;
219 case REMOVE_IF_VALUE_MATCH:
220 checkNotNull(update.currentValue, "currentValue must be specified.");
221 break;
222 default:
223 throw new IllegalStateException("Unknown operation type");
224 }
225 }
226 }
227}