package envoy.event; import static envoy.event.ElementOperation.*; import envoy.data.*; /** * This event is used to communicate changes in the group size between client and server. *

* Possible actions are adding or removing certain {@link User}s to or from a certain {@link Group}. * * @author Leon Hofmeister * @since Envoy Common v0.1-beta */ public final class GroupResize extends Event { private final long groupID; private final ElementOperation operation; private static final long serialVersionUID = 0L; /** * Initializes a {@link GroupResize} 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 operation describes what to do with the given user:
* add him to this group or remove him from it * @since Envoy Common v0.2-alpha */ public GroupResize(User user, Group group, ElementOperation operation) { super(user); this.operation = operation; final var contained = group.getContacts().contains(user); if (contained && operation.equals(ADD)) throw new IllegalArgumentException(String.format("Cannot add %s to %s!", user, group)); else if (operation.equals(REMOVE) && !contained) throw new IllegalArgumentException( String.format("Cannot remove %s from %s!", user, group)); groupID = group.getID(); } /** * @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 ElementOperation getOperation() { return operation; } /** * Applies the operation to a group. * * @param group the group to resize * @since Envoy Common v0.2-beta */ public void apply(Group group) { switch (operation) { case ADD: group.getContacts().add(value); break; case REMOVE: group.getContacts().remove(value); break; } } @Override public String toString() { return String.format("GroupResize[user=%s,groupid=%d,operation=%s]", get(), groupID, operation); } }