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

91 lines
2.8 KiB
Java
Raw Normal View History

package envoy.event;
import envoy.data.Contact;
import envoy.data.Group;
import envoy.data.User;
/**
2020-03-25 17:12:55 +01:00
* 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}
2020-03-25 17:12:55 +01:00
* <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;
2020-03-26 15:49:08 +01:00
private final Operation operation;
private static final long serialVersionUID = 0L;
/**
2020-03-25 17:12:55 +01:00
* This enum defines all possibilities for handling
* {@link GroupResizeEvent}s.<br>
2020-03-26 12:45:10 +01:00
* These can be: {@link Operation#ADD} or {@link Operation#REMOVE}.<br>
2020-03-25 17:12:55 +01:00
* <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
*/
2020-03-26 12:45:10 +01:00
public enum Operation {
/**
2020-03-25 17:12:55 +01:00
* Select this element, if the given {@link User} should be added to the given
* {@link Group}
*/
ADD,
/**
2020-03-25 17:12:55 +01:00
* 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.
*
2020-03-26 12:45:10 +01:00
* @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 operation whether the user should be removed from this group. If
* false, this user will be added.
* @since Envoy Common v0.2-alpha
*/
2020-03-26 12:45:10 +01:00
public GroupResizeEvent(User user, Group group, Operation operation) {
super(user.getID());
if (group.getMemberIDs().contains(user.getID())) {
2020-03-26 12:45:10 +01:00
if (operation.equals(Operation.ADD)) throw new IllegalStateException(
"Cannot add " + user + " to group " + group.getID() + " because he is already a member of this group");
2020-03-26 12:45:10 +01:00
} else if (operation.equals(Operation.REMOVE))
throw new IllegalStateException("Cannot remove " + user + " from group " + group.getID() + " because he is no part of this group");
groupID = group.getID();
2020-03-26 12:45:10 +01:00
this.operation = operation;
}
/**
* @return the ID of the {@link Contact} this event is related to
* @since Envoy Common v0.2-alpha
*/
2020-03-26 12:45:10 +01:00
public long getGroupID() { return groupID; }
/**
* @return the operationType
* @since Envoy Common v0.1-beta
*/
2020-03-26 15:49:08 +01:00
public Operation getOperation() { return operation; }
/**
* {@inheritDoc}
*/
@Override
2020-03-26 15:43:30 +01:00
public String toString() { return String.format("GroupResizeEvent[userid=%d,groupid=%d,operation=%s]", get(), groupID, operation); }
}