2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.eos.common.api;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
16 * Enumerates the ownership change states for an entity.
18 * @author Thomas Pantelis
20 public enum EntityOwnershipChangeState {
22 * The local process instance has been granted ownership.
24 LOCAL_OWNERSHIP_GRANTED(false, true, true),
27 * The local process instance has lost ownership and another process instance is now the owner.
29 LOCAL_OWNERSHIP_LOST_NEW_OWNER(true, false, true),
32 * The local process instance has lost ownership and there are no longer any candidates for the entity and
35 LOCAL_OWNERSHIP_LOST_NO_OWNER(true, false, false),
38 * The local process instance ownership has not changed but some other aspect has changed (for example inJeopardy).
40 LOCAL_OWNERSHIP_RETAINED_WITH_NO_CHANGE(true, true, true),
43 * Entity ownership has transitioned to another process instance and this instance was not the previous owner.
45 REMOTE_OWNERSHIP_CHANGED(false, false, true),
48 * A remote process instance has lost ownership and there are no longer any candidates for the entity and
51 REMOTE_OWNERSHIP_LOST_NO_OWNER(false, false, false);
53 private static final Map<Key, EntityOwnershipChangeState> BY_KEY;
55 final Builder<Key, EntityOwnershipChangeState> builder = ImmutableMap.<Key, EntityOwnershipChangeState>builder();
56 for(final EntityOwnershipChangeState e: values()) {
57 builder.put(new Key(e.wasOwner, e.isOwner, e.hasOwner), e);
60 BY_KEY = builder.build();
63 private final boolean wasOwner;
64 private final boolean isOwner;
65 private final boolean hasOwner;
67 private EntityOwnershipChangeState(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
68 this.wasOwner = wasOwner;
69 this.isOwner = isOwner;
70 this.hasOwner = hasOwner;
74 * Returns the previous ownership status of the entity for this process instance.
75 * @return true if this process was the owner of the entity at the time this notification was generated
77 public boolean wasOwner() {
82 * Returns the current ownership status of the entity for this process instance.
83 * @return true if this process is now the owner of the entity
85 public boolean isOwner() {
90 * Returns the current ownership status of the entity across all process instances.
91 * @return true if the entity has an owner which may or may not be this process. If false, then
92 * the entity has no candidates and thus no owner.
94 public boolean hasOwner() {
99 public String toString() {
100 return name() + " [wasOwner=" + wasOwner + ", isOwner=" + isOwner + ", hasOwner=" + hasOwner + "]";
103 public static EntityOwnershipChangeState from(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
104 final EntityOwnershipChangeState state = BY_KEY.get(new Key(wasOwner, isOwner, hasOwner));
105 Preconditions.checkArgument(state != null, "Invalid combination of wasOwner: %s, isOwner: %s, hasOwner: %s",
106 wasOwner, isOwner, hasOwner);
110 private static final class Key {
111 private final boolean wasOwner;
112 private final boolean isOwner;
113 private final boolean hasOwner;
115 Key(final boolean wasOwner, final boolean isOwner, final boolean hasOwner) {
116 this.wasOwner = wasOwner;
117 this.isOwner = isOwner;
118 this.hasOwner = hasOwner;
122 public int hashCode() {
123 final int prime = 31;
125 result = prime * result + (hasOwner ? 1231 : 1237);
126 result = prime * result + (isOwner ? 1231 : 1237);
127 result = prime * result + (wasOwner ? 1231 : 1237);
132 public boolean equals(final Object obj) {
133 final Key other = (Key) obj;
134 return hasOwner == other.hasOwner && isOwner == other.isOwner && wasOwner == other.wasOwner;