Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.store.primitives; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static com.google.common.base.Preconditions.checkState; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | import java.util.function.Function; |
| 24 | |
| 25 | import org.onlab.util.ByteArraySizeHashPrinter; |
| 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | import org.onosproject.store.service.DocumentPath; |
| 29 | |
| 30 | /** |
| 31 | * Map update operation. |
| 32 | * |
| 33 | * @param <V> map value type |
| 34 | */ |
| 35 | public final class NodeUpdate<V> { |
| 36 | |
| 37 | /** |
| 38 | * Type of database update operation. |
| 39 | */ |
| 40 | public enum Type { |
| 41 | /** |
| 42 | * Creates an entry if the current version matches specified version. |
| 43 | */ |
| 44 | CREATE_NODE, |
| 45 | /** |
| 46 | * Updates an entry if the current version matches specified version. |
| 47 | */ |
| 48 | UPDATE_NODE, |
| 49 | /** |
| 50 | * Deletes an entry if the current version matches specified version. |
| 51 | */ |
| 52 | DELETE_NODE |
| 53 | } |
| 54 | |
| 55 | private Type type; |
| 56 | private DocumentPath path; |
| 57 | private V value; |
| 58 | private long version = -1; |
| 59 | |
| 60 | /** |
| 61 | * Returns the type of update operation. |
| 62 | * @return type of update. |
| 63 | */ |
| 64 | public Type type() { |
| 65 | return type; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the item path being updated. |
| 70 | * @return item path |
| 71 | */ |
| 72 | public DocumentPath path() { |
| 73 | return path; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the new value. |
| 78 | * @return item's target value. |
| 79 | */ |
| 80 | public V value() { |
| 81 | return value; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the expected current version in the database for the key. |
| 86 | * @return expected version. |
| 87 | */ |
| 88 | public long version() { |
| 89 | return version; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Transforms this instance into an instance of different paramterized types. |
| 94 | * |
| 95 | * @param valueMapper transcoder to value type |
| 96 | * @return new instance |
| 97 | * @param <T> value type of returned instance |
| 98 | */ |
| 99 | public <T> NodeUpdate<T> map(Function<V, T> valueMapper) { |
| 100 | return NodeUpdate.<T>newBuilder() |
| 101 | .withType(type) |
| 102 | //.withKey(keyMapper.apply(key)) |
| 103 | .withValue(value == null ? null : valueMapper.apply(value)) |
| 104 | .withVersion(version) |
| 105 | .build(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int hashCode() { |
| 110 | return Objects.hash(type, path, value, version); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean equals(Object object) { |
| 115 | if (object instanceof NodeUpdate) { |
| 116 | NodeUpdate that = (NodeUpdate) object; |
| 117 | return this.type == that.type |
| 118 | && Objects.equals(this.path, that.path) |
| 119 | && Objects.equals(this.value, that.value) |
| 120 | && Objects.equals(this.version, that.version); |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public String toString() { |
| 127 | return MoreObjects.toStringHelper(this) |
| 128 | .add("type", type) |
| 129 | .add("path", path) |
| 130 | .add("value", value instanceof byte[] ? |
| 131 | new ByteArraySizeHashPrinter((byte[]) value) : value) |
| 132 | .add("version", version) |
| 133 | .toString(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates a new builder instance. |
| 138 | * |
| 139 | * @param <V> value type |
| 140 | * @return builder. |
| 141 | */ |
| 142 | public static <V> Builder<V> newBuilder() { |
| 143 | return new Builder<>(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * NodeUpdate builder. |
| 148 | * |
| 149 | * @param <V> value type |
| 150 | */ |
| 151 | public static final class Builder<V> { |
| 152 | |
| 153 | private NodeUpdate<V> update = new NodeUpdate<>(); |
| 154 | |
| 155 | public NodeUpdate<V> build() { |
| 156 | validateInputs(); |
| 157 | return update; |
| 158 | } |
| 159 | |
| 160 | public Builder<V> withType(Type type) { |
| 161 | update.type = checkNotNull(type, "type cannot be null"); |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | public Builder<V> withPath(DocumentPath key) { |
| 166 | update.path = checkNotNull(key, "key cannot be null"); |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | public Builder<V> withValue(V value) { |
| 171 | update.value = value; |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | public Builder<V> withVersion(long version) { |
| 176 | update.version = version; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | private void validateInputs() { |
| 181 | checkNotNull(update.type, "type must be specified"); |
| 182 | switch (update.type) { |
| 183 | case CREATE_NODE: |
| 184 | checkNotNull(update.path, "key must be specified"); |
| 185 | checkNotNull(update.value, "value must be specified."); |
| 186 | break; |
| 187 | case UPDATE_NODE: |
| 188 | checkNotNull(update.path, "key must be specified"); |
| 189 | checkNotNull(update.value, "value must be specified."); |
| 190 | checkState(update.version >= 0, "version must be specified"); |
| 191 | break; |
| 192 | case DELETE_NODE: |
| 193 | checkNotNull(update.path, "key must be specified"); |
| 194 | checkState(update.version >= 0, "version must be specified"); |
| 195 | break; |
| 196 | default: |
| 197 | throw new IllegalStateException("Unknown operation type"); |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | } |
| 202 | } |