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