blob: bf544dd6756a1893b1e3e21dbb7f5f502fadb657 [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")
Sithara Punnassery61a80252017-08-07 11:16:08 -070056 public static final DocumentTreeResult WRITE_LOCK =
57 new DocumentTreeResult(Status.WRITE_LOCK, null);
58
59 @SuppressWarnings("unchecked")
Jordan Haltermane853d032017-08-01 15:10:28 -070060 public static final DocumentTreeResult INVALID_PATH =
61 new DocumentTreeResult(Status.INVALID_PATH, null);
62
63 @SuppressWarnings("unchecked")
64 public static final DocumentTreeResult ILLEGAL_MODIFICATION =
65 new DocumentTreeResult(Status.ILLEGAL_MODIFICATION, null);
66
67 /**
68 * Returns a successful result.
69 *
70 * @param result the operation result
71 * @param <V> the result value type
72 * @return successful result
73 */
74 public static <V> DocumentTreeResult<V> ok(V result) {
75 return new DocumentTreeResult<V>(Status.OK, result);
76 }
77
78 /**
Sithara Punnassery61a80252017-08-07 11:16:08 -070079 * Returns a {@code WRITE_LOCK} error result.
80 *
81 * @param <V> the result value type
82 * @return write lock result
83 */
84 @SuppressWarnings("unchecked")
85 public static <V> DocumentTreeResult<V> writeLock() {
86 return WRITE_LOCK;
87 }
88
89 /**
Jordan Haltermane853d032017-08-01 15:10:28 -070090 * Returns an {@code INVALID_PATH} result.
91 *
92 * @param <V> the result value type
93 * @return invalid path result
94 */
95 @SuppressWarnings("unchecked")
96 public static <V> DocumentTreeResult<V> invalidPath() {
97 return INVALID_PATH;
98 }
99
100 /**
101 * Returns an {@code ILLEGAL_MODIFICATION} result.
102 *
103 * @param <V> the result value type
104 * @return illegal modification result
105 */
106 @SuppressWarnings("unchecked")
107 public static <V> DocumentTreeResult<V> illegalModification() {
108 return ILLEGAL_MODIFICATION;
109 }
110
111 private final Status status;
112 private final V result;
113
114 public DocumentTreeResult(Status status, V result) {
115 this.status = status;
116 this.result = result;
117 }
118
119 public Status status() {
120 return status;
121 }
122
123 public V result() {
124 return result;
125 }
126
127 public boolean updated() {
128 return status == Status.OK;
129 }
130
131 public boolean created() {
132 return updated() && result == null;
133 }
134
135 @Override
136 public String toString() {
137 return MoreObjects.toStringHelper(getClass())
138 .add("status", status)
139 .add("value", result)
140 .toString();
141 }
142}