Modernize EntityOwnershipChangeState
[mdsal.git] / entityownership / mdsal-eos-common-api / src / main / java / org / opendaylight / mdsal / eos / common / api / EntityOwnershipChangeState.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.eos.common.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.collect.ImmutableMap;
13
14 /**
15  * Enumerates the ownership change states for an entity.
16  *
17  * @author Thomas Pantelis
18  */
19 public enum EntityOwnershipChangeState {
20     /**
21      * The local process instance has been granted ownership.
22      */
23     LOCAL_OWNERSHIP_GRANTED(false, true, true),
24
25     /**
26      * The local process instance has lost ownership and another process instance is now the owner.
27      */
28     LOCAL_OWNERSHIP_LOST_NEW_OWNER(true, false, true),
29
30     /**
31      * The local process instance has lost ownership and there are no longer any candidates for the entity and
32      * thus has no owner.
33      */
34     LOCAL_OWNERSHIP_LOST_NO_OWNER(true, false, false),
35
36     /**
37      * The local process instance ownership has not changed but some other aspect has changed (for example inJeopardy).
38      */
39     LOCAL_OWNERSHIP_RETAINED_WITH_NO_CHANGE(true, true, true),
40
41     /**
42      * Entity ownership has transitioned to another process instance and this instance was not the previous owner.
43      */
44     REMOTE_OWNERSHIP_CHANGED(false, false, true),
45
46     /**
47      * A remote process instance has lost ownership and there are no longer any candidates for the entity and
48      * thus has no owner.
49      */
50     REMOTE_OWNERSHIP_LOST_NO_OWNER(false, false, false);
51
52     private static final ImmutableMap<Key, EntityOwnershipChangeState> BY_KEY;
53
54     static {
55         final var builder = ImmutableMap.<Key, EntityOwnershipChangeState>builder();
56         for (final EntityOwnershipChangeState e : values()) {
57             builder.put(new Key(e.wasOwner, e.isOwner, e.hasOwner), e);
58         }
59         BY_KEY = builder.build();
60     }
61
62     private final boolean wasOwner;
63     private final boolean isOwner;
64     private final boolean hasOwner;
65
66     EntityOwnershipChangeState(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
67         this.wasOwner = wasOwner;
68         this.isOwner = isOwner;
69         this.hasOwner = hasOwner;
70     }
71
72     /**
73      * Returns the previous ownership status of the entity for this process instance.
74      * @return true if this process was the owner of the entity at the time this notification was generated
75      */
76     public boolean wasOwner() {
77         return wasOwner;
78     }
79
80     /**
81      * Returns the current ownership status of the entity for this process instance.
82      * @return true if this process is now the owner of the entity
83      */
84     public boolean isOwner() {
85         return isOwner;
86     }
87
88     /**
89      * Returns the current ownership status of the entity across all process instances.
90      * @return true if the entity has an owner which may or may not be this process. If false, then
91      *         the entity has no candidates and thus no owner.
92      */
93     public boolean hasOwner() {
94         return hasOwner;
95     }
96
97     @Override
98     public String toString() {
99         return name() + " [wasOwner=" + wasOwner + ", isOwner=" + isOwner + ", hasOwner=" + hasOwner + "]";
100     }
101
102     public static EntityOwnershipChangeState from(
103             final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
104         final EntityOwnershipChangeState state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
105         checkArgument(state != null, "Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s",
106                 wasOwner, isOwner, hasOwner);
107         return state;
108     }
109
110     private static final class Key {
111         private final boolean wasOwner;
112         private final boolean isOwner;
113         private final boolean hasOwner;
114
115         Key(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
116             this.wasOwner = wasOwner;
117             this.isOwner = isOwner;
118             this.hasOwner = hasOwner;
119         }
120
121         @Override
122         public int hashCode() {
123             final int prime = 31;
124             int result = 1;
125             result = prime * result + Boolean.hashCode(hasOwner);
126             result = prime * result + Boolean.hashCode(isOwner);
127             result = prime * result + Boolean.hashCode(wasOwner);
128             return result;
129         }
130
131         @Override
132         public boolean equals(final Object obj) {
133             return obj == this || obj instanceof Key other
134                 && hasOwner == other.hasOwner && isOwner == other.isOwner && wasOwner == other.wasOwner;
135         }
136     }
137 }