Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
| 19 | import org.onosproject.store.service.DocumentPath; |
| 20 | import org.onosproject.store.service.Versioned; |
| 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | |
| 24 | /** |
| 25 | * Result of a document tree node update operation. |
| 26 | * <p> |
| 27 | * Both old and new values are accessible along with a status of update. |
| 28 | * |
| 29 | * @param <V> value type |
| 30 | */ |
| 31 | public class DocumentTreeUpdateResult<V> { |
| 32 | |
| 33 | public enum Status { |
| 34 | /** |
| 35 | * Indicates a successful update. |
| 36 | */ |
| 37 | OK, |
| 38 | |
| 39 | /** |
| 40 | * Indicates a noop i.e. existing and new value are both same. |
| 41 | */ |
| 42 | NOOP, |
| 43 | |
| 44 | /** |
| 45 | * Indicates a failed update due to a write lock. |
| 46 | */ |
| 47 | WRITE_LOCK, |
| 48 | |
| 49 | /** |
| 50 | * Indicates a failed update due to a invalid path. |
| 51 | */ |
| 52 | INVALID_PATH, |
| 53 | |
| 54 | /** |
| 55 | * Indicates a failed update due to a illegal modification attempt. |
| 56 | */ |
| 57 | ILLEGAL_MODIFICATION, |
| 58 | } |
| 59 | |
| 60 | private final DocumentPath path; |
| 61 | private final Status status; |
| 62 | private final Versioned<V> oldValue; |
| 63 | private final Versioned<V> newValue; |
| 64 | |
| 65 | public DocumentTreeUpdateResult(DocumentPath path, |
| 66 | Status status, |
| 67 | Versioned<V> newValue, |
| 68 | Versioned<V> oldValue) { |
| 69 | this.status = status; |
| 70 | this.path = path; |
| 71 | this.newValue = newValue; |
| 72 | this.oldValue = oldValue; |
| 73 | } |
| 74 | |
| 75 | public static <V> DocumentTreeUpdateResult<V> invalidPath(DocumentPath path) { |
| 76 | return new DocumentTreeUpdateResult<>(path, Status.INVALID_PATH, null, null); |
| 77 | } |
| 78 | |
| 79 | public static <V> DocumentTreeUpdateResult<V> illegalModification(DocumentPath path) { |
| 80 | return new DocumentTreeUpdateResult<>(path, Status.ILLEGAL_MODIFICATION, null, null); |
| 81 | } |
| 82 | |
| 83 | public Status status() { |
| 84 | return status; |
| 85 | } |
| 86 | |
| 87 | public DocumentPath path() { |
| 88 | return path; |
| 89 | } |
| 90 | |
| 91 | public Versioned<V> oldValue() { |
| 92 | return oldValue; |
| 93 | } |
| 94 | |
| 95 | public Versioned<V> newValue() { |
| 96 | return this.newValue; |
| 97 | } |
| 98 | |
| 99 | public boolean updated() { |
| 100 | return status == Status.OK; |
| 101 | } |
| 102 | |
| 103 | public boolean created() { |
| 104 | return updated() && oldValue == null; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return MoreObjects.toStringHelper(getClass()) |
| 110 | .add("path", path) |
| 111 | .add("status", status) |
| 112 | .add("newValue", newValue) |
| 113 | .add("oldValue", oldValue) |
| 114 | .toString(); |
| 115 | } |
| 116 | } |