Rename package in EosCommonApi
[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      * Entity ownership has transitioned to another process instance and this instance was not the previous owner.
39      */
40     REMOTE_OWNERSHIP_CHANGED(false, false, true),
41
42     /**
43      * A remote process instance has lost ownership and there are no longer any candidates for the entity and
44      * thus has no owner.
45      */
46     REMOTE_OWNERSHIP_LOST_NO_OWNER(false, false, false);
47
48     private static final Map<Key, EntityOwnershipChangeState> BY_KEY;
49     static {
50         Builder<Key, EntityOwnershipChangeState> builder = ImmutableMap.<Key, EntityOwnershipChangeState>builder();
51         for(EntityOwnershipChangeState e: values()) {
52             builder.put(new Key(e.wasOwner, e.isOwner, e.hasOwner), e);
53         }
54
55         BY_KEY = builder.build();
56     }
57
58     private final boolean wasOwner;
59     private final boolean isOwner;
60     private final boolean hasOwner;
61
62     private EntityOwnershipChangeState(boolean wasOwner, boolean isOwner, boolean hasOwner) {
63         this.wasOwner = wasOwner;
64         this.isOwner = isOwner;
65         this.hasOwner = hasOwner;
66     }
67
68     /**
69      * Returns the previous ownership status of the entity for this process instance.
70      * @return true if this process was the owner of the entity at the time this notification was generated
71      */
72     public boolean wasOwner() {
73         return wasOwner;
74     }
75
76     /**
77      * Returns the current ownership status of the entity for this process instance.
78      * @return true if this process is now the owner of the entity
79      */
80     public boolean isOwner() {
81         return isOwner;
82     }
83
84     /**
85      * Returns the current ownership status of the entity across all process instances.
86      * @return true if the entity has an owner which may or may not be this process. If false, then
87      *         the entity has no candidates and thus no owner.
88      */
89     public boolean hasOwner() {
90         return hasOwner;
91     }
92
93     @Override
94     public String toString() {
95         return name() + " [wasOwner=" + wasOwner + ", isOwner=" + isOwner + ", hasOwner=" + hasOwner + "]";
96     }
97
98     public static EntityOwnershipChangeState from(boolean wasOwner, boolean isOwner, boolean hasOwner) {
99         EntityOwnershipChangeState state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
100         Preconditions.checkArgument(state != null, "Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s",
101                 wasOwner, isOwner, hasOwner);
102         return state;
103     }
104
105     private static final class Key {
106         private final boolean wasOwner;
107         private final boolean isOwner;
108         private final boolean hasOwner;
109
110         Key(boolean wasOwner, boolean isOwner, boolean hasOwner) {
111             this.wasOwner = wasOwner;
112             this.isOwner = isOwner;
113             this.hasOwner = hasOwner;
114         }
115
116         @Override
117         public int hashCode() {
118             final int prime = 31;
119             int result = 1;
120             result = prime * result + (hasOwner ? 1231 : 1237);
121             result = prime * result + (isOwner ? 1231 : 1237);
122             result = prime * result + (wasOwner ? 1231 : 1237);
123             return result;
124         }
125
126         @Override
127         public boolean equals(Object obj) {
128             Key other = (Key) obj;
129             return hasOwner == other.hasOwner && isOwner == other.isOwner && wasOwner == other.wasOwner;
130         }
131     }
132 }