Merge branch 'develop' into f/advanced_login

This commit is contained in:
Kai S. K. Engelbart 2020-02-12 06:24:17 +01:00 committed by GitHub
commit 3fcf990696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 179 additions and 95 deletions

View File

@ -7,38 +7,27 @@ import java.util.List;
* Project: <strong>envoy-common</strong><br>
* File: <strong>Contacts.java</strong><br>
* Created: <strong>02.01.2020</strong><br>
*
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class Contacts implements Serializable {
private final long userId;
private final List<User> contacts;
private final List<User> contacts;
private static final long serialVersionUID = 136970804968152871L;
/**
* Creates an instance of {@link Contacts}.
*
* @param userId the ID of the user this contacts belong to
* @param contacts the contact list
* @since Envoy Common v0.2-alpha
*/
public Contacts(long userId, List<User> contacts) {
this.userId = userId;
this.contacts = contacts;
}
public Contacts(List<User> contacts) { this.contacts = contacts; }
@Override
public String toString() { return String.format("Contacts[%s]", contacts); }
/**
* @return the ID of the user this contacts belong to
* @since Envoy Common v0.2-alpha
*/
public long getUserId() { return userId; }
/**
* @return a list of users messages can be sent to
* @since Envoy Common v0.2-alpha

View File

@ -0,0 +1,61 @@
package envoy.event;
import envoy.data.User;
/**
* Signifies the modification of a contact list.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactOperationEvent.java</strong><br>
* Created: <strong>05.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.2-alpha
*/
public class ContactOperationEvent extends Event<User> {
/**
* Specifies the operation performed on a contact list.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactOperationEvent.java</strong><br>
* Created: <strong>05.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.2-alpha
*/
public enum Operation {
/**
* Adds a user to the contact list.
*/
ADD,
/**
* Removes a user from the contact list.
*/
REMOVE;
}
private final Operation operationType;
private static final long serialVersionUID = -1166652868189511553L;
/**
* Initializes a {@link ContactOperationEvent}.
*
* @param contact the user on which the operation is performed
* @param operationType the type of operation to perform
* @since Envoy Common v0.2-alpha
*/
public ContactOperationEvent(User contact, Operation operationType) {
super(contact);
this.operationType = operationType;
}
/**
* @return the type of operation to perform
* @since Envoy Common v0.2-alpha
*/
public Operation getOperationType() { return operationType; }
}

View File

@ -0,0 +1,24 @@
package envoy.event;
/**
* Requests a contact search from the server.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactSearchRequest.java</strong><br>
* Created: <strong>05.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.2-alpha
*/
public class ContactSearchRequest extends Event<String> {
private static final long serialVersionUID = -7969312055630533627L;
/**
* Initializes a {@link ContactSearchRequest}.
*
* @param searchPhrase the search phrase to use in the contact search
* @since Envoy Common v0.2-alpha
*/
public ContactSearchRequest(String searchPhrase) { super(searchPhrase); }
}

View File

@ -0,0 +1,28 @@
package envoy.event;
import java.util.List;
import envoy.data.User;
/**
* Contains a list of {@link User}s for which a search was performed.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactSearchResult.java</strong><br>
* Created: <strong>11 Feb 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class ContactSearchResult extends Event<List<User>> {
private static final long serialVersionUID = -8934154116102031561L;
/**
* Creates an instance of {@link ContactSearchResult}.
*
* @param users the users found during the search
* @since Envoy Common v0.2-alpha
*/
public ContactSearchResult(List<User> users) { super(users); }
}

View File

@ -11,10 +11,36 @@ import java.io.Serializable;
* @param <T> the type of the Event
* @since Envoy v0.2-alpha
*/
public interface Event<T> extends Serializable {
public abstract class Event<T> implements Serializable {
protected final T value;
private static final long serialVersionUID = 4673659457380399167L;
protected Event(T value) { this.value = value; }
/**
* @return the data associated with this event
*/
default T get() { return null; }
public T get() { return value; }
@Override
public String toString() { return String.format("%s[value=%s]", this.getClass().getSimpleName(), value); }
/**
* Serves as a super class for events that do not carry a value.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Event.java</strong><br>
* Created: <strong>11 Feb 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public static abstract class Valueless extends Event<Void> {
private static final long serialVersionUID = -9019362144094097997L;
protected Valueless() { super(null); }
}
}

View File

@ -1,18 +1,15 @@
package envoy.event;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Consumer;
/**
* This class handles events by allowing {@link EventHandler} object to register
* themselves and then be notified about certain events dispatched by the event
* bus.<br>
* This class handles events by allowing event handlers to register themselves
* and then be notified about certain events dispatched by the event bus.<br>
* <br>
* The event bus is a singleton and can be used across the entire application to
* guarantee the propagation of events.<br>
*
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>EventBus.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
@ -23,14 +20,13 @@ import java.util.Map;
public class EventBus {
/**
* Contains all {@link EventHandler} instances registered at this
* {@link EventBus} as values mapped to by their supported {@link Event}
* classes.
* Contains all event handler instances registered at this event bus as values
* mapped to by their supported event classes.
*/
private Map<Class<? extends Event<?>>, List<EventHandler>> handlers = new HashMap<>();
private Map<Class<? extends Event<?>>, List<Consumer<Event<?>>>> handlers = new HashMap<>();
/**
* The singleton instance of this {@link EventBus} that is used across the
* The singleton instance of this event bus that is used across the
* entire application.
*/
private static EventBus eventBus = new EventBus();
@ -43,39 +39,40 @@ public class EventBus {
private EventBus() {}
/**
* @return the singleton instance of the {@link EventBus}
* @return the singleton instance of the event bus
* @since Envoy v0.2-alpha
*/
public static EventBus getInstance() { return eventBus; }
/**
* Registers an {@link EventHandler} to be notified when a
* {@link Event} of a certain type is dispatched.
* Registers an event handler to be notified when an
* event of a certain type is dispatched.
*
* @param eventClass the class which the {@link EventHandler} is subscribed to
* @param handler the {@link EventHandler} to register
* @param <T> the type of event values to notify the handler about
* @param eventClass the class which the event handler is subscribing to
* @param handler the event handler to register
* @since Envoy v0.2-alpha
*/
public void register(Class<? extends Event<?>> eventClass, EventHandler handler) {
@SuppressWarnings("unchecked")
public <T extends Event<?>> void register(Class<T> eventClass, Consumer<T> handler) {
if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>());
handlers.get(eventClass).add(handler);
handlers.get(eventClass).add((Consumer<Event<?>>) handler);
}
/**
* Dispatches a {@link Event} to every {@link EventHandler} subscribed to it.
* Dispatches an event to every event handler subscribed to it.
*
* @param event the {@link Event} to dispatch
* @since Envoy v0.2-alpha
*/
public void dispatch(Event<?> event) {
handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.handle(event));
handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.accept(event));
}
/**
* @return a map of all {@link EventHandler} instances currently registered at
* this {@link EventBus} with the {@link Event} classes they are
* subscribed to as keys
* @return a map of all event handler instances currently registered at this
* event bus with the event classes they are subscribed to as keys
* @since Envoy v0.2-alpha
*/
public Map<Class<? extends Event<?>>, List<EventHandler>> getHandlers() { return handlers; }
public Map<Class<? extends Event<?>>, List<Consumer<Event<?>>>> getHandlers() { return handlers; }
}

View File

@ -1,18 +0,0 @@
package envoy.event;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>EventHandler.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
public interface EventHandler {
/**
* Consumes an event dispatched by the event bus.
*
* @param event The event dispatched by the event bus, only of supported type
*/
void handle(Event<?> event);
}

View File

@ -13,7 +13,7 @@ import envoy.data.LoginCredentials;
* @author Kai S. K. Engelbart
* @since Envoy Common v0.3-alpha
*/
public class HandshakeRejectionEvent implements Event<String> {
public class HandshakeRejectionEvent extends Event<String> {
/**
* Select this value if a given password hash was incorrect
@ -53,7 +53,7 @@ public class HandshakeRejectionEvent implements Event<String> {
*
* @since Envoy Common v0.3-alpha
*/
public HandshakeRejectionEvent() { this(""); }
public HandshakeRejectionEvent() { super(""); }
/**
* Creates an instance of {@link HandshakeRejectionEvent}.
@ -61,12 +61,5 @@ public class HandshakeRejectionEvent implements Event<String> {
* @param reason the reason why the handshake was rejected
* @since Envoy Common v0.3-alpha
*/
public HandshakeRejectionEvent(String reason) { this.reason = reason; }
/**
* @return the reason why the handshake was rejected
* @since Envoy Common v0.3-alpha
*/
@Override
public String get() { return reason; }
public HandshakeRejectionEvent(String reason) { super(reason); }
}

View File

@ -11,7 +11,7 @@ package envoy.event;
* @author Kai S. K. Engelbart
* @since Envoy Common v0.3-alpha
*/
public class IdGeneratorRequest implements Event<Void> {
public class IdGeneratorRequest extends Event.Valueless {
private static final long serialVersionUID = 1431107413883364583L;
}

View File

@ -12,10 +12,9 @@ import envoy.data.Message;
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class MessageStatusChangeEvent implements Event<Message.MessageStatus> {
public class MessageStatusChangeEvent extends Event<Message.MessageStatus> {
private final long id;
private final Message.MessageStatus status;
private final Date date;
private static final long serialVersionUID = 4566145392192761313L;
@ -30,8 +29,8 @@ public class MessageStatusChangeEvent implements Event<Message.MessageStatus> {
* @since Envoy Common v0.2-alpha
*/
public MessageStatusChangeEvent(long id, Message.MessageStatus status, Date date) {
super(status);
this.id = id;
this.status = status;
this.date = date;
}
@ -43,13 +42,6 @@ public class MessageStatusChangeEvent implements Event<Message.MessageStatus> {
*/
public MessageStatusChangeEvent(Message message) { this(message.getId(), message.getStatus(), new Date()); }
/**
* @return the status of the {@link Message} this event is related to
* @since Envoy Common v0.2-alpha
*/
@Override
public Message.MessageStatus get() { return status; }
/**
* @return the ID of the {@link Message} this event is related to
* @since Envoy Common v0.2-alpha
@ -63,5 +55,5 @@ public class MessageStatusChangeEvent implements Event<Message.MessageStatus> {
public Date getDate() { return date; }
@Override
public String toString() { return String.format("MessageStatusChangeEvent[id=%d,status=%s,date=%s]", id, status, date); }
public String toString() { return String.format("MessageStatusChangeEvent[id=%d,status=%s,date=%s]", id, value, date); }
}

View File

@ -11,10 +11,9 @@ import envoy.data.User.UserStatus;
* @author Leon Hofmeister
* @since Envoy Common v0.2-alpha
*/
public class UserStatusChangeEvent implements Event<UserStatus> {
public class UserStatusChangeEvent extends Event<UserStatus> {
private final long id;
private final User.UserStatus status;
private final long id;
private static final long serialVersionUID = 4566145392192761313L;
@ -27,8 +26,8 @@ public class UserStatusChangeEvent implements Event<UserStatus> {
* @since Envoy Common v0.2-alpha
*/
public UserStatusChangeEvent(long id, User.UserStatus status) {
this.id = id;
this.status = status;
super(status);
this.id = id;
}
/**
@ -39,13 +38,6 @@ public class UserStatusChangeEvent implements Event<UserStatus> {
*/
public UserStatusChangeEvent(User user) { this(user.getId(), user.getStatus()); }
/**
* @return the status of the {@link User} this event is related to
* @since Envoy Common v0.2-alpha
*/
@Override
public User.UserStatus get() { return status; }
/**
* @return the ID of the {@link User} this event is related to
* @since Envoy Common v0.2-alpha
@ -53,5 +45,5 @@ public class UserStatusChangeEvent implements Event<UserStatus> {
public long getId() { return id; }
@Override
public String toString() { return String.format("UserStatusChangeEvent[id=%d,status=%s]", id, status); }
public String toString() { return String.format("UserStatusChangeEvent[id=%d,status=%s]", id, value); }
}