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