blob: 3e30a0077ab0c2cfa5499e1fb782ce012b0a2e00 [file] [log] [blame]
Jordan Haltermane853d032017-08-01 15:10:28 -07001/*
2 * Copyright 2016-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
17package org.onosproject.store.primitives.resources.impl;
18
19import com.google.common.base.MoreObjects;
20
21/**
22 * Result of a document tree operation.
23 *
24 * @param <V> value type
25 */
26public class DocumentTreeResult<V> {
27
28 public enum Status {
29 /**
30 * Indicates a successful update.
31 */
32 OK,
33
34 /**
35 * Indicates a noop i.e. existing and new value are both same.
36 */
37 NOOP,
38
39 /**
40 * Indicates a failed update due to a write lock.
41 */
42 WRITE_LOCK,
43
44 /**
45 * Indicates a failed update due to a invalid path.
46 */
47 INVALID_PATH,
48
49 /**
50 * Indicates a failed update due to a illegal modification attempt.
51 */
52 ILLEGAL_MODIFICATION,
53 }
54
55 @SuppressWarnings("unchecked")
56 public static final DocumentTreeResult INVALID_PATH =
57 new DocumentTreeResult(Status.INVALID_PATH, null);
58
59 @SuppressWarnings("unchecked")
60 public static final DocumentTreeResult ILLEGAL_MODIFICATION =
61 new DocumentTreeResult(Status.ILLEGAL_MODIFICATION, null);
62
63 /**
64 * Returns a successful result.
65 *
66 * @param result the operation result
67 * @param <V> the result value type
68 * @return successful result
69 */
70 public static <V> DocumentTreeResult<V> ok(V result) {
71 return new DocumentTreeResult<V>(Status.OK, result);
72 }
73
74 /**
75 * Returns an {@code INVALID_PATH} result.
76 *
77 * @param <V> the result value type
78 * @return invalid path result
79 */
80 @SuppressWarnings("unchecked")
81 public static <V> DocumentTreeResult<V> invalidPath() {
82 return INVALID_PATH;
83 }
84
85 /**
86 * Returns an {@code ILLEGAL_MODIFICATION} result.
87 *
88 * @param <V> the result value type
89 * @return illegal modification result
90 */
91 @SuppressWarnings("unchecked")
92 public static <V> DocumentTreeResult<V> illegalModification() {
93 return ILLEGAL_MODIFICATION;
94 }
95
96 private final Status status;
97 private final V result;
98
99 public DocumentTreeResult(Status status, V result) {
100 this.status = status;
101 this.result = result;
102 }
103
104 public Status status() {
105 return status;
106 }
107
108 public V result() {
109 return result;
110 }
111
112 public boolean updated() {
113 return status == Status.OK;
114 }
115
116 public boolean created() {
117 return updated() && result == null;
118 }
119
120 @Override
121 public String toString() {
122 return MoreObjects.toStringHelper(getClass())
123 .add("status", status)
124 .add("value", result)
125 .toString();
126 }
127}