2 * Sone - ImageInserter.java - Copyright © 2011 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.core;
20 import java.util.Collections;
21 import java.util.HashMap;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
26 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
27 import net.pterodactylus.sone.data.Image;
28 import net.pterodactylus.sone.data.TemporaryImage;
29 import net.pterodactylus.util.logging.Logging;
30 import net.pterodactylus.util.validation.Validation;
33 * The image inserter is responsible for inserting images using
34 * {@link FreenetInterface#insertImage(TemporaryImage, Image, InsertToken)} and
35 * also tracks running inserts, giving the possibility to abort a running
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class ImageInserter {
43 private static final Logger logger = Logging.getLogger(ImageInserter.class);
46 private final Core core;
48 /** The freenet interface. */
49 private final FreenetInterface freenetInterface;
51 /** The tokens of running inserts. */
52 private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
55 * Creates a new image inserter.
59 * @param freenetInterface
60 * The freenet interface
62 public ImageInserter(Core core, FreenetInterface freenetInterface) {
64 this.freenetInterface = freenetInterface;
68 * Inserts the given image. The {@link #core} will automatically added as
69 * {@link ImageInsertListener} to the created {@link InsertToken}.
71 * @param temporaryImage
72 * The temporary image data
76 public void insertImage(TemporaryImage temporaryImage, Image image) {
77 Validation.begin().isNotNull("Temporary Image", temporaryImage).isNotNull("Image", image).check().isEqual("Image IDs", image.getId(), temporaryImage.getId()).check();
79 InsertToken insertToken = freenetInterface.new InsertToken(image);
80 insertTokens.put(image.getId(), insertToken);
81 insertToken.addImageInsertListener(core);
82 freenetInterface.insertImage(temporaryImage, image, insertToken);
83 } catch (SoneException se1) {
84 logger.log(Level.WARNING, "Could not insert image!", se1);
89 * Cancels a running image insert. If no insert is running for the given
90 * image, nothing happens.
93 * The image being inserted
95 public void cancelImageInsert(Image image) {
96 InsertToken insertToken = insertTokens.remove(image.getId());
97 if (insertToken == null) {
100 insertToken.cancel();
101 insertToken.removeImageInsertListener(core);