From: David ‘Bombe’ Roden Date: Sun, 15 Oct 2017 00:42:23 +0000 (+0200) Subject: Replace Sone provider interface with Kotlin version X-Git-Tag: 0.9.8^2~27 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8e313509a42a8c638fcac018dd73dd975bf9cb68;p=Sone.git Replace Sone provider interface with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 66677de..a2a400b 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -99,7 +99,6 @@ import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.thread.NamedThreadFactory; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; import com.google.common.collect.HashMultimap; @@ -109,6 +108,7 @@ import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; import com.google.inject.Singleton; +import kotlin.jvm.functions.Function1; /** * The Sone core. @@ -323,9 +323,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return database.getSones(); } + @Nonnull @Override - public Function> soneLoader() { - return database.soneLoader(); + public Function1 getSoneLoader() { + return database.getSoneLoader(); } /** @@ -338,7 +339,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * Sone */ @Override - public Optional getSone(String id) { + @Nullable + public Sone getSone(@Nonnull String id) { return database.getSone(id); } @@ -358,9 +360,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @return The Sone with the given ID, or {@code null} */ public Sone getLocalSone(String id) { - Optional sone = database.getSone(id); - if (sone.isPresent() && sone.get().isLocal()) { - return sone.get(); + Sone sone = database.getSone(id); + if ((sone != null) && sone.isLocal()) { + return sone; } return null; } @@ -382,7 +384,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @return The Sone with the given ID */ public Sone getRemoteSone(String id) { - return database.getSone(id).orNull(); + return database.getSone(id); } /** @@ -697,12 +699,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0"); long latestEdition = fromNullable(tryParse(property)).or(0L); - Optional existingSone = getSone(identity.getId()); - if (existingSone.isPresent() && existingSone.get().isLocal()) { - return existingSone.get(); + Sone existingSone = getSone(identity.getId()); + if ((existingSone != null )&& existingSone.isLocal()) { + return existingSone; } - boolean newSone = !existingSone.isPresent(); - Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build(); + boolean newSone = existingSone == null; + Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build(); sone.setLatestEdition(latestEdition); if (newSone) { synchronized (knownSones) { @@ -740,16 +742,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, if (!soneFollowingTimes.containsKey(soneId)) { long now = System.currentTimeMillis(); soneFollowingTimes.put(soneId, now); - Optional followedSone = getSone(soneId); - if (!followedSone.isPresent()) { + Sone followedSone = getSone(soneId); + if (followedSone == null) { return; } - for (Post post : followedSone.get().getPosts()) { + for (Post post : followedSone.getPosts()) { if (post.getTime() < now) { markPostKnown(post); } } - for (PostReply reply : followedSone.get().getReplies()) { + for (PostReply reply : followedSone.getReplies()) { if (reply.getTime() < now) { markReplyKnown(reply); } @@ -874,20 +876,20 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * of the age of the given Sone */ public void updateSone(final Sone sone, boolean soneRescueMode) { - Optional storedSone = getSone(sone.getId()); - if (storedSone.isPresent()) { - if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) { + Sone storedSone = getSone(sone.getId()); + if (storedSone != null) { + if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) { logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone)); return; } List events = - collectEventsForChangesInSone(storedSone.get(), sone); + collectEventsForChangesInSone(storedSone, sone); database.storeSone(sone); for (Object event : events) { eventBus.post(event); } - sone.setOptions(storedSone.get().getOptions()); - sone.setKnown(storedSone.get().isKnown()); + sone.setOptions(storedSone.getOptions()); + sone.setKnown(storedSone.isKnown()); sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); if (sone.isLocal()) { touchConfiguration(); @@ -1733,19 +1735,19 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return; } } - Optional sone = getSone(identity.getId()); - if (!sone.isPresent()) { + Sone sone = getSone(identity.getId()); + if (sone == null) { /* TODO - we don’t have the Sone anymore. should this happen? */ return; } - for (PostReply postReply : sone.get().getReplies()) { + for (PostReply postReply : sone.getReplies()) { eventBus.post(new PostReplyRemovedEvent(postReply)); } - for (Post post : sone.get().getPosts()) { + for (Post post : sone.getPosts()) { eventBus.post(new PostRemovedEvent(post)); } - eventBus.post(new SoneRemovedEvent(sone.get())); - database.removeSone(sone.get()); + eventBus.post(new SoneRemovedEvent(sone)); + database.removeSone(sone); } /** diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 26135ea..3c32f7f 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -59,7 +59,6 @@ import net.pterodactylus.util.template.XmlFilter; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Charsets; -import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; import com.google.common.eventbus.EventBus; @@ -125,20 +124,20 @@ public class SoneInserter extends AbstractService { this(core, eventBus, freenetInterface, soneId, new SoneModificationDetector(new LockableFingerprintProvider() { @Override public boolean isLocked() { - final Optional sone = core.getSone(soneId); - if (!sone.isPresent()) { + Sone sone = core.getSone(soneId); + if (sone == null) { return false; } - return core.isLocked(sone.get()); + return core.isLocked(sone); } @Override public String getFingerprint() { - final Optional sone = core.getSone(soneId); - if (!sone.isPresent()) { + Sone sone = core.getSone(soneId); + if (sone == null) { return null; } - return sone.get().getFingerprint(); + return sone.getFingerprint(); } }, insertionDelay), 1000); } @@ -219,12 +218,11 @@ public class SoneInserter extends AbstractService { sleep(delay); if (soneModificationDetector.isEligibleForInsert()) { - Optional soneOptional = core.getSone(soneId); - if (!soneOptional.isPresent()) { + Sone sone = core.getSone(soneId); + if (sone == null) { logger.log(Level.WARNING, format("Sone %s has disappeared, exiting inserter.", soneId)); return; } - Sone sone = soneOptional.get(); InsertInformation insertInformation = new InsertInformation(sone); logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName())); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java index 86f4098..2b785f9 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.data.impl; +import static com.google.common.base.Optional.fromNullable; + import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.SoneProvider; @@ -99,7 +101,7 @@ public class PostImpl implements Post { */ @Override public Sone getSone() { - return soneProvider.getSone(soneId).get(); + return soneProvider.getSone(soneId); } /** @@ -107,7 +109,7 @@ public class PostImpl implements Post { */ @Override public Optional getRecipientId() { - return Optional.fromNullable(recipientId); + return fromNullable(recipientId); } /** @@ -115,7 +117,7 @@ public class PostImpl implements Post { */ @Override public Optional getRecipient() { - return soneProvider.getSone(recipientId); + return fromNullable(soneProvider.getSone(recipientId)); } /** diff --git a/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java index 4105749..372c80a 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java @@ -83,7 +83,7 @@ public abstract class ReplyImpl> implements Reply { */ @Override public Sone getSone() { - return soneProvider.getSone(soneId).get(); + return soneProvider.getSone(soneId); } /** diff --git a/src/main/java/net/pterodactylus/sone/database/SoneProvider.java b/src/main/java/net/pterodactylus/sone/database/SoneProvider.java deleted file mode 100644 index 69f7eaf..0000000 --- a/src/main/java/net/pterodactylus/sone/database/SoneProvider.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Sone - SoneProvider.java - Copyright © 2011–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.database; - -import java.util.Collection; - -import javax.annotation.Nonnull; - -import net.pterodactylus.sone.core.Core; -import net.pterodactylus.sone.data.Sone; - -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.inject.ImplementedBy; - -/** - * Interface for objects that can provide {@link Sone}s by their ID. - * - * @author David ‘Bombe’ Roden - */ -@ImplementedBy(Core.class) -public interface SoneProvider { - - Function> soneLoader(); - - /** - * Returns the Sone with the given ID, or {@link Optional#absent()} if it - * does not exist. - * - * @param soneId - * The ID of the Sone to return - * @return The Sone with the given ID, or {@link Optional#absent()} - */ - public Optional getSone(String soneId); - - /** - * Returns all Sones. - * - * @return All Sones - */ - @Nonnull - public Collection getSones(); - - /** - * Returns all local Sones. - * - * @return All local Sones - */ - public Collection getLocalSones(); - - /** - * Returns all remote Sones. - * - * @return All remote Sones - */ - public Collection getRemoteSones(); - -} diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index aa4a2a8..6f9d246 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -37,6 +37,8 @@ import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import javax.annotation.Nonnull; + import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; @@ -56,7 +58,6 @@ import net.pterodactylus.sone.database.SoneProvider; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; -import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.HashMultimap; @@ -66,6 +67,7 @@ import com.google.common.collect.TreeMultimap; import com.google.common.util.concurrent.AbstractService; import com.google.inject.Inject; import com.google.inject.Singleton; +import kotlin.jvm.functions.Function1; /** * Memory-based {@link PostDatabase} implementation. @@ -241,21 +243,22 @@ public class MemoryDatabase extends AbstractService implements Database { } } + @Nonnull @Override - public Function> soneLoader() { - return new Function>() { + public Function1 getSoneLoader() { + return new Function1() { @Override - public Optional apply(String soneId) { + public Sone invoke(String soneId) { return getSone(soneId); } }; } @Override - public Optional getSone(String soneId) { + public Sone getSone(String soneId) { lock.readLock().lock(); try { - return fromNullable(allSones.get(soneId)); + return allSones.get(soneId); } finally { lock.readLock().unlock(); } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java index acf2374..724c627 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.database.memory; +import static com.google.common.base.Optional.fromNullable; + import java.util.UUID; import net.pterodactylus.sone.data.Post; @@ -104,7 +106,7 @@ class MemoryPost implements Post { */ @Override public Sone getSone() { - return soneProvider.getSone(soneId).get(); + return soneProvider.getSone(soneId); } /** @@ -112,7 +114,7 @@ class MemoryPost implements Post { */ @Override public Optional getRecipientId() { - return Optional.fromNullable(recipientId); + return fromNullable(recipientId); } /** @@ -120,7 +122,7 @@ class MemoryPost implements Post { */ @Override public Optional getRecipient() { - return soneProvider.getSone(recipientId); + return fromNullable(soneProvider.getSone(recipientId)); } /** diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReply.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReply.java index 714b84e..2fc8a88 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReply.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReply.java @@ -97,7 +97,7 @@ class MemoryPostReply implements PostReply { */ @Override public Sone getSone() { - return soneProvider.getSone(soneId).get(); + return soneProvider.getSone(soneId); } /** diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 98a43ab..93b5d32 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -161,11 +161,11 @@ public abstract class AbstractSoneCommand extends AbstractCommand { if (mandatory && (soneId == null)) { throw new FcpException("Could not load Sone ID from “" + parameterName + "”."); } - Optional sone = core.getSone(soneId); - if ((mandatory && !sone.isPresent()) || (sone.isPresent() && localOnly && !sone.get().isLocal())) { + Sone sone = core.getSone(soneId); + if ((mandatory && (sone == null)) || ((sone != null) && localOnly && !sone.isLocal())) { throw new FcpException("Could not load Sone from “" + soneId + "”."); } - return sone; + return Optional.fromNullable(sone); } /** diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java index 7b804fb..4ee15e3 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java @@ -28,7 +28,6 @@ import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.fcp.FcpException; -import com.google.common.base.Optional; import com.google.common.collect.Collections2; import freenet.support.SimpleFieldSet; @@ -63,11 +62,11 @@ public class GetPostFeedCommand extends AbstractSoneCommand { Collection allPosts = new HashSet(); allPosts.addAll(sone.getPosts()); for (String friendSoneId : sone.getFriends()) { - Optional friendSone = getCore().getSone(friendSoneId); - if (!friendSone.isPresent()) { + Sone friendSone = getCore().getSone(friendSoneId); + if (friendSone == null) { continue; } - allPosts.addAll(friendSone.get().getPosts()); + allPosts.addAll(friendSone.getPosts()); } allPosts.addAll(getCore().getDirectedPosts(sone.getId())); allPosts = Collections2.filter(allPosts, Post.FUTURE_POSTS_FILTER); diff --git a/src/main/kotlin/net/pterodactylus/sone/database/SoneProvider.kt b/src/main/kotlin/net/pterodactylus/sone/database/SoneProvider.kt new file mode 100644 index 0000000..b8ce5e1 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/database/SoneProvider.kt @@ -0,0 +1,40 @@ +/* + * Sone - SoneProvider.java - Copyright © 2011–2016 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.database + +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Sone + +import com.google.common.base.Function +import com.google.common.base.Optional +import com.google.inject.ImplementedBy + +/** + * Interface for objects that can provide [Sone]s by their ID. + */ +@ImplementedBy(Core::class) +interface SoneProvider { + + val sones: Collection + val localSones: Collection + val remoteSones: Collection + val soneLoader: (String) -> Sone? + + fun getSone(soneId: String): Sone? + +} diff --git a/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt index cec5d56..67b03f3 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt @@ -20,7 +20,7 @@ class ParserFilter @Inject constructor(private val core: Core, private val soneT val text = data?.toString() ?: return listOf() val soneParameter = parameters?.get("sone") val sone = when (soneParameter) { - is String -> core.getSone(soneParameter).orNull() + is String -> core.getSone(soneParameter) is Sone -> soneParameter else -> null } diff --git a/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt index 43331c3..25df239 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt @@ -101,7 +101,7 @@ class RenderFilter(private val core: Core, private val templateContextFactory: T private fun render(writer: Writer, freemailPart: FreemailPart) { val sone = core.getSone(freemailPart.identityId) - val soneName = sone.transform(SoneAccessor::getNiceName).or(freemailPart.identityId) + val soneName = sone?.let(SoneAccessor::getNiceName) ?: freemailPart.identityId renderLink(writer, "/Freemail/NewMessage?to=${freemailPart.identityId}", "${freemailPart.emailLocalPart}@$soneName.freemail", diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt index 68a38ef..64125c1 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt @@ -72,7 +72,7 @@ class SoneTextParser(private val soneProvider: SoneProvider?, private val postPr } catch (e: MalformedURLException) { PlainTextPart(link) } - SONE -> link.substring(7).let { SonePart(soneProvider?.getSone(it)?.orNull() ?: IdOnlySone(it)) } + SONE -> link.substring(7).let { SonePart(soneProvider?.getSone(it) ?: IdOnlySone(it)) } POST -> postProvider?.getPost(link.substring(7))?.let { PostPart(it) } ?: PlainTextPart(link) FREEMAIL -> link.indexOf('@').let { atSign -> link.substring(atSign + 1, link.length - 9).let { freemailId -> diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt index ec65b55..868e990 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt @@ -2,6 +2,7 @@ package net.pterodactylus.sone.web.ajax import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.text.TextFilter +import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.utils.emptyToNull import net.pterodactylus.sone.utils.headers import net.pterodactylus.sone.utils.let @@ -18,13 +19,13 @@ class CreatePostAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("createP request.parameters["text"].emptyToNull ?.let { TextFilter.filter(request.headers["Host"], it) } ?.let { text -> - val sender = request.parameters["sender"].emptyToNull?.let(core::getSone)?.orNull() ?: currentSone - val recipient = request.parameters["recipient"].let(core::getSone) - core.createPost(sender, recipient, text).let { post -> + val sender = request.parameters["sender"].emptyToNull?.let(core::getSone) ?: currentSone + val recipient = request.parameters["recipient"]?.let(core::getSone) + core.createPost(sender, recipient.asOptional(), text).let { post -> createSuccessJsonObject().apply { put("postId", post.id) put("sone", sender.id) - put("recipient", recipient.let(Sone::getId)) + put("recipient", recipient?.id) } } } ?: createErrorJsonObject("text-required") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt index e4f8435..4eadfde 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.kt @@ -16,7 +16,7 @@ class DistrustAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("distrustS override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = request.parameters["sone"] - .let(core::getSone) + ?.let(core::getSone) ?.let { sone -> createSuccessJsonObject() .put("trustValue", core.preferences.negativeTrust) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.kt index c6aa25e..0c37ac0 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.kt @@ -14,7 +14,7 @@ class FollowSoneAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("followS override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = request.parameters["sone"] - .let(core::getSone) + ?.let(core::getSone) ?.also { core.followSone(currentSone, it.id) } ?.also(core::markSoneKnown) ?.let { createSuccessJsonObject() } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt index acb8cec..7646704 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt @@ -36,7 +36,7 @@ class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: E this["loggedIn"] = currentSone != null this["options"] = currentSone?.options?.toJsonOptions() ?: jsonObject {} this["notificationHash"] = webInterface.getNotifications(currentSone).sortedBy { it.createdTime }.hashCode() - this["sones"] = request.httpRequest.getParam("soneIds").split(',').mapPresent(core::getSone).plus(currentSone).filterNotNull().toJsonSones() + this["sones"] = request.httpRequest.getParam("soneIds").split(',').mapNotNull(core::getSone).plus(currentSone).filterNotNull().toJsonSones() this["newPosts"] = webInterface.getNewPosts(currentSone).toJsonPosts() this["newReplies"] = webInterface.getNewReplies(currentSone).toJsonReplies() this["linkedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements() diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.kt index 9e4e4a3..cca5cef 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.ajax import com.google.common.base.Optional +import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.utils.mapPresent import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -15,7 +16,7 @@ class MarkAsKnownAjaxPage(webInterface: WebInterface) : JsonPage("markAsKnown.aj override val requiresLogin = false override fun createJsonObject(request: FreenetRequest) = when (request.parameters["type"]) { - "sone" -> processIds(request, core::getSone, core::markSoneKnown) + "sone" -> processIds(request, { core.getSone(it).asOptional() }, core::markSoneKnown) "post" -> processIds(request, core::getPost, core::markPostKnown) "reply" -> processIds(request, core::getPostReply, core::markReplyKnown) else -> createErrorJsonObject("invalid-type") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPage.kt index 9a01af8..1dfb058 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPage.kt @@ -15,7 +15,7 @@ class TrustAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("trustSone.aj override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = request.parameters["sone"] - .let(core::getSone) + ?.let(core::getSone) ?.let { core.trustSone(currentSone, it) } ?.let { createSuccessJsonObject().put("trustValue", core.preferences.positiveTrust) } ?: createErrorJsonObject("invalid-sone-id") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.kt index 6889d04..83b474c 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.kt @@ -12,8 +12,8 @@ class UnfollowSoneAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("unfol override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = request.parameters["sone"] - ?.takeIf { core.getSone(it).isPresent } - ?.also { core.unfollowSone(currentSone, it) } + ?.let(core::getSone) + ?.also { core.unfollowSone(currentSone, it.id) } ?.let { createSuccessJsonObject() } ?: createErrorJsonObject("invalid-sone-id") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt index 30a1c31..6d0c634 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt @@ -1,6 +1,7 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.text.TextFilter +import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -24,7 +25,7 @@ class CreatePostPage(template: Template, webInterface: WebInterface): } val sender = webInterface.core.getLocalSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(freenetRequest.toadletContext) val recipient = webInterface.core.getSone(freenetRequest.httpRequest.getPartAsStringFailsafe("recipient", 43)) - webInterface.core.createPost(sender, recipient, TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), text)) + webInterface.core.createPost(sender, recipient.asOptional(), TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), text)) throw RedirectException(returnPage) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt index ce5aaec..82f6da1 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DistrustPage.kt @@ -17,8 +17,8 @@ class DistrustPage(template: Template, webInterface: WebInterface): override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { if (freenetRequest.isPOST) { - val sone = webInterface.core.getSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sone", 44)).orNull() - sone?.run { webInterface.core.distrustSone(getCurrentSone(freenetRequest.toadletContext), this) } + webInterface.core.getSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sone", 44)) + ?.run { webInterface.core.distrustSone(getCurrentSone(freenetRequest.toadletContext), this) } throw RedirectException(freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt index 781e07f..5069d16 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/FollowSonePage.kt @@ -16,8 +16,7 @@ class FollowSonePage(template: Template, webInterface: WebInterface): if (freenetRequest.isPOST) { freenetRequest.httpRequest.getPartAsStringFailsafe("sone", 1200).split(Regex("[ ,]+")) .map { it to webInterface.core.getSone(it) } - .filter { it.second.isPresent } - .map { it.first to it.second.get() } + .filterNot { it.second == null } .forEach { sone -> webInterface.core.followSone(freenetRequest.currentSone, sone.first) webInterface.core.markSoneKnown(sone.second) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt index 3181355..60a2bd5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt @@ -40,7 +40,7 @@ class ImageBrowserPage(template: Template, webInterface: WebInterface): } } else { templateContext["soneRequested"] = true - templateContext["sone"] = webInterface.core.getSone(freenetRequest.httpRequest.getParam("sone")).orNull() ?: getCurrentSone(freenetRequest.toadletContext) + templateContext["sone"] = webInterface.core.getSone(freenetRequest.httpRequest.getParam("sone")) ?: getCurrentSone(freenetRequest.toadletContext) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt index bf6e0c4..e5685ef 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt @@ -19,9 +19,7 @@ class IndexPage(template: Template, webInterface: WebInterface, private val post getCurrentSone(freenetRequest.toadletContext)!!.let { currentSone -> (currentSone.posts + currentSone.friends - .map { webInterface.core.getSone(it) } - .filter { it.isPresent } - .map { it.get() } + .mapNotNull(webInterface.core::getSone) .flatMap { it.posts } + webInterface.core.getDirectedPosts(currentSone.id) ).distinct() diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/MarkAsKnownPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/MarkAsKnownPage.kt index 570e570..bc96af5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/MarkAsKnownPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/MarkAsKnownPage.kt @@ -18,7 +18,7 @@ class MarkAsKnownPage(template: Template, webInterface: WebInterface): override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { val ids = freenetRequest.parameters["id", 65536]!!.split(" ") when (freenetRequest.parameters["type", 5]) { - "sone" -> ids.mapPresent(webInterface.core::getSone).forEach(webInterface.core::markSoneKnown) + "sone" -> ids.mapNotNull(webInterface.core::getSone).forEach(webInterface.core::markSoneKnown) "post" -> ids.mapPresent(webInterface.core::getPost).forEach(webInterface.core::markPostKnown) "reply" -> ids.mapPresent(webInterface.core::getPostReply).forEach(webInterface.core::markReplyKnown) else -> throw RedirectException("invalid.html") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt index aff6aaf..5f40238 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt @@ -42,7 +42,7 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web 0 -> redirect("index.html") 1 -> phrases.first().phrase.also { word -> when { - word.removePrefix("sone://").let(webInterface.core::getSone).isPresent -> redirect("viewSone.html?sone=${word.removePrefix("sone://")}") + word.removePrefix("sone://").let(webInterface.core::getSone) != null -> redirect("viewSone.html?sone=${word.removePrefix("sone://")}") word.removePrefix("post://").let(webInterface.core::getPost).isPresent -> redirect("viewPost.html?post=${word.removePrefix("post://")}") word.removePrefix("reply://").let(webInterface.core::getPostReply).isPresent -> redirect("viewPost.html?post=${word.removePrefix("reply://").let(webInterface.core::getPostReply).get().postId}") word.removePrefix("album://").let(webInterface.core::getAlbum) != null -> redirect("imageBrowser.html?album=${word.removePrefix("album://")}") diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt index bdc8952..db53ac3 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/TrustPage.kt @@ -1,7 +1,6 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.utils.isPOST -import net.pterodactylus.sone.utils.let import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -18,7 +17,7 @@ class TrustPage(template: Template, webInterface: WebInterface): override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { if (freenetRequest.isPOST) { getCurrentSone(freenetRequest.toadletContext)?.also { currentSone -> - webInterface.core.getSone(freenetRequest.parameters["sone"]).let { sone -> + webInterface.core.getSone(freenetRequest.parameters["sone"]!!)?.let { sone -> webInterface.core.trustSone(currentSone, sone) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt index a46b272..77fcdd5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/UntrustPage.kt @@ -1,6 +1,5 @@ package net.pterodactylus.sone.web.pages -import net.pterodactylus.sone.utils.also import net.pterodactylus.sone.utils.isPOST import net.pterodactylus.sone.utils.parameters import net.pterodactylus.sone.web.WebInterface @@ -18,9 +17,9 @@ class UntrustPage(template: Template, webInterface: WebInterface): override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { if (freenetRequest.isPOST) { getCurrentSone(freenetRequest.toadletContext)!!.also { currentSone -> - freenetRequest.parameters["sone", 44] + freenetRequest.parameters["sone", 44]!! .let(webInterface.core::getSone) - .also { webInterface.core.untrustSone(currentSone, it) } + ?.also { webInterface.core.untrustSone(currentSone, it) } } throw RedirectException(freenetRequest.parameters["returnPage", 256]) } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt index 90bc147..2e175af 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt @@ -3,7 +3,6 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.template.SoneAccessor -import net.pterodactylus.sone.utils.let import net.pterodactylus.sone.utils.mapPresent import net.pterodactylus.sone.utils.paginate import net.pterodactylus.sone.utils.parameters @@ -21,7 +20,7 @@ class ViewSonePage(template: Template, webInterface: WebInterface): override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { templateContext["soneId"] = freenetRequest.parameters["sone"] - freenetRequest.parameters["sone"].let(webInterface.core::getSone).let { sone -> + freenetRequest.parameters["sone"]!!.let(webInterface.core::getSone)?.let { sone -> templateContext["sone"] = sone val sonePosts = sone.posts val directedPosts = webInterface.core.getDirectedPosts(sone.id) @@ -51,7 +50,7 @@ class ViewSonePage(template: Template, webInterface: WebInterface): override fun isLinkExcepted(link: URI?) = true public override fun getPageTitle(freenetRequest: FreenetRequest): String = - freenetRequest.parameters["sone"].let(webInterface.core::getSone).let { sone -> + freenetRequest.parameters["sone"]!!.let(webInterface.core::getSone)?.let { sone -> "${SoneAccessor.getNiceName(sone)} - ${webInterface.l10n.getString("Page.ViewSone.Title")}" } ?: webInterface.l10n.getString("Page.ViewSone.Page.TitleWithoutSone") diff --git a/src/test/java/net/pterodactylus/sone/core/CoreTest.java b/src/test/java/net/pterodactylus/sone/core/CoreTest.java index cea8ed7..dbe70d5 100644 --- a/src/test/java/net/pterodactylus/sone/core/CoreTest.java +++ b/src/test/java/net/pterodactylus/sone/core/CoreTest.java @@ -22,7 +22,6 @@ import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; import net.pterodactylus.util.config.Configuration; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.eventbus.EventBus; @@ -74,7 +73,7 @@ public class CoreTest { Identity identity = mock(Identity.class); when(identity.getId()).thenReturn("sone-id"); Sone sone = mock(Sone.class); - when(database.getSone("sone-id")).thenReturn(Optional.of(sone)); + when(database.getSone("sone-id")).thenReturn(sone); PostReply postReply1 = mock(PostReply.class); PostReply postReply2 = mock(PostReply.class); when(sone.getReplies()).thenReturn(ImmutableSet.of(postReply1, postReply2)); diff --git a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java index 51947d1..5b94f08 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java @@ -1,6 +1,5 @@ package net.pterodactylus.sone.core; -import static com.google.common.base.Optional.of; import static com.google.common.io.ByteStreams.toByteArray; import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; import static java.lang.System.currentTimeMillis; @@ -62,7 +61,7 @@ public class SoneInserterTest { public void setupCore() { UpdateChecker updateChecker = mock(UpdateChecker.class); when(core.getUpdateChecker()).thenReturn(updateChecker); - when(core.getSone(anyString())).thenReturn(Optional.absent()); + when(core.getSone(anyString())).thenReturn(null); } @Test @@ -78,7 +77,7 @@ public class SoneInserterTest { when(sone.getInsertUri()).thenReturn(insertUri); when(sone.getFingerprint()).thenReturn(fingerprint); when(sone.getRootAlbum()).thenReturn(mock(Album.class)); - when(core.getSone(anyString())).thenReturn(of(sone)); + when(core.getSone(anyString())).thenReturn(sone); return sone; } @@ -223,7 +222,7 @@ public class SoneInserterTest { new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1); when(soneModificationDetector.isEligibleForInsert()).thenReturn(true); - when(core.getSone("SoneId")).thenReturn(Optional.absent()); + when(core.getSone("SoneId")).thenReturn(null); soneInserter.serviceRun(); } diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 940c157..b763cd1 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -26,14 +26,17 @@ import static org.hamcrest.Matchers.notNullValue; import java.io.IOException; import java.util.Collection; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.impl.IdOnlySone; import net.pterodactylus.sone.database.PostProvider; import net.pterodactylus.sone.database.SoneProvider; -import com.google.common.base.Function; import com.google.common.base.Optional; +import kotlin.jvm.functions.Function1; import org.junit.Test; /** @@ -410,22 +413,21 @@ public class SoneTextParserTest { */ private static class TestSoneProvider implements SoneProvider { + @Nonnull @Override - public Function> soneLoader() { - return new Function>() { + public Function1 getSoneLoader() { + return new Function1() { @Override - public Optional apply(String soneId) { + public Sone invoke(String soneId) { return getSone(soneId); } }; } - /** - * {@inheritDoc} - */ + @Nullable @Override - public Optional getSone(final String soneId) { - return Optional.of(new IdOnlySone(soneId)); + public Sone getSone(final String soneId) { + return new IdOnlySone(soneId); } /** @@ -457,8 +459,8 @@ public class SoneTextParserTest { private static class AbsentSoneProvider extends TestSoneProvider { @Override - public Optional getSone(String soneId) { - return Optional.absent(); + public Sone getSone(String soneId) { + return null; } } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt index 5fc84f0..2d77c6e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt @@ -1,6 +1,5 @@ package net.pterodactylus.sone.fcp -import com.google.common.base.Optional import com.google.common.base.Optional.absent import com.google.common.base.Optional.of import net.pterodactylus.sone.core.Core @@ -47,7 +46,7 @@ class CreatePostCommandTest : SoneCommandTest() { @Test fun `request without text results in fcp exception`() { parameters += "Sone" to "LocalSoneId" - whenever(core.getSone("LocalSoneId")).thenReturn(Optional.of(localSone)) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) executeCommandAndExpectFcpException() } @@ -55,7 +54,7 @@ class CreatePostCommandTest : SoneCommandTest() { fun `request with text creates post`() { parameters += "Sone" to "LocalSoneId" parameters += "Text" to "Test" - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) val post = mock().apply { whenever(id).thenReturn("PostId") } whenever(core.createPost(localSone, absent(), "Test")).thenReturn(post) val response = command.execute(parameters) @@ -68,7 +67,7 @@ class CreatePostCommandTest : SoneCommandTest() { parameters += "Sone" to "LocalSoneId" parameters += "Text" to "Test" parameters += "Recipient" to "InvalidSoneId" - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) executeCommandAndExpectFcpException() } @@ -77,7 +76,7 @@ class CreatePostCommandTest : SoneCommandTest() { parameters += "Sone" to "LocalSoneId" parameters += "Text" to "Test" parameters += "Recipient" to "LocalSoneId" - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) val response = command.execute(parameters) assertThat(response.replyParameters["Message"], equalTo("Error")) assertThat(response.replyParameters["ErrorMessage"], notNullValue()) @@ -88,8 +87,8 @@ class CreatePostCommandTest : SoneCommandTest() { parameters += "Sone" to "LocalSoneId" parameters += "Text" to "Test" parameters += "Recipient" to "RemoteSoneId" - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) - whenever(core.getSone("RemoteSoneId")).thenReturn(of(remoteSone)) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) val post = mock().apply { whenever(id).thenReturn("PostId") } whenever(core.createPost(localSone, of(remoteSone), "Test")).thenReturn(post) val response = command.execute(parameters) diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt index f464de4..5b4809c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt @@ -46,7 +46,7 @@ class CreateReplyCommandTest : SoneCommandTest() { private fun addValidLocalSoneParameter() { parameters += "Sone" to "LocalSoneId" - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt index 72daa07..d8ab243 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt @@ -3,7 +3,6 @@ package net.pterodactylus.sone.fcp import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.equalTo @@ -58,8 +57,8 @@ class GetPostFeedCommandTest : SoneCommandTest() { private fun setupAllPostsAndReplies() { parameters += "Sone" to "ValidSoneId" whenever(localSone.id).thenReturn("ValidSoneId") - whenever(core.getSone("ValidSoneId")).thenReturn(localSone.asOptional()) - whenever(core.getSone("Friend1")).thenReturn(friend1.asOptional()) + whenever(core.getSone("ValidSoneId")).thenReturn(localSone) + whenever(core.getSone("Friend1")).thenReturn(friend1) whenever(core.getLikes(post1)).thenReturn(setOf(sone3, sone4)) whenever(core.getLikes(post1Reply1)).thenReturn(setOf(sone2, sone3)) whenever(core.getLikes(post1Reply2)).thenReturn(setOf(sone3)) diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt index 6483085..acf1824 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt @@ -3,7 +3,6 @@ package net.pterodactylus.sone.fcp import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.equalTo @@ -49,8 +48,8 @@ class GetPostsCommandTest : SoneCommandTest() { whenever(core.getReplies("Post2")).thenReturn(listOf(post2Reply1, post2Reply2)) whenever(localSone.id).thenReturn("LocalSone") whenever(remoteSone.id).thenReturn("RemoteSone") - whenever(core.getSone("LocalSone")).thenReturn(localSone.asOptional()) - whenever(core.getSone("ValidSoneId")).thenReturn(remoteSone.asOptional()) + whenever(core.getSone("LocalSone")).thenReturn(localSone) + whenever(core.getSone("ValidSoneId")).thenReturn(remoteSone) whenever(remoteSone.posts).thenReturn(listOf(post2, post1)) parameters += "Sone" to "ValidSoneId" } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt index e73160b..79aad1a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt @@ -3,7 +3,6 @@ package net.pterodactylus.sone.fcp import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.freenet.fcp.FcpException import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.nullValue @@ -43,8 +42,7 @@ class GetSoneCommandTest : SoneCommandTest() { @Test fun `request with valid Sone parameter results in response with Sone information`() { - whenever(core.getSone("SoneId")).thenReturn(sone.asOptional()) - whenever(core.getSone(null)).thenReturn(null.asOptional()) + whenever(core.getSone("SoneId")).thenReturn(sone) parameters += "Sone" to "SoneId" val replyParameters = command.execute(parameters).replyParameters assertThat(replyParameters["Message"], equalTo("Sone")) @@ -54,8 +52,8 @@ class GetSoneCommandTest : SoneCommandTest() { @Test fun `request with local sone parameter results in followed being true for friend sone`() { - whenever(core.getSone("SoneId")).thenReturn(sone.asOptional()) - whenever(core.getSone("LocalSone")).thenReturn(localSone.asOptional()) + whenever(core.getSone("SoneId")).thenReturn(sone) + whenever(core.getSone("LocalSone")).thenReturn(localSone) whenever(localSone.id).thenReturn("LocalSone") whenever(localSone.hasFriend("SoneId")).thenReturn(true) parameters += "Sone" to "SoneId" @@ -68,8 +66,8 @@ class GetSoneCommandTest : SoneCommandTest() { @Test fun `request with local sone parameter results in followed being false for non-friend sone`() { - whenever(core.getSone("SoneId")).thenReturn(sone.asOptional()) - whenever(core.getSone("LocalSone")).thenReturn(localSone.asOptional()) + whenever(core.getSone("SoneId")).thenReturn(sone) + whenever(core.getSone("LocalSone")).thenReturn(localSone) whenever(localSone.id).thenReturn("LocalSone") parameters += "Sone" to "SoneId" parameters += "LocalSone" to "LocalSone" @@ -81,8 +79,8 @@ class GetSoneCommandTest : SoneCommandTest() { @Test fun `request with remote sone as local sone parameter results in fcp exception`() { - whenever(core.getSone("SoneId")).thenReturn(sone.asOptional()) - whenever(core.getSone("RemoteSone")).thenReturn(remoteSone.asOptional()) + whenever(core.getSone("SoneId")).thenReturn(sone) + whenever(core.getSone("RemoteSone")).thenReturn(remoteSone) whenever(localSone.id).thenReturn("RemoteSone") parameters += "Sone" to "SoneId" parameters += "LocalSone" to "RemoteSone" diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt index 54f784f..b344f51 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt @@ -24,8 +24,8 @@ class LikePostCommandTest : SoneCommandTest() { @Before fun setupPostAndSones() { whenever(core.getPost("PostId")).thenReturn(post.asOptional()) - whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone.asOptional()) - whenever(core.getSone("LocalSoneId")).thenReturn(localSone.asOptional()) + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt index 69ace0a..8705815 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt @@ -25,8 +25,8 @@ class LikeReplyCommandTest : SoneCommandTest() { @Before fun setupRepliesAndSones() { whenever(core.getPostReply("ReplyId")).thenReturn(reply.asOptional()) - whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone.asOptional()) - whenever(core.getSone("LocalSoneId")).thenReturn(localSone.asOptional()) + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt index 1caea96..284df69 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt @@ -2,7 +2,6 @@ package net.pterodactylus.sone.fcp import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Before @@ -18,8 +17,8 @@ class LockSoneCommandTest : SoneCommandTest() { @Before fun setupSones() { - whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone.asOptional()) - whenever(core.getSone("LocalSoneId")).thenReturn(localSone.asOptional()) + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) whenever(localSone.id).thenReturn("LocalSoneId") } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index aa38f88..6370acc 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -1,6 +1,5 @@ package net.pterodactylus.sone.fcp -import com.google.common.base.Optional import com.google.common.base.Optional.absent import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core @@ -39,7 +38,7 @@ abstract class SoneCommandTest { @Before fun setupCore() { - whenever(core.getSone(anyString())).thenReturn(absent()) + whenever(core.getSone(anyString())).thenReturn(null) whenever(core.getPost(anyString())).thenReturn(absent()) whenever(core.getPostReply(anyString())).thenReturn(absent()) } @@ -92,7 +91,7 @@ abstract class SoneCommandTest { fun requestWithValidRemoteSoneParameterResultsInFcpException() { parameters += "Sone" to "RemoteSoneId" - whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone)) + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) executeCommandAndExpectFcpException() } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt index c002db3..77e78b3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt @@ -2,7 +2,6 @@ package net.pterodactylus.sone.fcp import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Before @@ -18,8 +17,8 @@ class UnlockSoneCommandTest : SoneCommandTest() { @Before fun setupSones() { - whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone.asOptional()) - whenever(core.getSone("LocalSoneId")).thenReturn(localSone.asOptional()) + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) whenever(localSone.id).thenReturn("LocalSoneId") } diff --git a/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt index 00f3d04..0e44568 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt @@ -1,6 +1,5 @@ package net.pterodactylus.sone.template -import com.google.common.base.Optional.of import com.google.inject.Guice import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Sone @@ -40,7 +39,7 @@ class ParserFilterTest { private fun setupSone(identity: String): Sone { val sone = mock() `when`(sone.id).thenReturn(identity) - `when`(core.getSone(identity)).thenReturn(of(sone)) + `when`(core.getSone(identity)).thenReturn(sone) return sone } diff --git a/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt index b8d6259..0920280 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/RenderFilterTest.kt @@ -1,6 +1,5 @@ package net.pterodactylus.sone.template -import com.google.common.base.Optional import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.Profile @@ -101,7 +100,7 @@ class RenderFilterTest { `when`(sone.profile).thenReturn(Profile(sone)) `when`(sone.name).thenReturn(name) sone.profile.firstName = firstName - `when`(core.getSone(identity)).thenReturn(Optional.of(sone)) + `when`(core.getSone(identity)).thenReturn(sone) return sone } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt index 1cbba3d..96474ed 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt @@ -91,7 +91,7 @@ open class TestObjects { whenever(core.preferences).thenReturn(preferences) whenever(core.updateChecker).thenReturn(updateChecker) - whenever(core.getSone(ArgumentMatchers.anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() } + whenever(core.getSone(ArgumentMatchers.anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)] } whenever(core.getLocalSone(ArgumentMatchers.anyString())).thenAnswer { localSones[it[0]] } whenever(core.getPost(ArgumentMatchers.anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() } whenever(core.getLikes(ArgumentMatchers.any())).then { postLikes[it[0]] ?: emptySet() } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPageTest.kt index e4b53fd..b53cc8e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPageTest.kt @@ -1,6 +1,8 @@ package net.pterodactylus.sone.web.ajax +import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Test @@ -24,7 +26,7 @@ class UnfollowSoneAjaxPageTest : JsonPageTest("unfollowSone.ajax", pageSupplier @Test fun `request with valid sone unfollows sone`() { - addSone(mock(), "sone-id") + addSone(mock().apply { whenever(id).thenReturn("sone-id") }) addRequestParameter("sone", "sone-id") assertThatJsonIsSuccessful() verify(core).unfollowSone(currentSone, "sone-id") diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt index a1fc2de..1bf75c0 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt @@ -89,7 +89,7 @@ open class WebPageTest(pageSupplier: (Template, WebInterface) -> SoneTemplatePag whenever(core.preferences).thenReturn(preferences) whenever(core.identityManager.allOwnIdentities).then { ownIdentities } whenever(core.sones).then { allSones.values } - whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() } + whenever(core.getSone(anyString())).then { allSones[it[0]] } whenever(core.localSones).then { localSones.values } whenever(core.getLocalSone(anyString())).then { localSones[it[0]] } whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }