Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [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 | */ |
| 16 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 17 | package org.onosproject.store.service; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 18 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.*; |
| 20 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 21 | import com.google.common.base.MoreObjects; |
| 22 | |
| 23 | /** |
| 24 | * Database update operation. |
| 25 | * |
| 26 | * @param <K> key type. |
| 27 | * @param <V> value type. |
| 28 | */ |
| 29 | public class UpdateOperation<K, V> { |
| 30 | |
| 31 | /** |
| 32 | * Type of database update operation. |
| 33 | */ |
| 34 | public static enum Type { |
| 35 | PUT, |
| 36 | PUT_IF_ABSENT, |
| 37 | PUT_IF_VERSION_MATCH, |
| 38 | PUT_IF_VALUE_MATCH, |
| 39 | REMOVE, |
| 40 | REMOVE_IF_VERSION_MATCH, |
| 41 | REMOVE_IF_VALUE_MATCH, |
| 42 | } |
| 43 | |
| 44 | private Type type; |
| 45 | private String tableName; |
| 46 | private K key; |
| 47 | private V value; |
| 48 | private V currentValue; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 49 | private long currentVersion = -1; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Returns the type of update operation. |
| 53 | * @return type of update. |
| 54 | */ |
| 55 | public Type type() { |
| 56 | return type; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the tableName being updated. |
| 61 | * @return table name. |
| 62 | */ |
| 63 | public String tableName() { |
| 64 | return tableName; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the item key being updated. |
| 69 | * @return item key |
| 70 | */ |
| 71 | public K key() { |
| 72 | return key; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the new value. |
| 77 | * @return item's target value. |
| 78 | */ |
| 79 | public V value() { |
| 80 | return value; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the expected current value in the database value for the key. |
| 85 | * @return current value in database. |
| 86 | */ |
| 87 | public V currentValue() { |
| 88 | return currentValue; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the expected current version in the database for the key. |
| 93 | * @return expected version. |
| 94 | */ |
| 95 | public long currentVersion() { |
| 96 | return currentVersion; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return MoreObjects.toStringHelper(this) |
| 102 | .add("type", type) |
| 103 | .add("tableName", tableName) |
| 104 | .add("key", key) |
| 105 | .add("value", value) |
| 106 | .add("currentValue", currentValue) |
| 107 | .add("currentVersion", currentVersion) |
| 108 | .toString(); |
| 109 | } |
| 110 | |
| 111 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 112 | * Creates a new builder instance. |
| 113 | * @param <K> key type. |
| 114 | * @param <V> value type. |
| 115 | * |
| 116 | * @return builder. |
| 117 | */ |
| 118 | public static <K, V> Builder<K, V> newBuilder() { |
| 119 | return new Builder<>(); |
| 120 | } |
| 121 | |
| 122 | /** |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 123 | * UpdatOperation builder. |
| 124 | * |
| 125 | * @param <K> key type. |
| 126 | * @param <V> value type. |
| 127 | */ |
| 128 | public static final class Builder<K, V> { |
| 129 | |
| 130 | private UpdateOperation<K, V> operation = new UpdateOperation<>(); |
| 131 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 132 | public UpdateOperation<K, V> build() { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 133 | validateInputs(); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 134 | return operation; |
| 135 | } |
| 136 | |
| 137 | public Builder<K, V> withType(Type type) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 138 | operation.type = checkNotNull(type, "type cannot be null"); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 139 | return this; |
| 140 | } |
| 141 | |
| 142 | public Builder<K, V> withTableName(String tableName) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 143 | operation.tableName = checkNotNull(tableName, "tableName cannot be null"); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 144 | return this; |
| 145 | } |
| 146 | |
| 147 | public Builder<K, V> withKey(K key) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 148 | operation.key = checkNotNull(key, "key cannot be null"); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 149 | return this; |
| 150 | } |
| 151 | |
| 152 | public Builder<K, V> withCurrentValue(V value) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 153 | operation.currentValue = checkNotNull(value, "currentValue cannot be null"); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 154 | return this; |
| 155 | } |
| 156 | |
| 157 | public Builder<K, V> withValue(V value) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 158 | operation.value = checkNotNull(value, "value cannot be null"); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 159 | return this; |
| 160 | } |
| 161 | |
| 162 | public Builder<K, V> withCurrentVersion(long version) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 163 | checkArgument(version >= 0, "version cannot be negative"); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 164 | operation.currentVersion = version; |
| 165 | return this; |
| 166 | } |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 167 | |
| 168 | private void validateInputs() { |
| 169 | checkNotNull(operation.type, "type must be specified"); |
| 170 | checkNotNull(operation.tableName, "table name must be specified"); |
| 171 | checkNotNull(operation.key, "key must be specified"); |
| 172 | switch (operation.type) { |
| 173 | case PUT: |
| 174 | case PUT_IF_ABSENT: |
| 175 | checkNotNull(operation.value, "value must be specified."); |
| 176 | break; |
| 177 | case PUT_IF_VERSION_MATCH: |
| 178 | checkNotNull(operation.value, "value must be specified."); |
| 179 | checkState(operation.currentVersion >= 0, "current version must be specified"); |
| 180 | break; |
| 181 | case PUT_IF_VALUE_MATCH: |
| 182 | checkNotNull(operation.value, "value must be specified."); |
| 183 | checkNotNull(operation.currentValue, "currentValue must be specified."); |
| 184 | break; |
| 185 | case REMOVE: |
| 186 | break; |
| 187 | case REMOVE_IF_VERSION_MATCH: |
| 188 | checkState(operation.currentVersion >= 0, "current version must be specified"); |
| 189 | break; |
| 190 | case REMOVE_IF_VALUE_MATCH: |
| 191 | checkNotNull(operation.currentValue, "currentValue must be specified."); |
| 192 | break; |
| 193 | default: |
| 194 | throw new IllegalStateException("Unknown operation type"); |
| 195 | } |
| 196 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 197 | } |
| 198 | } |