diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/DirectoryScanningChangeListener.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/DirectoryScanningChangeListener.java index d5d055d03904af6e5166a396c260de60b8ea31d2..7522a78d6db943ecd5a40f36c228cb155d3471d5 100644 --- a/common/source/java/ch/systemsx/cisd/common/highwatermark/DirectoryScanningChangeListener.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/DirectoryScanningChangeListener.java @@ -24,8 +24,8 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.HighwaterMarkEvent; -import ch.systemsx.cisd.common.utilities.DirectoryScannedStore; import ch.systemsx.cisd.common.utilities.DirectoryScanningTimerTask; +import ch.systemsx.cisd.common.utilities.StoreItem; /** * A <code>ChangeListener</code> implementation that informs the encapsulated @@ -38,16 +38,22 @@ import ch.systemsx.cisd.common.utilities.DirectoryScanningTimerTask; */ public abstract class DirectoryScanningChangeListener implements ChangeListener { - private static final File[] EMPTY_FILE_ARRAY = new File[0]; - private DirectoryScanningTimerTask directoryScanning; - protected final Set<File> unhandledPaths = new HashSet<File>(); + protected final Set<StoreItem> unhandledItems = new HashSet<StoreItem>(); protected DirectoryScanningChangeListener() { } + /** + * Encapsulates given <var>path</var> in a <code>StoreItem</code> and returns it. + */ + protected final StoreItem asStoreItem(final File path) + { + return StoreItem.asItem(path); + } + /** * Sets the <code>DirectoryScanningTimerTask</code> that should get informed (and remove the * unhandled paths from the faulty ones) when free space is again OK. @@ -68,9 +74,8 @@ public abstract class DirectoryScanningChangeListener implements ChangeListener final HighwaterMarkEvent event = (HighwaterMarkEvent) e; if (event.isBelow() == false) { - directoryScanning.removeFaultyPaths(DirectoryScannedStore.asItems(unhandledPaths - .toArray(EMPTY_FILE_ARRAY))); - unhandledPaths.clear(); + directoryScanning.removeFaultyPaths(unhandledItems.toArray(StoreItem.EMPTY_ARRAY)); + unhandledItems.clear(); } } diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/PathHandlerInterceptor.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/PathHandlerInterceptor.java index b9dafe54155ce225119b72c2555eb73e075bef7f..65b6b23ed550e915185823903e75b8f8aa7165a3 100644 --- a/common/source/java/ch/systemsx/cisd/common/highwatermark/PathHandlerInterceptor.java +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/PathHandlerInterceptor.java @@ -50,7 +50,7 @@ public final class PathHandlerInterceptor extends DirectoryScanningChangeListene final boolean mayHandle = pathHandler.mayHandle(path); if (mayHandle == false) { - unhandledPaths.add(path); + unhandledItems.add(asStoreItem(path)); } return mayHandle; } diff --git a/common/source/java/ch/systemsx/cisd/common/highwatermark/StoreHandlerInterceptor.java b/common/source/java/ch/systemsx/cisd/common/highwatermark/StoreHandlerInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..cd6f45670ddc905231d1b1caf9c9a7e26783c93c --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/highwatermark/StoreHandlerInterceptor.java @@ -0,0 +1,58 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.highwatermark; + +import ch.systemsx.cisd.common.utilities.IStoreHandler; +import ch.systemsx.cisd.common.utilities.StoreItem; + +/** + * An <code>IStoreHandler</code> implementation which collects the unhandled store items before + * delegating the calls. + * + * @author Christian Ribeaud + */ +public final class StoreHandlerInterceptor extends DirectoryScanningChangeListener implements + IStoreHandler +{ + private final IStoreHandler storeHandler; + + public StoreHandlerInterceptor(final IStoreHandler storeHandler) + { + super(); + this.storeHandler = storeHandler; + } + + // + // IStoreHandler + // + + public final void handle(final StoreItem item) + { + storeHandler.handle(item); + } + + public final boolean mayHandle(final StoreItem item) + { + final boolean mayHandle = storeHandler.mayHandle(item); + if (mayHandle == false) + { + unhandledItems.add(item); + } + return mayHandle; + } + +} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/DirectoryScannedStore.java b/common/source/java/ch/systemsx/cisd/common/utilities/DirectoryScannedStore.java index cc9856435a0fafc12e5d8c73e3dbc6976ab3ff1e..de43510eb15117851046927ca54905d681a0b629 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/DirectoryScannedStore.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/DirectoryScannedStore.java @@ -38,21 +38,6 @@ public final class DirectoryScannedStore implements IScannedStore this.directory = directory; } - public final static StoreItem[] asItems(final File[] files) - { - final StoreItem[] items = new StoreItem[files.length]; - for (int i = 0; i < items.length; i++) - { - items[i] = asItem(files[i]); - } - return items; - } - - public final static StoreItem asItem(final File file) - { - return new StoreItem(file.getName()); - } - // // IScannedStore // @@ -73,7 +58,7 @@ public final class DirectoryScannedStore implements IScannedStore if (files != null) { FileUtilities.sortByLastModified(files); - return asItems(files); + return StoreItem.asItems(files); } else { return null; diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/StoreItem.java b/common/source/java/ch/systemsx/cisd/common/utilities/StoreItem.java index 7f55db7aef16c8302101f143f76d7cdb586bba07..80599dd83ce46c2a3dbd8a73542cc0ea79b61ed1 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/StoreItem.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/StoreItem.java @@ -23,7 +23,7 @@ import java.io.File; * * @author Tomasz Pylak */ -public class StoreItem +public final class StoreItem { public static final StoreItem[] EMPTY_ARRAY = new StoreItem[0]; @@ -40,11 +40,43 @@ public class StoreItem return name; } + /** + * Translates given {@link StoreItem} into a {@link File}. + */ public final static File asFile(final File parentDirectory, final StoreItem item) { + assert item != null : "Unspecified item"; + assert parentDirectory != null : "Unspecified parent directory"; return new File(parentDirectory, item.getName()); } + /** + * Translates given {@link File}s into {@link StoreItem}s. + * <p> + * Never returns <code>null</code> but could return an empty array. + * </p> + */ + public final static StoreItem[] asItems(final File[] files) + { + assert files != null : "Unspecified files"; + final int len = files.length; + final StoreItem[] items = new StoreItem[len]; + for (int i = 0; i < len; i++) + { + items[i] = asItem(files[i]); + } + return items; + } + + /** + * Translates given {@link File} into a {@link StoreItem}. + */ + public final static StoreItem asItem(final File file) + { + assert file != null : "Unspecified file"; + return new StoreItem(file.getName()); + } + // // Object // @@ -66,4 +98,5 @@ public class StoreItem { return name.hashCode(); } + } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/DirectoryScanningTimerTaskTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/DirectoryScanningTimerTaskTest.java index 40e4fd0f0acbff284770832fead1532d3401d4e0..5e9bb5ff9f67427f60a06445cc092f9444770fc0 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/DirectoryScanningTimerTaskTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/DirectoryScanningTimerTaskTest.java @@ -438,7 +438,7 @@ public class DirectoryScanningTimerTaskTest List<String> faulty = CollectionIO.readList(getFaultyPathFile()); assertEquals(1, faulty.size()); final File file = new File(faulty.get(0)); - directoryScanning.removeFaultyPaths(DirectoryScannedStore.asItem(file)); + directoryScanning.removeFaultyPaths(StoreItem.asItem(file)); faulty = CollectionIO.readList(getFaultyPathFile()); // Faulty file is empty. assertEquals(0, faulty.size());