blob: 4bc783b35be9820cd4c597438a15ebedf0b5ff5b [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani5e5b3d62016-02-01 16:03:33 -08003 *
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.primitives.resources.impl;
17
Madan Jampani5e5b3d62016-02-01 16:03:33 -080018import com.google.common.base.MoreObjects;
Jordan Halterman71635ae2017-07-28 10:35:43 -070019import org.onosproject.store.service.Versioned;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080020
21/**
22 * Result of a map entry update operation.
23 * <p>
24 * Both old and new values are accessible along with a flag that indicates if the
25 * the value was updated. If flag is false, oldValue and newValue both
26 * point to the same unmodified value.
Madan Jampanicadd70b2016-02-08 13:45:43 -080027 *
28 * @param <K> key type
Madan Jampani5e5b3d62016-02-01 16:03:33 -080029 * @param <V> result type
30 */
31public class MapEntryUpdateResult<K, V> {
32
33 public enum Status {
34
35 /**
36 * Indicates a successful update.
37 */
38 OK,
39
40 /**
41 * Indicates a noop i.e. existing and new value are both null.
42 */
43 NOOP,
44
45 /**
46 * Indicates a failed update due to a write lock.
47 */
48 WRITE_LOCK,
49
50 /**
51 * Indicates a failed update due to a precondition check failure.
52 */
53 PRECONDITION_FAILED
54 }
55
Jordan Halterman71635ae2017-07-28 10:35:43 -070056 private final Status status;
57 private final long version;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080058 private final K key;
Jordan Halterman71635ae2017-07-28 10:35:43 -070059 private final Versioned<V> result;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080060
Jordan Halterman71635ae2017-07-28 10:35:43 -070061 public MapEntryUpdateResult(Status status, long version, K key, Versioned<V> result) {
Madan Jampani5e5b3d62016-02-01 16:03:33 -080062 this.status = status;
Jordan Halterman71635ae2017-07-28 10:35:43 -070063 this.version = version;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080064 this.key = key;
Jordan Halterman71635ae2017-07-28 10:35:43 -070065 this.result = result;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080066 }
67
68 /**
69 * Returns {@code true} if the update was successful.
70 * @return {@code true} if yes, {@code false} otherwise
71 */
72 public boolean updated() {
73 return status == Status.OK;
74 }
75
76 /**
Madan Jampani5e5b3d62016-02-01 16:03:33 -080077 * Returns the update status.
78 * @return update status
79 */
80 public Status status() {
81 return status;
82 }
83
84 /**
Jordan Halterman71635ae2017-07-28 10:35:43 -070085 * Returns the result version.
86 * @return result version
Madan Jampani5e5b3d62016-02-01 16:03:33 -080087 */
Jordan Halterman71635ae2017-07-28 10:35:43 -070088 public long version() {
89 return version;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080090 }
91
92 /**
Jordan Halterman71635ae2017-07-28 10:35:43 -070093 * Returns the value.
94 * @return the value associated with key if updated was successful, otherwise current value
Madan Jampani5e5b3d62016-02-01 16:03:33 -080095 */
Jordan Halterman71635ae2017-07-28 10:35:43 -070096 public Versioned<V> result() {
97 return result;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080098 }
99
100 @Override
101 public String toString() {
102 return MoreObjects.toStringHelper(MapEntryUpdateResult.class)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800103 .add("status", status)
104 .add("key", key)
Jordan Halterman71635ae2017-07-28 10:35:43 -0700105 .add("result", result)
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800106 .toString();
107 }
108}