blob: acd724cb893176dd2d64bc773bb71bf58f1f5fab [file] [log] [blame]
Madan Jampanibff6d8f2015-03-31 16:53:47 -07001/*
2 * Copyright 2015 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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070017
Madan Jampani92c64eb2015-07-23 15:37:07 -070018import static com.google.common.base.MoreObjects.toStringHelper;
19
20import java.util.Objects;
21
Madan Jampanibff6d8f2015-03-31 16:53:47 -070022/**
23 * Result of a database update operation.
24 *
25 * @param <V> return value type
26 */
27public final class Result<V> {
28
29 public enum Status {
30 /**
31 * Indicates a successful update.
32 */
33 OK,
34
35 /**
36 * Indicates a failure due to underlying state being locked by another transaction.
37 */
38 LOCKED
39 }
40
41 private final Status status;
42 private final V value;
43
44 /**
45 * Creates a new Result instance with the specified value with status set to Status.OK.
46 *
47 * @param <V> result value type
48 * @param value result value
49 * @return Result instance
50 */
51 public static <V> Result<V> ok(V value) {
52 return new Result<>(value, Status.OK);
53 }
54
55 /**
56 * Creates a new Result instance with status set to Status.LOCKED.
57 *
58 * @param <V> result value type
59 * @return Result instance
60 */
61 public static <V> Result<V> locked() {
62 return new Result<>(null, Status.LOCKED);
63 }
64
65 private Result(V value, Status status) {
66 this.value = value;
67 this.status = status;
68 }
69
70 /**
71 * Returns true if this result indicates a successful execution i.e status is Status.OK.
72 *
73 * @return true if successful, false otherwise
74 */
75 public boolean success() {
76 return status == Status.OK;
77 }
78
79 /**
80 * Returns the status of database update operation.
Madan Jampani92c64eb2015-07-23 15:37:07 -070081 *
Madan Jampanibff6d8f2015-03-31 16:53:47 -070082 * @return database update status
83 */
84 public Status status() {
85 return status;
86 }
87
88 /**
89 * Returns the return value for the update.
Madan Jampani92c64eb2015-07-23 15:37:07 -070090 *
Madan Jampanibff6d8f2015-03-31 16:53:47 -070091 * @return value returned by database update. If the status is another
92 * other than Status.OK, this returns a null
93 */
94 public V value() {
95 return value;
96 }
Madan Jampani92c64eb2015-07-23 15:37:07 -070097
98 @Override
99 public int hashCode() {
100 return Objects.hash(value, status);
101 }
102
103 @SuppressWarnings("unchecked")
104 @Override
105 public boolean equals(Object other) {
106 if (!(other instanceof Result)) {
107 return false;
108 }
109 Result<V> that = (Result<V>) other;
110 return Objects.equals(this.value, that.value) &&
111 Objects.equals(this.status, that.status);
112 }
113
114 @Override
115 public String toString() {
116 return toStringHelper(this)
117 .add("status", status)
118 .add("value", value)
119 .toString();
120 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700121}