Contact conversion does not result in endless recursion anymore

This commit is contained in:
delvh 2020-04-09 21:01:19 +02:00
parent 8096d3ff58
commit 97545fc285
3 changed files with 25 additions and 4 deletions

View File

@ -35,6 +35,16 @@ public abstract class Contact {
*/
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

View File

@ -6,8 +6,6 @@ import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import envoy.data.User;
/**
* This class serves as a way to let Hibernate communicate with the server
* without bringing the dependency of JPA/Hibernate into the client.<br>
@ -31,6 +29,12 @@ public class Group extends Contact {
*/
@Override
public envoy.data.Group toCommon() {
return new envoy.data.Group(id, name, contacts.parallelStream().map(Contact::toCommon).map(User.class::cast).collect(Collectors.toSet()));
return new envoy.data.Group(id, name, contacts.parallelStream().map(User.class::cast).map(User::toFlatCommon).collect(Collectors.toSet()));
}
/**
* {@inheritDoc}
*/
@Override
protected envoy.data.Group toFlatCommon() { return toCommon(); }
}

View File

@ -1,6 +1,7 @@
package envoy.server.data;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.*;
@ -45,9 +46,15 @@ public class User extends Contact {
*/
@Override
public envoy.data.User toCommon() {
return new envoy.data.User(id, name, status, contacts.stream().map(Contact::toCommon).collect(Collectors.toSet()));
return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet()));
}
/**
* {@inheritDoc}
*/
@Override
protected envoy.data.User toFlatCommon() { return new envoy.data.User(id, name, status, Set.of()); }
/**
* @return the password hash
* @since Envoy Server Standalone v0.1-alpha