🎨 Use StandardCharsets instead of Charset.forName
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / TorrentState.java
index c4c3e0b..3a7bf54 100644 (file)
 
 package net.pterodactylus.rhynodge.states;
 
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Optional;
 
 import net.pterodactylus.rhynodge.State;
 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
@@ -42,10 +45,41 @@ public class TorrentState extends AbstractState implements Iterable<TorrentFile>
        @JsonProperty
        private List<TorrentFile> files = Lists.newArrayList();
 
+       /**
+        * Creates a new torrent state without torrent files.
+        */
+       public TorrentState() {
+               this(Collections.<TorrentFile> emptySet());
+       }
+
+       /**
+        * Creates a new torrent state containing the given torrent files.
+        *
+        * @param torrentFiles
+        *            The torrent files
+        */
+       public TorrentState(Collection<TorrentFile> torrentFiles) {
+               files.addAll(torrentFiles);
+       }
+
        //
        // ACCESSORS
        //
 
+       @Override
+       public boolean isEmpty() {
+               return files.isEmpty();
+       }
+
+       /**
+        * Returns all torrent files of this state.
+        *
+        * @return All torrent files of this state
+        */
+       public Collection<TorrentFile> torrentFiles() {
+               return Collections.unmodifiableList(files);
+       }
+
        /**
         * Adds a torrent file to this state.
         *
@@ -238,11 +272,7 @@ public class TorrentState extends AbstractState implements Iterable<TorrentFile>
                 */
                private String generateId() {
                        if (magnetUri != null) {
-                               String id = extractId(magnetUri);
-                               if (id != null) {
-                                       return id;
-                               }
-                               return magnetUri;
+                               return extractId(magnetUri).orElse(magnetUri);
                        }
                        return (downloadUri != null) ? downloadUri : name;
                }
@@ -258,14 +288,17 @@ public class TorrentState extends AbstractState implements Iterable<TorrentFile>
                 *            The magnet URI to extract the “xt” from
                 * @return The extracted ID, or {@code null} if no ID could be found
                 */
-               private static String extractId(String magnetUri) {
-                       List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8"));
+               private static Optional<String> extractId(String magnetUri) {
+                       if ((magnetUri == null) || (magnetUri.length() < 8)) {
+                               return Optional.empty();
+                       }
+                       List<NameValuePair> parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), StandardCharsets.UTF_8);
                        for (NameValuePair parameter : parameters) {
                                if (parameter.getName().equals("xt")) {
-                                       return parameter.getValue();
+                                       return Optional.of(parameter.getValue().toLowerCase());
                                }
                        }
-                       return null;
+                       return Optional.empty();
                }
 
                //