mirror of
https://codeberg.org/Mercury-IM/Mercury-IM
synced 2024-06-18 09:34:52 +02:00
Fix faulty use of .map(ResultDelegate::firstOrNull)
This commit is contained in:
parent
6ae00a936b
commit
6cb9ce5362
|
@ -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) {
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -9,6 +9,10 @@ public class Optional<T> {
|
|||
|
||||
private final T item;
|
||||
|
||||
public Optional() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public Optional(T item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
package org.mercury_im.messenger.xmpp;
|
||||
|
||||
public class MercuryConnectionManager {
|
||||
}
|
Loading…
Reference in a new issue