Fix faulty use of .map(ResultDelegate::firstOrNull)

This commit is contained in:
Paul Schaub 2019-12-26 08:46:55 +01:00
parent 6ae00a936b
commit 6cb9ce5362
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 41 additions and 14 deletions

View file

@ -1,5 +1,7 @@
package org.mercury_im.messenger.data.mapping;
import org.mercury_im.messenger.util.Optional;
import lombok.NonNull;
public abstract class AbstractMapping<E, M> implements Mapping<E, M> {
@ -12,6 +14,15 @@ public abstract class AbstractMapping<E, M> implements Mapping<E, M> {
return toModel(entity, newModel(entity));
}
@Override
public Optional<M> toModel(Optional<E> entity) {
if (entity.isPresent()) {
return new Optional<>(toModel(entity.getItem()));
} else {
return new Optional<>();
}
}
@Override
public E toEntity(M model) {
if (model == null) {
@ -20,6 +31,15 @@ public abstract class AbstractMapping<E, M> implements Mapping<E, M> {
return toEntity(model, newEntity(model));
}
@Override
public Optional<E> toEntity(Optional<M> model) {
if (model.isPresent()) {
return new Optional<>(toEntity(model.getItem()));
} else {
return new Optional<>();
}
}
@Override
public M toModel(E entity, M model) {
if (entity == null) {

View file

@ -1,5 +1,7 @@
package org.mercury_im.messenger.data.mapping;
import org.mercury_im.messenger.util.Optional;
/**
* Interface that defines a mapping between entities and database models.
*
@ -16,6 +18,8 @@ public interface Mapping<E, M> {
*/
M toModel(E entity);
Optional<M> toModel(Optional<E> entity);
/**
* Copy data from the model to a new entity.
* @param model database model
@ -23,6 +27,8 @@ public interface Mapping<E, M> {
*/
E toEntity(M model);
Optional<E> toEntity(Optional<M> model);
/**
* Map an entity to a model.
*

View file

@ -55,9 +55,8 @@ public class XmppAccountRepository
@Override
public Observable<Optional<Account>> observeAccount(UUID accountId) {
return dao.get(accountId).observableResult()
.map(ResultDelegate::firstOrNull)
.map(result -> new Optional<>(result.firstOrNull()))
.map(accountMapping::toEntity)
.map(Optional::new)
.subscribeOn(subscriberScheduler())
.observeOn(observerScheduler());
}
@ -73,9 +72,8 @@ public class XmppAccountRepository
@Override
public Observable<Optional<Account>> observeAccountByAddress(String address) {
return dao.get(address).observableResult()
.map(ResultDelegate::firstOrNull)
.map(result -> new Optional<>(result.firstOrNull()))
.map(accountMapping::toEntity)
.map(Optional::new)
.subscribeOn(subscriberScheduler())
.observeOn(observerScheduler());
}

View file

@ -60,9 +60,8 @@ public class XmppDirectChatRepository
@Override
public Observable<Optional<DirectChat>> observeDirectChat(UUID chatId) {
return dao.get(chatId).observableResult()
.map(ResultDelegate::firstOrNull)
.map(result -> new Optional<>(result.firstOrNull()))
.map(directChatMapping::toEntity)
.map(Optional::new)
.subscribeOn(subscriberScheduler())
.observeOn(observerScheduler());
}
@ -92,9 +91,8 @@ public class XmppDirectChatRepository
@Override
public Observable<Optional<DirectChat>> observeDirectChatByPeer(Peer peer) {
return dao.getByPeer(peer.getId()).observableResult()
.map(ReactiveResult::firstOrNull)
.map(result -> new Optional<>(result.firstOrNull()))
.map(directChatMapping::toEntity)
.map(Optional::new)
.subscribeOn(subscriberScheduler())
.observeOn(observerScheduler());
}

View file

@ -57,9 +57,8 @@ public class XmppGroupChatRepository
@Override
public Observable<Optional<GroupChat>> observeGroupChat(UUID chatId) {
return dao.get(chatId).observableResult()
.map(ResultDelegate::firstOrNull)
.map(result -> new Optional<>(result.firstOrNull()))
.map(groupChatMapping::toEntity)
.map(Optional::new)
.subscribeOn(subscriberScheduler())
.observeOn(observerScheduler());
}
@ -89,9 +88,8 @@ public class XmppGroupChatRepository
@Override
public Observable<Optional<GroupChat>> observeGroupChatByRoomAddress(UUID accountId, String roomAddress) {
return dao.get(accountId, roomAddress).observableResult()
.map(ResultDelegate::firstOrNull)
.map(result -> new Optional<>(result.firstOrNull()))
.map(groupChatMapping::toEntity)
.map(Optional::new)
.subscribeOn(subscriberScheduler())
.observeOn(observerScheduler());
}

View file

@ -80,9 +80,8 @@ public class XmppPeerRepository
.where(PeerModel.ACCOUNT_ID.eq(accountId))
.and(PeerModel.ADDRESS.eq(address))
.get().observableResult()
.map(ResultDelegate::firstOrNull)
.map(result -> new Optional<>(result.firstOrNull()))
.map(peerMapping::toEntity)
.map(Optional::new)
.subscribeOn(subscriberScheduler())
.observeOn(observerScheduler());
}

View file

@ -9,6 +9,10 @@ public class Optional<T> {
private final T item;
public Optional() {
this(null);
}
public Optional(T item) {
this.item = item;
}

View file

@ -0,0 +1,4 @@
package org.mercury_im.messenger.xmpp;
public class MercuryConnectionManager {
}