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/server/src/main/java/envoy/server/data/Contact.java

132 lines
3.5 KiB
Java

package envoy.server.data;
import java.time.Instant;
import java.util.*;
import javax.persistence.*;
/**
* This class acts as a superclass for all contacts, being {@link User}s and
* {@link Group}s.
*
* @author Maximilian Käfer
* @since Envoy Server Standalone v0.1-alpha
*/
@Entity
@Table(name = "contacts", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }) })
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Contact {
@Id
@GeneratedValue
protected long id;
protected String name;
@Column(name = "creation_date")
private Instant creationDate;
@ManyToMany(fetch = FetchType.EAGER)
protected Set<Contact> contacts;
/**
* @return a {@link envoy.data.Contact} object of this envoy.server.data.Contact
* object.
* @since Envoy Server Standalone v0.1-beta
*/
public abstract envoy.data.Contact toCommon();
/**
* Transforms this contact into a {@link envoy.data.Contact} where the contacts
* set of contacts is empty.
*
* @return a {@link envoy.data.Contact} object of this contact
* object.
* @since Envoy Server Standalone v0.1-beta
*/
protected abstract envoy.data.Contact toFlatCommon();
/**
* @return the ID of this contact.
* @since Envoy Server Standalone v0.1-beta
*/
public long getID() { return id; }
/**
* Sets the ID of this contact.
*
* @param id to set for this contact
* @since Envoy Server Standalone v0.1-beta
*/
public void setID(long id) { this.id = id; }
/**
* @return the name of this contact.
* @since Envoy Server Standalone v0.1-beta
*/
public String getName() { return name; }
/**
* Sets the name of this contact.
*
* @param name to set for this contact
* @since Envoy Server Standalone v0.1-beta
*/
public void setName(String name) { this.name = name; }
/**
* @return the contacts
* @since Envoy Server Standalone v0.1-beta
*/
public Set<Contact> getContacts() { return contacts; }
/**
* @param contacts the contacts to set
* @since Envoy Server Standalone v0.1-beta
*/
public void setContacts(Set<Contact> contacts) { this.contacts = contacts; }
/**
* @return the creationDate
* @since Envoy Server Standalone v0.2-beta
*/
public Instant getCreationDate() { return creationDate; }
/**
* @param creationDate the creationDate to set
* @since Envoy Server Standalone v0.2-beta
*/
public void setCreationDate(Instant creationDate) { this.creationDate = creationDate; }
/**
* Shortcut to convert a {@code Contact} into a {@code User}.
*
* @param contact the contact to convert
* @return the casted contact
* @throws IllegalStateException if the given contact is not a User
* @since Envoy Server v0.3-beta
*/
public static User toUser(Contact contact) {
if (!(contact instanceof User)) throw new IllegalStateException("Cannot cast a non user to a user");
return (User) contact;
}
/**
* Shortcut to convert a set of {@code Contact}s into a set of {@code User}s.
*
* @param contacts the contacts to convert
* @return the casted contacts
* @throws IllegalStateException if one of the given contacts is not a User
* @since Envoy Server v0.3-beta
*/
public static Set<User> toUser(Set<Contact> contacts) {
final var newSet = new HashSet<User>();
for (final var contact : contacts)
newSet.add(toUser(contact));
return newSet;
}
@Override
public String toString() { return String.format("%s[id=%d,name=%s,%d contact(s)]", getClass().getSimpleName(), id, name, contacts.size()); }
}