Fix checkstyle
[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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import java.util.Map;
14
15 /**
16  * Enumerates the ownership change states for an entity.
17  *
18  * @author Thomas Pantelis
19  */
20 public enum EntityOwnershipChangeState {
21     /**
22      * The local process instance has been granted ownership.
23      */
24     LOCAL_OWNERSHIP_GRANTED(false, true, true),
25
26     /**
27      * The local process instance has lost ownership and another process instance is now the owner.
28      */
29     LOCAL_OWNERSHIP_LOST_NEW_OWNER(true, false, true),
30
31     /**
32      * The local process instance has lost ownership and there are no longer any candidates for the entity and
33      * thus has no owner.
34      */
35     LOCAL_OWNERSHIP_LOST_NO_OWNER(true, false, false),
36
37     /**
38      * The local process instance ownership has not changed but some other aspect has changed (for example inJeopardy).
39      */
40     LOCAL_OWNERSHIP_RETAINED_WITH_NO_CHANGE(true, true, true),
41
42     /**
43      * Entity ownership has transitioned to another process instance and this instance was not the previous owner.
44      */
45     REMOTE_OWNERSHIP_CHANGED(false, false, true),
46
47     /**
48      * A remote process instance has lost ownership and there are no longer any candidates for the entity and
49      * thus has no owner.
50      */
51     REMOTE_OWNERSHIP_LOST_NO_OWNER(false, false, false);
52
53     private static final Map<Key, EntityOwnershipChangeState> BY_KEY;
54
55     static {
56         final Builder<Key, EntityOwnershipChangeState> builder = ImmutableMap.builder();
57         for (final EntityOwnershipChangeState e: values()) {
58             builder.put(new Key(e.wasOwner, e.isOwner, e.hasOwner), e);
59         }
60
61         BY_KEY = builder.build();
62     }
63
64     private final boolean wasOwner;
65     private final boolean isOwner;
66     private final boolean hasOwner;
67
68     EntityOwnershipChangeState(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
69         this.wasOwner = wasOwner;
70         this.isOwner = isOwner;
71         this.hasOwner = hasOwner;
72     }
73
74     /**
75      * Returns the previous ownership status of the entity for this process instance.
76      * @return true if this process was the owner of the entity at the time this notification was generated
77      */
78     public boolean wasOwner() {
79         return wasOwner;
80     }
81
82     /**
83      * Returns the current ownership status of the entity for this process instance.
84      * @return true if this process is now the owner of the entity
85      */
86     public boolean isOwner() {
87         return isOwner;
88     }
89
90     /**
91      * Returns the current ownership status of the entity across all process instances.
92      * @return true if the entity has an owner which may or may not be this process. If false, then
93      *         the entity has no candidates and thus no owner.
94      */
95     public boolean hasOwner() {
96         return hasOwner;
97     }
98
99     @Override
100     public String toString() {
101         return name() + " [wasOwner=" + wasOwner + ", isOwner=" + isOwner + ", hasOwner=" + hasOwner + "]";
102     }
103
104     public static EntityOwnershipChangeState from(
105             final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
106         final EntityOwnershipChangeState state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
107         Preconditions.checkArgument(state != null, "Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s",
108                 wasOwner, isOwner, hasOwner);
109         return state;
110     }
111
112     private static final class Key {
113         private final boolean wasOwner;
114         private final boolean isOwner;
115         private final boolean hasOwner;
116
117         Key(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
118             this.wasOwner = wasOwner;
119             this.isOwner = isOwner;
120             this.hasOwner = hasOwner;
121         }
122
123         @Override
124         public int hashCode() {
125             final int prime = 31;
126             int result = 1;
127             result = prime * result + (hasOwner ? 1231 : 1237);
128             result = prime * result + (isOwner ? 1231 : 1237);
129             result = prime * result + (wasOwner ? 1231 : 1237);
130             return result;
131         }
132
133         @Override
134         public boolean equals(final Object obj) {
135             if (obj == this) {
136                 return true;
137             }
138             if (!(obj instanceof Key)) {
139                 return false;
140             }
141             final Key other = (Key) obj;
142             return hasOwner == other.hasOwner && isOwner == other.isOwner && wasOwner == other.wasOwner;
143         }
144     }
145 }