Add state for torrent files.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / states / TorrentState.java
1 /*
2  * Reactor - TorrentState.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.reactor.states;
19
20 import java.util.Iterator;
21 import java.util.List;
22
23 import net.pterodactylus.reactor.State;
24 import net.pterodactylus.reactor.states.TorrentState.TorrentFile;
25
26 import com.google.common.collect.Lists;
27
28 /**
29  * {@link State} that contains information about an arbitrary number of torrent
30  * files.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class TorrentState extends AbstractState implements Iterable<TorrentFile> {
35
36         /** The torrent files. */
37         private final List<TorrentFile> files = Lists.newArrayList();
38
39         //
40         // ACCESSORS
41         //
42
43         /**
44          * Adds a torrent file to this state.
45          *
46          * @param torrentFile
47          *            The torrent file to add
48          * @return This state
49          */
50         public TorrentState addTorrentFile(TorrentFile torrentFile) {
51                 files.add(torrentFile);
52                 return this;
53         }
54
55         //
56         // ITERABLE METHODS
57         //
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         public Iterator<TorrentFile> iterator() {
64                 return files.iterator();
65         }
66
67         //
68         // OBJECT METHODS
69         //
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         public String toString() {
76                 return String.format("%s[files=%s]", getClass().getSimpleName(), files);
77         }
78
79         /**
80          * Container for torrent file data.
81          *
82          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
83          */
84         public static class TorrentFile {
85
86                 /** The name of the file. */
87                 private final String name;
88
89                 /** The size of the file. */
90                 private final String size;
91
92                 /** The magnet URI of the file. */
93                 private final String magnetUri;
94
95                 /** The download URI of the file. */
96                 private final String downloadUri;
97
98                 /**
99                  * Creates a new torrent file.
100                  *
101                  * @param name
102                  *            The name of the file
103                  * @param size
104                  *            The size of the file
105                  * @param magnetUri
106                  *            The magnet URI of the file
107                  * @param downloadUri
108                  *            The download URI of the file
109                  */
110                 public TorrentFile(String name, String size, String magnetUri, String downloadUri) {
111                         this.name = name;
112                         this.size = size;
113                         this.magnetUri = magnetUri;
114                         this.downloadUri = downloadUri;
115                 }
116
117                 //
118                 // ACCESSORS
119                 //
120
121                 /**
122                  * Returns the name of the file.
123                  *
124                  * @return The name of the file
125                  */
126                 public String name() {
127                         return name;
128                 }
129
130                 /**
131                  * Returns the size of the file. The returned size may included
132                  * non-numeric information, such as units (e. g. “860.46 MB”).
133                  *
134                  * @return The size of the file
135                  */
136                 public String size() {
137                         return size;
138                 }
139
140                 /**
141                  * Returns the magnet URI of the file.
142                  *
143                  * @return The magnet URI of the file
144                  */
145                 public String magnetUri() {
146                         return magnetUri;
147                 }
148
149                 /**
150                  * Returns the download URI of the file.
151                  *
152                  * @return The download URI of the file
153                  */
154                 public String downloadUri() {
155                         return downloadUri;
156                 }
157
158                 //
159                 // OBJECT METHODS
160                 //
161
162                 /**
163                  * {@inheritDoc}
164                  */
165                 @Override
166                 public String toString() {
167                         return String.format("%s(%s,%s,%s)", name(), size(), magnetUri(), downloadUri());
168                 }
169
170         }
171
172 }