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;
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;
}
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() {
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.
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;
}
*/
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.
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;
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.
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;
}
}
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);
}
}
private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
- return context.isPresent() && !ownIdentity.hasContext(context.transform(extractContext).get());
+ return (context != null) && !ownIdentity.hasContext(context.getContext());
}
}
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;
* 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);
}
/**
* @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;
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 {
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;
};
}
- 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);
}
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;
.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 {
.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);
}
}
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")
))
}
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() {
@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"))
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
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())
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]