All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openstreetmap.atlas.utilities.archive.AbstractArchiverOrExtractor Maven / Gradle / Ivy

There is a newer version: 7.0.8
Show newest version
package org.openstreetmap.atlas.utilities.archive;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Abstract superclass for the Archiver and Extractor classes; handles all of the event listener
 * subscriptions and provides helper methods for sending events
 *
 * @author cstaylor
 * @param 
 *            either the Archiver or Extractor class
 */
abstract class AbstractArchiverOrExtractor
{
    private final Collection> listeners;

    private final Class klass;

    private ArchiveVetoDelegate delegate;

    protected AbstractArchiverOrExtractor(final Class klass)
    {
        this.klass = klass;
        this.listeners = new CopyOnWriteArraySet<>();
    }

    public AbstractArchiverOrExtractor addArchiverEventListener(
            final ArchiverEventListener listener)
    {
        this.listeners.add(listener);
        return this;
    }

    public AbstractArchiverOrExtractor removeArchiverEventListener(
            final ArchiverEventListener listener)
    {
        this.listeners.remove(listener);
        return this;
    }

    public AbstractArchiverOrExtractor setVetoDelegate(final ArchiveVetoDelegate delegate)
    {
        this.delegate = delegate;
        return this;
    }

    protected void fireArchiveCompleted()
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.archiveCompleted(this.klass.cast(this));
        }
    }

    protected void fireArchiveFailed()
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.archiveFailed(this.klass.cast(this));
        }
    }

    protected void fireArchiveStarted()
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.archiveStarted(this.klass.cast(this));
        }
    }

    protected void fireItemCompleted(final File file)
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.itemCompleted(this.klass.cast(this), file);
        }
    }

    protected void fireItemFailed(final File file, final IOException oops)
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.itemFailed(this.klass.cast(this), file, oops);
        }
    }

    protected void fireItemInProgress(final File file, final long count, final long length)
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.itemInProgress(this.klass.cast(this), file, count, length);
        }
    }

    protected void fireItemSkipped(final File file)
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.itemSkipped(this.klass.cast(this), file);
        }
    }

    protected void fireItemStarted(final File file)
    {
        for (final ArchiverEventListener listener : this.listeners)
        {
            listener.itemStarted(this.klass.cast(this), file);
        }
    }

    protected boolean shouldSkip(final File file)
    {
        return this.delegate != null && this.delegate.shouldSkip(this.klass.cast(this), file);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy