Remove unused cast.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / SoneTextParser.java
index 0b1585a..3085517 100644 (file)
@@ -28,16 +28,15 @@ import java.util.regex.Pattern;
 
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.SoneImpl;
-import net.pterodactylus.sone.database.PostProvider;
-import net.pterodactylus.sone.database.SoneProvider;
+import net.pterodactylus.sone.data.impl.DefaultSone;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 
-import com.google.common.base.Optional;
-
 import freenet.keys.FreenetURI;
 
+import com.google.common.base.Optional;
+
 /**
  * {@link Parser} implementation that can recognize Freenet URIs.
  *
@@ -59,40 +58,43 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
        private enum LinkType {
 
                /** Link is a KSK. */
-               KSK("KSK@"),
+               KSK("KSK@", true),
 
                /** Link is a CHK. */
-               CHK("CHK@"),
+               CHK("CHK@", true),
 
                /** Link is an SSK. */
-               SSK("SSK@"),
+               SSK("SSK@", true),
 
                /** Link is a USK. */
-               USK("USK@"),
+               USK("USK@", true),
 
                /** Link is HTTP. */
-               HTTP("http://"),
+               HTTP("http://", false),
 
                /** Link is HTTPS. */
-               HTTPS("https://"),
+               HTTPS("https://", false),
 
                /** Link is a Sone. */
-               SONE("sone://"),
+               SONE("sone://", false),
 
                /** Link is a post. */
-               POST("post://");
+               POST("post://", false);
 
                /** 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
+                * @param freenetLink
                 */
-               private LinkType(String scheme) {
+               private LinkType(String scheme, boolean freenetLink) {
                        this.scheme = scheme;
+                       this.freenetLink = freenetLink;
                }
 
                /**
@@ -104,34 +106,27 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                        return scheme;
                }
 
-       }
+               public boolean isFreenetLink() {
+                       return freenetLink;
+               }
 
-       /** The Sone provider. */
-       private final SoneProvider soneProvider;
+       }
 
-       /** The post provider. */
-       private final PostProvider postProvider;
+       private final Database database;
 
        /**
         * Creates a new freenet link parser.
         *
-        * @param soneProvider
-        *            The Sone provider
-        * @param postProvider
-        *            The post provider
+        * @param database
         */
-       public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
-               this.soneProvider = soneProvider;
-               this.postProvider = postProvider;
+       public SoneTextParser(Database database) {
+               this.database = database;
        }
 
        //
        // PART METHODS
        //
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public Iterable<Part> parse(SoneTextParserContext context, Reader source) throws IOException {
                PartContainer parts = new PartContainer();
@@ -210,7 +205,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);
                                        }
@@ -222,7 +217,6 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (next > 0) {
                                                parts.add(new PlainTextPart(line.substring(0, next)));
                                                line = line.substring(next);
-                                               next = 0;
                                        }
                                        lineComplete = false;
 
@@ -243,13 +237,13 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (linkType == LinkType.SONE) {
                                                if (line.length() >= (7 + 43)) {
                                                        String soneId = line.substring(7, 50);
-                                                       Optional<Sone> sone = soneProvider.getSone(soneId);
+                                                       Optional<Sone> sone = database.getSone(soneId);
                                                        if (!sone.isPresent()) {
                                                                /*
                                                                 * don’t use create=true above, we don’t want
                                                                 * the empty shell.
                                                                 */
-                                                               sone = Optional.<Sone>of(new SoneImpl(soneId, false));
+                                                               sone = Optional.<Sone>of(new DefaultSone(database, soneId, false, null));
                                                        }
                                                        parts.add(new SonePart(sone.get()));
                                                        line = line.substring(50);
@@ -262,7 +256,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (linkType == LinkType.POST) {
                                                if (line.length() >= (7 + 36)) {
                                                        String postId = line.substring(7, 43);
-                                                       Optional<Post> post = postProvider.getPost(postId);
+                                                       Optional<Post> post = database.getPost(postId);
                                                        if (post.isPresent()) {
                                                                parts.add(new PostPart(post.get()));
                                                        } else {
@@ -276,7 +270,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                continue;
                                        }
 
-                                       if ((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
+                                       if (linkType.isFreenetLink()) {
                                                FreenetURI uri;
                                                if (name.indexOf('?') > -1) {
                                                        name = name.substring(0, name.indexOf('?'));