Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 19 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 20 | |
| 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 Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 27 | * |
| 28 | * @param <K> key type |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 29 | * @param <V> result type |
| 30 | */ |
| 31 | public 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 Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 56 | private final Status status; |
| 57 | private final long version; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 58 | private final K key; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 59 | private final Versioned<V> result; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 60 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 61 | public MapEntryUpdateResult(Status status, long version, K key, Versioned<V> result) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 62 | this.status = status; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 63 | this.version = version; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 64 | this.key = key; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 65 | this.result = result; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 66 | } |
| 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 Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 77 | * Returns the update status. |
| 78 | * @return update status |
| 79 | */ |
| 80 | public Status status() { |
| 81 | return status; |
| 82 | } |
| 83 | |
| 84 | /** |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 85 | * Returns the result version. |
| 86 | * @return result version |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 87 | */ |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 88 | public long version() { |
| 89 | return version; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 93 | * Returns the value. |
| 94 | * @return the value associated with key if updated was successful, otherwise current value |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 95 | */ |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 96 | public Versioned<V> result() { |
| 97 | return result; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String toString() { |
| 102 | return MoreObjects.toStringHelper(MapEntryUpdateResult.class) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 103 | .add("status", status) |
| 104 | .add("key", key) |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 105 | .add("result", result) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 106 | .toString(); |
| 107 | } |
| 108 | } |