This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/event/GroupResizeEvent.java

98 lines
3.0 KiB
Java

package envoy.event;
import envoy.data.Contact;
import envoy.data.Group;
import envoy.data.User;
/**
* This event is used to communicate changes in the group size between client
* and server.<br>
* Possible actions are adding or removing certain {@link User}s to or from a
* certain {@link Group}
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupResizeEvent.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public class GroupResizeEvent extends Event<Long> {
private final long groupID;
private OperationType operationType;
private static final long serialVersionUID = 0L;
/**
* This enum defines all possibilities for handling
* {@link GroupResizeEvent}s.<br>
* These can be: {@link OperationType#ADD} or {@link OperationType#REMOVE}.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupResizeEvent.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public enum OperationType {
/**
* Select this element, if the given {@link User} should be added to the given
* {@link Group}
*/
ADD,
/**
* Select this element, if the given {@link User} should be removed from the
* given {@link Group}
*/
REMOVE
}
/**
* Initializes a {@link GroupResizeEvent} through a Contact where the name has
* already been set.
*
* @param user the {@link User} who wants to join or leave a group
* @param group the {@link Group} he wants to join or leave
* @param operationType whether the user should be removed from this group. If
* false,
* this user will be added
* @since Envoy Common v0.2-alpha
*/
public GroupResizeEvent(User user, Group group, OperationType operationType) {
super(user.getID());
if (group.getMemberIDs().contains(user.getID())) {
if (operationType.equals(OperationType.ADD)) throw new IllegalStateException(
"Cannot add " + user + " to group " + group.getID() + " because he is already a member of this group");
} else if (operationType.equals(OperationType.REMOVE))
throw new IllegalStateException("Cannot remove " + user + " from group " + group.getID() + " because he is no part of this group");
groupID = group.getID();
this.operationType = operationType;
}
/**
* @return the ID of the {@link Contact} this event is related to
* @since Envoy Common v0.2-alpha
*/
public long getGroupId() { return groupID; }
/**
* @return the operationType
* @since Envoy Common v0.1-beta
*/
public OperationType getOperationType() { return operationType; }
/**
* @param operationType the operationType to set
* @since Envoy Common v0.1-beta
*/
public void setOperationType(OperationType operationType) { this.operationType = operationType; }
/**
* {@inheritDoc}
*/
@Override
public String toString() { return String.format("GroupResizeEvent[userid=%d,groupid=%d,operationType=%b]", get(), groupID, operationType); }
}