package envoy.data;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Set;
/**
* Project: envoy-common
* File: Group.java
* Created: 24 Mar 2020
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public final class Group extends Contact {
private static final long serialVersionUID = 0L;
/**
* Creates a new instance of a {@link Group} without any members.
*
* @param id the ID of this group
* @param name the name of this group
* @since Envoy Common v0.1-beta
*/
public Group(long id, String name) { this(id, name, new HashSet()); }
/**
* Creates an instance of a {@link Group}.
*
* @param id the ID of this group
* @param name the name of this group
* @param members all members that should be preinitialized
* @since Envoy Common v0.1-beta
*/
public Group(long id, String name, Set members) { super(id, name, members); }
@Override
public String toString() { return String.format("Group[id=%d,name=%s,%d member(s)]", id, name, contacts.size()); }
private void readObject(ObjectInputStream inputStream) throws Exception {
inputStream.defaultReadObject();
var contacts = Contact.class.getDeclaredField("contacts");
contacts.setAccessible(true);
contacts.set(this, inputStream.readObject());
}
private void writeObject(ObjectOutputStream outputStream) throws Exception {
outputStream.defaultWriteObject();
getContacts().forEach(user -> user.serializeContacts(false));
outputStream.writeObject(getContacts());
}
@Override
public Set getContacts() { return (Set) contacts; }
}