Merge branch 'feature/fix-coverity-defects' into next
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 20 Jul 2015 17:28:08 +0000 (19:28 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 20 Jul 2015 17:28:08 +0000 (19:28 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/impl/AlbumImpl.java
src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java
src/main/java/net/pterodactylus/sone/web/UploadImagePage.java

index e70f963..fe0907f 100644 (file)
@@ -687,8 +687,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.WARNING, "Given Identity is null!");
                        return null;
                }
-               final Long latestEdition = tryParse(fromNullable(
-                               identity.getProperty("Sone.LatestEdition")).or("0"));
+               String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+               long latestEdition = fromNullable(tryParse(property)).or(0L);
                Optional<Sone> existingSone = getSone(identity.getId());
                if (existingSone.isPresent() && existingSone.get().isLocal()) {
                        return existingSone.get();
@@ -1064,11 +1064,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
 
                /* load options. */
-               sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
-               sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
-               sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
-               sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
-               sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
+               sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
+               sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
+               sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
+               sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
+               sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
                sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
 
                /* if we’re still here, Sone was loaded successfully. */
index 04c5f39..e64a388 100644 (file)
@@ -171,17 +171,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
                }
        };
 
-       public static final Function<Sone, String> toSoneXmlUri =
-                       new Function<Sone, String>() {
-                               @Nonnull
-                               @Override
-                               public String apply(@Nullable Sone input) {
-                                       return input.getRequestUri()
-                                                       .setMetaString(new String[] { "sone.xml" })
-                                                       .toString();
-                               }
-                       };
-
        public static final Function<Sone, List<Album>> toAllAlbums = new Function<Sone, List<Album>>() {
                @Override
                public List<Album> apply(@Nullable Sone sone) {
index c569e0a..3488577 100644 (file)
@@ -134,7 +134,7 @@ public class AlbumImpl implements Album {
                checkArgument(equals(album.getParent()), "album must belong to this album");
                int oldIndex = albums.indexOf(album);
                if (oldIndex <= 0) {
-                       return null;
+                       return album;
                }
                albums.remove(oldIndex);
                albums.add(oldIndex - 1, album);
@@ -148,7 +148,7 @@ public class AlbumImpl implements Album {
                checkArgument(equals(album.getParent()), "album must belong to this album");
                int oldIndex = albums.indexOf(album);
                if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
-                       return null;
+                       return album;
                }
                albums.remove(oldIndex);
                albums.add(oldIndex + 1, album);
@@ -209,7 +209,7 @@ public class AlbumImpl implements Album {
                checkArgument(image.getAlbum().equals(this), "image must belong to this album");
                int oldIndex = imageIds.indexOf(image.getId());
                if (oldIndex <= 0) {
-                       return null;
+                       return image;
                }
                imageIds.remove(image.getId());
                imageIds.add(oldIndex - 1, image.getId());
@@ -224,7 +224,7 @@ public class AlbumImpl implements Album {
                checkArgument(image.getAlbum().equals(this), "image must belong to this album");
                int oldIndex = imageIds.indexOf(image.getId());
                if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
-                       return null;
+                       return image;
                }
                imageIds.remove(image.getId());
                imageIds.add(oldIndex + 1, image.getId());
index 0ef220b..e9a0c57 100644 (file)
@@ -18,6 +18,8 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 
 import freenet.keys.FreenetURI;
 
+import com.google.common.base.Objects;
+
 /**
  * {@link Sone} implementation that only stores the ID of a Sone and returns
  * {@code null}, {@code 0}, or empty collections where appropriate.
@@ -240,4 +242,14 @@ public class IdOnlySone implements Sone {
                return id;
        }
 
+       @Override
+       public int hashCode() {
+               return id.hashCode();
+       }
+
+       @Override
+       public boolean equals(Object object) {
+               return (object != null) && (object.getClass() == getClass()) && Objects.equal(id, ((IdOnlySone) object).id);
+       }
+
 }
index a8cb62b..2fe10df 100644 (file)
@@ -142,7 +142,7 @@ public class IdentityChangeDetector {
                return new Predicate<Identity>() {
                        @Override
                        public boolean apply(Identity identity) {
-                               return identities.containsKey(identity.getId());
+                               return (identity != null) && identities.containsKey(identity.getId());
                        }
                };
        }
index b85f100..7319b2b 100644 (file)
@@ -59,41 +59,21 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
         */
        private enum LinkType {
 
-               /** Link is a KSK. */
-               KSK("KSK@"),
+               KSK("KSK@", true),
+               CHK("CHK@", true),
+               SSK("SSK@", true),
+               USK("USK@", true),
+               HTTP("http://", false),
+               HTTPS("https://", false),
+               SONE("sone://", false),
+               POST("post://", false);
 
-               /** Link is a CHK. */
-               CHK("CHK@"),
-
-               /** Link is an SSK. */
-               SSK("SSK@"),
-
-               /** Link is a USK. */
-               USK("USK@"),
-
-               /** Link is HTTP. */
-               HTTP("http://"),
-
-               /** Link is HTTPS. */
-               HTTPS("https://"),
-
-               /** Link is a Sone. */
-               SONE("sone://"),
-
-               /** Link is a post. */
-               POST("post://");
-
-               /** The scheme identifying this link type. */
                private final String scheme;
+               private final boolean freenetLink;
 
-               /**
-                * Creates a new link type identified by the given scheme.
-                *
-                * @param scheme
-                *            The scheme of the link type
-                */
-               private LinkType(String scheme) {
+               LinkType(String scheme, boolean freenetLink) {
                        this.scheme = scheme;
+                       this.freenetLink = freenetLink;
                }
 
                /**
@@ -105,6 +85,10 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                        return scheme;
                }
 
+               public boolean isFreenetLink() {
+                       return freenetLink;
+               }
+
        }
 
        /** The Sone provider. */
@@ -211,7 +195,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        }
 
                                        /* cut off “freenet:” from before keys. */
-                                       if (((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
+                                       if (linkType.isFreenetLink() && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
                                                next -= 8;
                                                line = line.substring(0, next) + line.substring(next + 8);
                                        }
index 30a5890..a1e987a 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import static com.google.common.base.Optional.fromNullable;
 import static java.util.logging.Logger.getLogger;
 
 import java.awt.Image;
@@ -55,8 +56,8 @@ import freenet.support.api.HTTPUploadedFile;
  */
 public class UploadImagePage extends SoneTemplatePage {
 
-       /** The logger. */
        private static final Logger logger = getLogger(UploadImagePage.class.getName());
+       private static final String UNKNOWN_MIME_TYPE = "application/octet-stream";
 
        /**
         * Creates a new “upload image” page.
@@ -154,12 +155,13 @@ public class UploadImagePage extends SoneTemplatePage {
                        ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageDataInputStream);
                        Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
                        if (imageReaders.hasNext()) {
-                               return imageReaders.next().getOriginatingProvider().getMIMETypes()[0];
+                               return fromNullable(imageReaders.next().getOriginatingProvider().getMIMETypes())
+                                               .or(new String[] { UNKNOWN_MIME_TYPE })[0];
                        }
                } catch (IOException ioe1) {
                        logger.log(Level.FINE, "Could not detect MIME type for image.", ioe1);
                }
-               return "application/octet-stream";
+               return UNKNOWN_MIME_TYPE;
        }
 
 }