2 * Sone - ImageInserter.java - Copyright © 2011–2012 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);
45 /** The freenet interface. */
46 private final FreenetInterface freenetInterface;
48 /** The tokens of running inserts. */
49 private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
52 * Creates a new image inserter.
54 * @param freenetInterface
55 * The freenet interface
57 public ImageInserter(FreenetInterface freenetInterface) {
58 this.freenetInterface = freenetInterface;
62 * Inserts the given image.
64 * @param temporaryImage
65 * The temporary image data
69 public void insertImage(TemporaryImage temporaryImage, Image image) {
70 Validation.begin().isNotNull("Temporary Image", temporaryImage).isNotNull("Image", image).check().isEqual("Image IDs", image.getId(), temporaryImage.getId()).check();
72 InsertToken insertToken = freenetInterface.new InsertToken(image);
73 insertTokens.put(image.getId(), insertToken);
74 freenetInterface.insertImage(temporaryImage, image, insertToken);
75 } catch (SoneException se1) {
76 logger.log(Level.WARNING, "Could not insert image!", se1);
81 * Cancels a running image insert. If no insert is running for the given
82 * image, nothing happens.
85 * The image being inserted
87 public void cancelImageInsert(Image image) {
88 InsertToken insertToken = insertTokens.remove(image.getId());
89 if (insertToken == null) {