blob: 548174a0e512ae694a8a6cd00bc456880aa92d7d [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 */
16package org.onosproject.store.consistent.impl;
17
18/**
19 * Result of a database update operation.
20 *
21 * @param <V> return value type
22 */
23public final class Result<V> {
24
25 public enum Status {
26 /**
27 * Indicates a successful update.
28 */
29 OK,
30
31 /**
32 * Indicates a failure due to underlying state being locked by another transaction.
33 */
34 LOCKED
35 }
36
37 private final Status status;
38 private final V value;
39
40 /**
41 * Creates a new Result instance with the specified value with status set to Status.OK.
42 *
43 * @param <V> result value type
44 * @param value result value
45 * @return Result instance
46 */
47 public static <V> Result<V> ok(V value) {
48 return new Result<>(value, Status.OK);
49 }
50
51 /**
52 * Creates a new Result instance with status set to Status.LOCKED.
53 *
54 * @param <V> result value type
55 * @return Result instance
56 */
57 public static <V> Result<V> locked() {
58 return new Result<>(null, Status.LOCKED);
59 }
60
61 private Result(V value, Status status) {
62 this.value = value;
63 this.status = status;
64 }
65
66 /**
67 * Returns true if this result indicates a successful execution i.e status is Status.OK.
68 *
69 * @return true if successful, false otherwise
70 */
71 public boolean success() {
72 return status == Status.OK;
73 }
74
75 /**
76 * Returns the status of database update operation.
77 * @return database update status
78 */
79 public Status status() {
80 return status;
81 }
82
83 /**
84 * Returns the return value for the update.
85 * @return value returned by database update. If the status is another
86 * other than Status.OK, this returns a null
87 */
88 public V value() {
89 return value;
90 }
91}