2 * Sone - ImageInserter.java - Copyright © 2011–2016 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;
22 import static java.util.logging.Logger.getLogger;
24 import java.util.Collections;
25 import java.util.HashMap;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
31 import net.pterodactylus.sone.data.Image;
32 import net.pterodactylus.sone.data.TemporaryImage;
34 import com.google.common.base.Function;
37 * The image inserter is responsible for inserting images using
38 * {@link FreenetInterface#insertImage(TemporaryImage, Image, InsertToken)} and
39 * also tracks running inserts, giving the possibility to abort a running
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class ImageInserter {
47 private static final Logger logger = getLogger(ImageInserter.class.getName());
49 /** The freenet interface. */
50 private final FreenetInterface freenetInterface;
51 private final Function<Image, InsertToken> insertTokenSupplier;
53 /** The tokens of running inserts. */
54 private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
57 * Creates a new image inserter.
59 * @param freenetInterface
60 * The freenet interface
61 * @param insertTokenSupplier
62 * The supplier for insert tokens
64 public ImageInserter(FreenetInterface freenetInterface, Function<Image, InsertToken> insertTokenSupplier) {
65 this.freenetInterface = freenetInterface;
66 this.insertTokenSupplier = insertTokenSupplier;
70 * Inserts the given image.
72 * @param temporaryImage
73 * The temporary image data
77 public void insertImage(TemporaryImage temporaryImage, Image image) {
78 checkNotNull(temporaryImage, "temporaryImage must not be null");
79 checkNotNull(image, "image must not be null");
80 checkArgument(image.getId().equals(temporaryImage.getId()), "image IDs must match");
82 InsertToken insertToken = insertTokenSupplier.apply(image);
83 insertTokens.put(image.getId(), insertToken);
84 freenetInterface.insertImage(temporaryImage, image, insertToken);
85 } catch (SoneException se1) {
86 logger.log(Level.WARNING, "Could not insert image!", se1);
91 * Cancels a running image insert. If no insert is running for the given
92 * image, nothing happens.
95 * The image being inserted
97 public void cancelImageInsert(Image image) {
98 InsertToken insertToken = insertTokens.remove(image.getId());
99 if (insertToken == null) {
102 insertToken.cancel();