From 97545fc28531a8a40e95242c444e20e3b86b05b0 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 9 Apr 2020 21:01:19 +0200 Subject: [PATCH] Contact conversion does not result in endless recursion anymore --- src/main/java/envoy/server/data/Contact.java | 10 ++++++++++ src/main/java/envoy/server/data/Group.java | 10 +++++++--- src/main/java/envoy/server/data/User.java | 9 ++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java index eea8fe1..b629c37 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -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 diff --git a/src/main/java/envoy/server/data/Group.java b/src/main/java/envoy/server/data/Group.java index 6301624..595b252 100644 --- a/src/main/java/envoy/server/data/Group.java +++ b/src/main/java/envoy/server/data/Group.java @@ -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.
@@ -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(); } } diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index fa6db47..3fb167f 100755 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -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