2 * Sone - ImageInserter.java - Copyright © 2011–2013 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 static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
23 import java.util.Collections;
24 import java.util.HashMap;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
30 import net.pterodactylus.sone.data.Image;
31 import net.pterodactylus.sone.data.TemporaryImage;
32 import net.pterodactylus.util.logging.Logging;
35 * The image inserter is responsible for inserting images using
36 * {@link FreenetInterface#insertImage(TemporaryImage, Image, InsertToken)} and
37 * also tracks running inserts, giving the possibility to abort a running
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class ImageInserter {
45 private static final Logger logger = Logging.getLogger(ImageInserter.class);
47 /** The freenet interface. */
48 private final FreenetInterface freenetInterface;
50 /** The tokens of running inserts. */
51 private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
54 * Creates a new image inserter.
56 * @param freenetInterface
57 * The freenet interface
59 public ImageInserter(FreenetInterface freenetInterface) {
60 this.freenetInterface = freenetInterface;
64 * Inserts the given image.
66 * @param temporaryImage
67 * The temporary image data
71 public void insertImage(TemporaryImage temporaryImage, Image image) {
72 checkNotNull(temporaryImage, "temporaryImage must not be null");
73 checkNotNull(image, "image must not be null");
74 checkArgument(image.getId().equals(temporaryImage.getId()), "image IDs must match");
76 InsertToken insertToken = freenetInterface.new InsertToken(image);
77 insertTokens.put(image.getId(), insertToken);
78 freenetInterface.insertImage(temporaryImage, image, insertToken);
79 } catch (SoneException se1) {
80 logger.log(Level.WARNING, "Could not insert image!", se1);
85 * Cancels a running image insert. If no insert is running for the given
86 * image, nothing happens.
89 * The image being inserted
91 public void cancelImageInsert(Image image) {
92 InsertToken insertToken = insertTokens.remove(image.getId());
93 if (insertToken == null) {