blob: c105a08d91060b14869ae23bf0dd461907eabf3a [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
73 private Type type;
74 private K key;
75 private V value;
76 private V currentValue;
77 private long currentVersion = -1;
78
79 /**
80 * Returns the type of update operation.
81 * @return type of update.
82 */
83 public Type type() {
84 return type;
85 }
86
87 /**
88 * Returns the item key being updated.
89 * @return item key
90 */
91 public K key() {
92 return key;
93 }
94
95 /**
96 * Returns the new value.
97 * @return item's target value.
98 */
99 public V value() {
100 return value;
101 }
102
103 /**
104 * Returns the expected current value for the key.
105 * @return current value in database.
106 */
107 public V currentValue() {
108 return currentValue;
109 }
110
111 /**
112 * Returns the expected current version in the database for the key.
113 * @return expected version.
114 */
115 public long currentVersion() {
116 return currentVersion;
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(this)
122 .add("type", type)
123 .add("key", key)
124 .add("value", value)
125 .add("currentValue", currentValue)
126 .add("currentVersion", currentVersion)
127 .toString();
128 }
129
130 /**
131 * Creates a new builder instance.
132 *
133 * @param <K> key type
134 * @param <V> value type
135 * @return builder.
136 */
137 public static <K, V> Builder<K, V> newBuilder() {
138 return new Builder<>();
139 }
140
141 /**
142 * MapUpdate builder.
143 *
144 * @param <K> key type
145 * @param <V> value type
146 */
147 public static final class Builder<K, V> {
148
149 private MapUpdate<K, V> update = new MapUpdate<>();
150
151 public MapUpdate<K, V> build() {
152 validateInputs();
153 return update;
154 }
155
156 public Builder<K, V> withType(Type type) {
157 update.type = checkNotNull(type, "type cannot be null");
158 return this;
159 }
160
161 public Builder<K, V> withKey(K key) {
162 update.key = checkNotNull(key, "key cannot be null");
163 return this;
164 }
165
166 public Builder<K, V> withCurrentValue(V value) {
167 update.currentValue = checkNotNull(value, "currentValue cannot be null");
168 return this;
169 }
170
171 public Builder<K, V> withValue(V value) {
172 update.value = checkNotNull(value, "value cannot be null");
173 return this;
174 }
175
176 public Builder<K, V> withCurrentVersion(long version) {
177 checkArgument(version >= 0, "version cannot be negative");
178 update.currentVersion = version;
179 return this;
180 }
181
182 private void validateInputs() {
183 checkNotNull(update.type, "type must be specified");
184 checkNotNull(update.key, "key must be specified");
185 switch (update.type) {
186 case PUT:
187 case PUT_IF_ABSENT:
188 checkNotNull(update.value, "value must be specified.");
189 break;
190 case PUT_IF_VERSION_MATCH:
191 checkNotNull(update.value, "value must be specified.");
192 checkState(update.currentVersion >= 0, "current version must be specified");
193 break;
194 case PUT_IF_VALUE_MATCH:
195 checkNotNull(update.value, "value must be specified.");
196 checkNotNull(update.currentValue, "currentValue must be specified.");
197 break;
198 case REMOVE:
199 break;
200 case REMOVE_IF_VERSION_MATCH:
201 checkState(update.currentVersion >= 0, "current version must be specified");
202 break;
203 case REMOVE_IF_VALUE_MATCH:
204 checkNotNull(update.currentValue, "currentValue must be specified.");
205 break;
206 default:
207 throw new IllegalStateException("Unknown operation type");
208 }
209 }
210 }
211}