From f4d5dbe7d115fb18ec375d5e109c18359d50cbd9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 12 Apr 2011 09:03:31 +0200 Subject: [PATCH] Add image inserter. --- .../net/pterodactylus/sone/core/ImageInserter.java | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/core/ImageInserter.java diff --git a/src/main/java/net/pterodactylus/sone/core/ImageInserter.java b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java new file mode 100644 index 0000000..1881902 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java @@ -0,0 +1,104 @@ +/* + * Sone - ImageInserter.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.core; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.pterodactylus.sone.core.FreenetInterface.InsertToken; +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.TemporaryImage; +import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.validation.Validation; + +/** + * The image inserter is responsible for inserting images using + * {@link FreenetInterface#insertImage(TemporaryImage, Image, InsertToken)} and + * also tracks running inserts, giving the possibility to abort a running + * insert. + * + * @author David ‘Bombe’ Roden + */ +public class ImageInserter { + + /** The logger. */ + private static final Logger logger = Logging.getLogger(ImageInserter.class); + + /** The core. */ + private final Core core; + + /** The freenet interface. */ + private final FreenetInterface freenetInterface; + + /** The tokens of running inserts. */ + private final Map insertTokens = Collections.synchronizedMap(new HashMap()); + + /** + * Creates a new image inserter. + * + * @param core + * The Sone core + * @param freenetInterface + * The freenet interface + */ + public ImageInserter(Core core, FreenetInterface freenetInterface) { + this.core = core; + this.freenetInterface = freenetInterface; + } + + /** + * Inserts the given image. The {@link #core} will automatically added as + * {@link ImageInsertListener} to the created {@link InsertToken}. + * + * @param temporaryImage + * The temporary image data + * @param image + * The image + */ + public void insertImage(TemporaryImage temporaryImage, Image image) { + Validation.begin().isNotNull("Temporary Image", temporaryImage).isNotNull("Image", image).check().isEqual("Image IDs", image.getId(), temporaryImage.getId()).check(); + try { + InsertToken insertToken = new InsertToken(image); + insertTokens.put(image.getId(), insertToken); + insertToken.addImageInsertListener(core); + freenetInterface.insertImage(temporaryImage, image, insertToken); + } catch (SoneException se1) { + logger.log(Level.WARNING, "Could not insert image!", se1); + } + } + + /** + * Cancels a running image insert. If no insert is running for the given + * image, nothing happens. + * + * @param image + * The image being inserted + */ + public void cancelImageInsert(Image image) { + InsertToken insertToken = insertTokens.remove(image); + if (insertToken == null) { + return; + } + insertToken.cancel(); + insertToken.removeImageInsertListener(core); + } + +} -- 2.7.4