🎨 Use nullable type instead of optional
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 23 Sep 2019 19:36:43 +0000 (21:36 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 24 Sep 2019 06:06:53 +0000 (08:06 +0200)
src/main/java/net/pterodactylus/sone/core/SoneModificationDetector.java
src/main/java/net/pterodactylus/sone/data/impl/AlbumImpl.java
src/main/java/net/pterodactylus/sone/data/impl/ImageImpl.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java
src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java
src/test/java/net/pterodactylus/sone/test/Matchers.java
src/test/kotlin/net/pterodactylus/sone/core/ConfigurationSoneParserTest.kt
src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt

index 92fa3dc..e5e20d9 100644 (file)
@@ -24,7 +24,7 @@ class SoneModificationDetector {
        private final Ticker ticker;
        private final LockableFingerprintProvider lockableFingerprintProvider;
        private final AtomicInteger insertionDelay;
-       private Optional<Long> lastModificationTime;
+       private Long lastModificationTime;
        private String lastInsertFingerprint;
        private String lastCheckFingerprint;
 
@@ -42,18 +42,18 @@ class SoneModificationDetector {
 
        public boolean isEligibleForInsert() {
                if (lockableFingerprintProvider.isLocked()) {
-                       lastModificationTime = absent();
+                       lastModificationTime = null;
                        lastCheckFingerprint = "";
                        return false;
                }
                String fingerprint = lockableFingerprintProvider.getFingerprint();
                if (fingerprint.equals(lastInsertFingerprint)) {
-                       lastModificationTime = absent();
+                       lastModificationTime = null;
                        lastCheckFingerprint = fingerprint;
                        return false;
                }
                if (!Objects.equal(lastCheckFingerprint, fingerprint)) {
-                       lastModificationTime = of(ticker.read());
+                       lastModificationTime = ticker.read();
                        lastCheckFingerprint = fingerprint;
                        return false;
                }
@@ -67,11 +67,11 @@ class SoneModificationDetector {
        public void setFingerprint(String fingerprint) {
                lastInsertFingerprint = fingerprint;
                lastCheckFingerprint = lastInsertFingerprint;
-               lastModificationTime = absent();
+               lastModificationTime = null;
        }
 
        private boolean insertionDelayHasPassed() {
-               return NANOSECONDS.toSeconds(ticker.read() - lastModificationTime.get()) >= insertionDelay.get();
+               return NANOSECONDS.toSeconds(ticker.read() - lastModificationTime) >= insertionDelay.get();
        }
 
        public boolean isModified() {
index 3ec362f..a8768e1 100644 (file)
 
 package net.pterodactylus.sone.data.impl;
 
-import static com.google.common.base.Optional.absent;
-import static com.google.common.base.Optional.fromNullable;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-import static java.nio.charset.StandardCharsets.UTF_8;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-
-import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
-
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
-import com.google.common.hash.Hasher;
+import java.util.*;
+import javax.annotation.*;
+
+import com.google.common.base.*;
+import com.google.common.collect.*;
 import com.google.common.hash.Hashing;
+import com.google.common.hash.*;
+import net.pterodactylus.sone.data.*;
+
+import static com.google.common.base.Preconditions.*;
+import static java.nio.charset.StandardCharsets.*;
 
 /**
  * Container for images that can also contain nested {@link AlbumImpl}s.
@@ -258,32 +247,33 @@ public class AlbumImpl implements Album {
        public Modifier modify() throws IllegalStateException {
                // TODO: reenable check for local Sones
                return new Modifier() {
-                       private Optional<String> title = absent();
-
-                       private Optional<String> description = absent();
+                       @Nullable
+                       private String title;
+                       @Nullable
+                       private String description;
 
                        @Override
                        public Modifier setTitle(String title) {
-                               this.title = fromNullable(title);
+                               this.title = title;
                                return this;
                        }
 
                        @Override
                        public Modifier setDescription(String description) {
-                               this.description = fromNullable(description);
+                               this.description = description;
                                return this;
                        }
 
                        @Override
                        public Album update() throws IllegalStateException {
-                               if (title.isPresent() && title.get().trim().isEmpty()) {
+                               if (title != null && title.trim().isEmpty()) {
                                        throw new AlbumTitleMustNotBeEmpty();
                                }
-                               if (title.isPresent()) {
-                                       AlbumImpl.this.title = title.get();
+                               if (title != null) {
+                                       AlbumImpl.this.title = title;
                                }
-                               if (description.isPresent()) {
-                                       AlbumImpl.this.description = description.get();
+                               if (description != null) {
+                                       AlbumImpl.this.description = description;
                                }
                                return AlbumImpl.this;
                        }
index a54a8de..b357b6a 100644 (file)
  */
 package net.pterodactylus.sone.data.impl;
 
-import static com.google.common.base.Optional.absent;
-import static com.google.common.base.Optional.fromNullable;
-import static com.google.common.base.Optional.of;
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-import static java.nio.charset.StandardCharsets.UTF_8;
+import java.util.*;
+import javax.annotation.*;
 
-import java.util.UUID;
+import com.google.common.hash.*;
+import net.pterodactylus.sone.data.*;
 
-import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
-
-import com.google.common.base.Optional;
-import com.google.common.hash.Hasher;
-import com.google.common.hash.Hashing;
+import static com.google.common.base.Preconditions.*;
+import static java.nio.charset.StandardCharsets.*;
 
 /**
  * Container for image metadata.
@@ -146,93 +138,94 @@ public class ImageImpl implements Image {
        public Modifier modify() throws IllegalStateException {
                // TODO: reenable check for local images
                return new Modifier() {
-                       private Optional<Sone> sone = absent();
-
-                       private Optional<Long> creationTime = absent();
-
-                       private Optional<String> key = absent();
-
-                       private Optional<String> title = absent();
-
-                       private Optional<String> description = absent();
-
-                       private Optional<Integer> width = absent();
-
-                       private Optional<Integer> height = absent();
+                       @Nullable
+                       private Sone sone;
+                       @Nullable
+                       private Long creationTime;
+                       @Nullable
+                       private String key;
+                       @Nullable
+                       private String title;
+                       @Nullable
+                       private String description;
+                       @Nullable
+                       private Integer width;
+                       @Nullable
+                       private Integer height;
 
                        @Override
                        public Modifier setSone(Sone sone) {
-                               this.sone = fromNullable(sone);
+                               this.sone = sone;
                                return this;
                        }
 
                        @Override
                        public Modifier setCreationTime(long creationTime) {
-                               this.creationTime = of(creationTime);
+                               this.creationTime = creationTime;
                                return this;
                        }
 
                        @Override
                        public Modifier setKey(String key) {
-                               this.key = fromNullable(key);
+                               this.key = key;
                                return this;
                        }
 
                        @Override
                        public Modifier setTitle(String title) {
-                               this.title = fromNullable(title);
+                               this.title = title;
                                return this;
                        }
 
                        @Override
                        public Modifier setDescription(String description) {
-                               this.description = fromNullable(description);
+                               this.description = description;
                                return this;
                        }
 
                        @Override
                        public Modifier setWidth(int width) {
-                               this.width = of(width);
+                               this.width = width;
                                return this;
                        }
 
                        @Override
                        public Modifier setHeight(int height) {
-                               this.height = of(height);
+                               this.height = height;
                                return this;
                        }
 
                        @Override
                        public Image update() throws IllegalStateException {
-                               checkState(!sone.isPresent() || (ImageImpl.this.sone == null) || sone.get().equals(ImageImpl.this.sone), "can not change Sone once set");
-                               checkState(!creationTime.isPresent() || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime.get())), "can not change creation time once set");
-                               checkState(!key.isPresent() || (ImageImpl.this.key == null) || key.get().equals(ImageImpl.this.key), "can not change key once set");
-                               if (title.isPresent() && title.get().trim().isEmpty()) {
+                               checkState(sone == null || (ImageImpl.this.sone == null) || sone.equals(ImageImpl.this.sone), "can not change Sone once set");
+                               checkState(creationTime == null || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime)), "can not change creation time once set");
+                               checkState(key == null || (ImageImpl.this.key == null) || key.equals(ImageImpl.this.key), "can not change key once set");
+                               if (title != null && title.trim().isEmpty()) {
                                        throw new ImageTitleMustNotBeEmpty();
                                }
-                               checkState(!width.isPresent() || (ImageImpl.this.width == 0) || width.get().equals(ImageImpl.this.width), "can not change width once set");
-                               checkState(!height.isPresent() || (ImageImpl.this.height == 0) || height.get().equals(ImageImpl.this.height), "can not change height once set");
+                               checkState(width == null || (ImageImpl.this.width == 0) || width.equals(ImageImpl.this.width), "can not change width once set");
+                               checkState(height == null || (ImageImpl.this.height == 0) || height.equals(ImageImpl.this.height), "can not change height once set");
 
-                               if (sone.isPresent()) {
-                                       ImageImpl.this.sone = sone.get();
+                               if (sone != null) {
+                                       ImageImpl.this.sone = sone;
                                }
-                               if (creationTime.isPresent()) {
-                                       ImageImpl.this.creationTime = creationTime.get();
+                               if (creationTime != null) {
+                                       ImageImpl.this.creationTime = creationTime;
                                }
-                               if (key.isPresent()) {
-                                       ImageImpl.this.key = key.get();
+                               if (key != null) {
+                                       ImageImpl.this.key = key;
                                }
-                               if (title.isPresent()) {
-                                       ImageImpl.this.title = title.get();
+                               if (title != null) {
+                                       ImageImpl.this.title = title;
                                }
-                               if (description.isPresent()) {
-                                       ImageImpl.this.description = description.get();
+                               if (description != null) {
+                                       ImageImpl.this.description = description;
                                }
-                               if (width.isPresent()) {
-                                       ImageImpl.this.width = width.get();
+                               if (width != null) {
+                                       ImageImpl.this.width = width;
                                }
-                               if (height.isPresent()) {
-                                       ImageImpl.this.height = height.get();
+                               if (height != null) {
+                                       ImageImpl.this.height = height;
                                }
 
                                return ImageImpl.this;
index 0917130..b446e71 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot;
 
-import static java.util.concurrent.TimeUnit.*;
-import static net.pterodactylus.sone.freenet.wot.Context.*;
-
 import java.util.*;
 import java.util.logging.*;
+import javax.annotation.*;
 
-import net.pterodactylus.sone.freenet.plugin.*;
-
-import com.google.common.base.Optional;
 import com.google.common.base.*;
 import com.google.inject.*;
+import net.pterodactylus.sone.freenet.plugin.*;
+
+import static java.util.concurrent.TimeUnit.*;
 
 /**
  * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
@@ -36,14 +34,15 @@ public class IdentityLoader {
 
        private final Logger logger = Logger.getLogger(IdentityLoader.class.getName());
        private final WebOfTrustConnector webOfTrustConnector;
-       private final Optional<Context> context;
+       @Nullable
+       private final Context context;
 
        public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
-               this(webOfTrustConnector, Optional.<Context>absent());
+               this(webOfTrustConnector, null);
        }
 
        @Inject
-       public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<Context> context) {
+       public IdentityLoader(WebOfTrustConnector webOfTrustConnector, @Nullable Context context) {
                this.webOfTrustConnector = webOfTrustConnector;
                this.context = context;
        }
@@ -65,7 +64,7 @@ public class IdentityLoader {
                        }
 
                        Stopwatch stopwatch = Stopwatch.createStarted();
-                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext));
+                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, (context == null) ? null : context.getContext());
                        logger.fine("Loaded " + trustedIdentities.size() + " identities for " + ownIdentity.getNickname() + " in " + (stopwatch.elapsed(MILLISECONDS) / 1000.0) + "s.");
                        currentIdentities.put(ownIdentity, trustedIdentities);
                }
@@ -74,7 +73,7 @@ public class IdentityLoader {
        }
 
        private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
-               return context.isPresent() && !ownIdentity.hasContext(context.transform(extractContext).get());
+               return (context != null) && !ownIdentity.hasContext(context.getContext());
        }
 
 }
index c1dbef9..a67ba82 100644 (file)
@@ -28,6 +28,8 @@ import java.util.concurrent.atomic.AtomicLong;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.annotation.*;
+
 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
 import net.pterodactylus.sone.freenet.plugin.PluginException;
 import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
@@ -124,7 +126,7 @@ public class WebOfTrustConnector {
         *             if an error occured talking to the Web of Trust plugin
         */
        public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity) throws PluginException {
-               return loadTrustedIdentities(ownIdentity, Optional.<String>absent());
+               return loadTrustedIdentities(ownIdentity, null);
        }
 
        /**
@@ -139,8 +141,8 @@ public class WebOfTrustConnector {
         * @throws PluginException
         *             if an error occured talking to the Web of Trust plugin
         */
-       public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, Optional<String> context) throws PluginException {
-               Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", context.or("")).put("WantTrustValues", "true").get());
+       public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, @Nullable String context) throws PluginException {
+               Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context ==null) ? "" : context).put("WantTrustValues", "true").get());
                SimpleFieldSet fields = reply.getFields();
                Set<Identity> identities = new HashSet<>();
                int identityCounter = -1;
index 45e95db..62f91b8 100644 (file)
@@ -19,7 +19,7 @@ public class IdentityManagerTest {
 
        private final EventBus eventBus = mock(EventBus.class);
        private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
-       private final IdentityManager identityManager = new IdentityManagerImpl(eventBus, webOfTrustConnector, new IdentityLoader(webOfTrustConnector, of(new Context("Test"))));
+       private final IdentityManager identityManager = new IdentityManagerImpl(eventBus, webOfTrustConnector, new IdentityLoader(webOfTrustConnector, new Context("Test")));
 
        @Test
        public void identityManagerPingsWotConnector() throws PluginException {
index d04da0f..5d2d641 100644 (file)
@@ -22,6 +22,8 @@ import static java.util.regex.Pattern.compile;
 import java.io.IOException;
 import java.io.InputStream;
 
+import javax.annotation.*;
+
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
@@ -91,8 +93,7 @@ public class Matchers {
                };
        }
 
-       public static Matcher<Post> isPost(String postId, long time,
-                       String text, Optional<String> recipient) {
+       public static Matcher<Post> isPost(String postId, long time, String text, @Nullable String recipient) {
                return new PostMatcher(postId, time, text, recipient);
        }
 
@@ -229,10 +230,10 @@ public class Matchers {
                private final String postId;
                private final long time;
                private final String text;
-               private final Optional<String> recipient;
+               @Nullable
+               private final String recipient;
 
-               private PostMatcher(String postId, long time, String text,
-                               Optional<String> recipient) {
+               private PostMatcher(String postId, long time, String text, @Nullable String recipient) {
                        this.postId = postId;
                        this.time = time;
                        this.text = text;
@@ -257,15 +258,15 @@ public class Matchers {
                                                .appendValue(text);
                                return false;
                        }
-                       if (recipient.isPresent()) {
+                       if (recipient != null) {
                                if (!post.getRecipientId().isPresent()) {
                                        mismatchDescription.appendText(
                                                        "Recipient not present");
                                        return false;
                                }
-                               if (!post.getRecipientId().get().equals(recipient.get())) {
+                               if (!post.getRecipientId().get().equals(recipient)) {
                                        mismatchDescription.appendText("Recipient is not ")
-                                                       .appendValue(recipient.get());
+                                                       .appendValue(recipient);
                                        return false;
                                }
                        } else {
@@ -283,9 +284,9 @@ public class Matchers {
                                        .appendValue(postId);
                        description.appendText(", created at @").appendValue(time);
                        description.appendText(", text ").appendValue(text);
-                       if (recipient.isPresent()) {
+                       if (recipient != null) {
                                description.appendText(", directed at ")
-                                               .appendValue(recipient.get());
+                                               .appendValue(recipient);
                        }
                }
 
index 6f93cd4..90e3fa6 100644 (file)
@@ -95,8 +95,8 @@ class ConfigurationSoneParserTest {
                val postBuilderFactory = createPostBuilderFactory()
                val posts = configurationSoneParser.parsePosts(postBuilderFactory)
                assertThat(posts, containsInAnyOrder(
-                               isPost("P0", 1000L, "T0", absent()),
-                               isPost("P1", 1001L, "T1", of("1234567890123456789012345678901234567890123"))
+                               isPost("P0", 1000L, "T0", null),
+                               isPost("P1", 1001L, "T1", "1234567890123456789012345678901234567890123")
                ))
        }
 
@@ -149,7 +149,7 @@ class ConfigurationSoneParserTest {
        fun postWithInvalidRecipientIdIsRecognized() {
                setupPostWithInvalidRecipientId()
                val posts = configurationSoneParser.parsePosts(createPostBuilderFactory())
-               assertThat(posts, contains(isPost("P0", 1000L, "T0", absent())))
+               assertThat(posts, contains(isPost("P0", 1000L, "T0", null)))
        }
 
        private fun setupPostWithInvalidRecipientId() {
index 46796ab..5d4fe3f 100644 (file)
@@ -50,8 +50,8 @@ class MemoryDatabaseTest {
        @Test
        fun `stored sone is made available`() {
                storeSone()
-               assertThat(memoryDatabase.getPost("post1"), isPost("post1", 1000L, "post1", absent()))
-               assertThat(memoryDatabase.getPost("post2"), isPost("post2", 2000L, "post2", of(RECIPIENT_ID)))
+               assertThat(memoryDatabase.getPost("post1"), isPost("post1", 1000L, "post1", null))
+               assertThat(memoryDatabase.getPost("post2"), isPost("post2", 2000L, "post2", RECIPIENT_ID))
                assertThat(memoryDatabase.getPost("post3"), nullValue())
                assertThat(memoryDatabase.getPostReply("reply1"), isPostReply("reply1", "post1", 3000L, "reply1"))
                assertThat(memoryDatabase.getPostReply("reply2"), isPostReply("reply2", "post2", 4000L, "reply2"))
index 5c363fc..07126bb 100644 (file)
@@ -33,7 +33,7 @@ import org.mockito.Mockito.verify
 class IdentityLoaderTest {
 
        private val webOfTrustConnector = mock<WebOfTrustConnector>()
-       private val identityLoader = IdentityLoader(webOfTrustConnector, of(Context("Test")))
+       private val identityLoader = IdentityLoader(webOfTrustConnector, Context("Test"))
        private val identityLoaderWithoutContext = IdentityLoader(webOfTrustConnector)
 
        @Before
@@ -84,10 +84,10 @@ class IdentityLoaderTest {
                val ownIdentities = createOwnIdentities()
                val identities = identityLoader.loadIdentities()
                verify(webOfTrustConnector).loadAllOwnIdentities()
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[0]), eq(of("Test")))
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[1]), eq(of("Test")))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[0]), eq("Test"))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[1]), eq("Test"))
                verify(webOfTrustConnector, never()).loadTrustedIdentities(eq(ownIdentities[2]), any())
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[3]), eq(of("Test")))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[3]), eq("Test"))
                assertThat(identities.keys, hasSize(4))
                assertThat(identities.keys, containsInAnyOrder(ownIdentities[0], ownIdentities[1], ownIdentities[2], ownIdentities[3]))
                verifyIdentitiesForOwnIdentity(identities, ownIdentities[0], createTrustedIdentitiesForFirstOwnIdentity())
@@ -101,10 +101,10 @@ class IdentityLoaderTest {
                val ownIdentities = createOwnIdentities()
                val identities = identityLoaderWithoutContext.loadIdentities()
                verify(webOfTrustConnector).loadAllOwnIdentities()
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[0]), eq(absent()))
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[1]), eq(absent()))
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[2]), eq(absent()))
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[3]), eq(absent()))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[0]), eq(null))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[1]), eq(null))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[2]), eq(null))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[3]), eq(null))
                assertThat(identities.keys, hasSize(4))
                val firstOwnIdentity = ownIdentities[0]
                val secondOwnIdentity = ownIdentities[1]