<version>3.8.2</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>net.pterodactylus</groupId>
+ <artifactId>utils</artifactId>
+ <version>0.1.1</version>
+ </dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+++ /dev/null
-/*
- * jFCPlib - AbstractListenerManager.java - Copyright © 2009 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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.util.event;
-
-import java.util.EventListener;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.Executor;
-
-import net.pterodactylus.util.thread.CurrentThreadExecutor;
-
-/**
- * Abstract implementation of a listener support class. The listener support
- * takes care of adding and removing {@link EventListener} implementations, and
- * subclasses are responsible for firing appropriate events.
- *
- * @param <S>
- * The type of the source
- *@param <L>
- * The type of the event listeners
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public abstract class AbstractListenerManager<S, L extends EventListener> {
-
- /** The source that emits the events. */
- private final S source;
-
- /** The list of listeners. */
- private final List<L> listeners = new CopyOnWriteArrayList<L>();
-
- /** Service that executes event threads. */
- private final Executor executor;
-
- /**
- * Creates a new listener support that emits events from the given source.
- *
- * @param source
- * The source of the events
- */
- public AbstractListenerManager(S source) {
- this(source, new CurrentThreadExecutor());
- }
-
- /**
- * Creates a new listener support that emits events from the given source.
- *
- * @param source
- * The source of the events
- * @param executor
- * The executor used to fire events
- */
- public AbstractListenerManager(S source, Executor executor) {
- this.source = source;
- this.executor = executor;
- }
-
- /**
- * Adds the given listener to the list of reigstered listeners.
- *
- * @param listener
- * The listener to add
- */
- public void addListener(L listener) {
- synchronized (listeners) {
- listeners.add(listener);
- }
- }
-
- /**
- * Removes the given listener from the list of registered listeners.
- *
- * @param listener
- * The listener to remove
- */
- public void removeListener(L listener) {
- synchronized (listeners) {
- listeners.remove(listener);
- }
- }
-
- //
- // PROTECTED METHODS
- //
-
- /**
- * Returns the source for the events.
- *
- * @return The event source
- */
- protected S getSource() {
- return source;
- }
-
- /**
- * Returns the executor for the event firing.
- *
- * @return The executor
- */
- protected Executor getExecutor() {
- return executor;
- }
-
- /**
- * Returns a list of all registered listeners. The returned list is a copy
- * of the original list so structural modifications will never occur when
- * using the returned list.
- *
- * @return The list of all registered listeners
- */
- protected List<L> getListeners() {
- return listeners;
- }
-
-}
+++ /dev/null
-/*
- * jFCPlib - Filter.java - Copyright © 2009 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 2 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-package net.pterodactylus.util.filter;
-
-/**
- * Interface for a filter that determines whether a certain action can be
- * performed on an object based on its properties.
- *
- * @param <T>
- * The type of the filtered object
- * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- */
-public interface Filter<T> {
-
- /**
- * Runs the given object through this filter and return whether the object
- * matches this filter or not.
- *
- * @param object
- * The object to analyse
- * @return <code>true</code> if the object matched this filter,
- * <code>false</code> otherwise
- */
- public boolean filterObject(T object);
-
-}
+++ /dev/null
-/*
- * jFCPlib - Filters.java - Copyright © 2009 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 2 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-package net.pterodactylus.util.filter;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Set;
-import java.util.Map.Entry;
-
-/**
- * Defines various methods to filter {@link Collection}s.
- *
- * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- */
-public class Filters {
-
- /**
- * Returns a list that contains only the elements from the given list that
- * match the given filter.
- *
- * @param <E>
- * The type of the list elements
- * @param list
- * The list to filter
- * @param listFilter
- * The list filter
- * @return The filtered list
- */
- public static <E> List<E> filteredList(List<E> list, Filter<E> listFilter) {
- List<E> filteredList = new ArrayList<E>();
- for (E element : list) {
- if (listFilter.filterObject(element)) {
- filteredList.add(element);
- }
- }
- return filteredList;
- }
-
- /**
- * Returns a set that contains only the elements from the given set that
- * match the given filter.
- *
- * @param <E>
- * The type of the set elements
- * @param set
- * The set to filter
- * @param setFilter
- * The set filter
- * @return The filtered set
- */
- public static <E> Set<E> filteredSet(Set<E> set, Filter<E> setFilter) {
- Set<E> filteredSet = new HashSet<E>();
- for (E element : set) {
- if (setFilter.filterObject(element)) {
- filteredSet.add(element);
- }
- }
- return filteredSet;
- }
-
- /**
- * Returns a map that contains only the elements from the given map that
- * match the given filter.
- *
- * @param <K>
- * The type of the map keys
- * @param <V>
- * The type of the map values
- * @param map
- * The map to filter
- * @param mapFilter
- * The map filter
- * @return The filtered map
- */
- public static <K, V> Map<K, V> filteredMap(Map<K, V> map, Filter<Entry<K, V>> mapFilter) {
- Map<K, V> filteredMap = new HashMap<K, V>();
- for (Entry<K, V> element : map.entrySet()) {
- if (mapFilter.filterObject(element)) {
- filteredMap.put(element.getKey(), element.getValue());
- }
- }
- return filteredMap;
- }
-
- /**
- * Returns a collection that contains only the elements from the given
- * collection that match the given filter.
- *
- * @param <K>
- * The type of the collection values
- * @param collection
- * The collection to filter
- * @param collectionFilter
- * The collection filter
- * @return The filtered collection
- */
- public static <K> Collection<K> filteredCollection(Collection<K> collection, Filter<K> collectionFilter) {
- return filteredList(new ArrayList<K>(collection), collectionFilter);
- }
-
- /**
- * Returns an iterator that contains only the elements from the given
- * iterator that match the given filter.
- *
- * @param <E>
- * The type of the iterator elements
- * @param iterator
- * The iterator to filter
- * @param iteratorFilter
- * The iterator filter
- * @return The filtered iterator
- */
- public static <E> Iterator<E> filteredIterator(final Iterator<E> iterator, final Filter<E> iteratorFilter) {
- return new Iterator<E>() {
-
- private boolean gotNextElement = false;
-
- private E nextElement;
-
- private void getNextElement() {
- if (gotNextElement) {
- return;
- }
- while (iterator.hasNext()) {
- nextElement = iterator.next();
- if (iteratorFilter.filterObject(nextElement)) {
- gotNextElement = true;
- break;
- }
- }
- }
-
- /**
- * {@inheritDoc}
- *
- * @see java.util.Iterator#hasNext()
- */
- public boolean hasNext() {
- getNextElement();
- return gotNextElement;
- }
-
- /**
- * {@inheritDoc}
- *
- * @see java.util.Iterator#next()
- */
- public E next() {
- getNextElement();
- if (!gotNextElement) {
- throw new NoSuchElementException("no more elements in iteration");
- }
- gotNextElement = false;
- return nextElement;
- }
-
- /**
- * {@inheritDoc}
- *
- * @see java.util.Iterator#remove()
- */
- public void remove() {
- throw new UnsupportedOperationException("remove() not supported on this iteration");
- }
-
- };
- }
-
-}
+++ /dev/null
-/*
- * jFCPlib - CurrentThreadExecutor.java - Copyright © 2009 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 <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.util.thread;
-
-import java.util.concurrent.Executor;
-
-/**
- * An {@link Executor} that executes {@link Runnable}s in the current thread.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class CurrentThreadExecutor implements Executor {
-
- /**
- * {@inheritDoc}
- *
- * @see java.util.concurrent.Executor#execute(java.lang.Runnable)
- */
- public void execute(Runnable command) {
- command.run();
- }
-
-}
+++ /dev/null
-/*
- * jFCPlib - UserObject.java - Copyright © 2009 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 2 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-package net.pterodactylus.util.thread;
-
-/**
- * Wrapper around an object that can be set and retrieved. Its primary use is as
- * a container for return values from anonymous classes.
- *
- * <pre>
- * final ObjectWrapper<Object> objectWrapper = new ObjectWrapper<Object>();
- * new Runnable() {
- * public void run() {
- * ...
- * objectWrapper.set(someResult);
- * }
- * }.run();
- * Object result = objectWrapper.get();
- * </pre>
- *
- * @param <T>
- * The type of the wrapped object
- * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
- */
-public class ObjectWrapper<T> {
-
- /** The wrapped object. */
- private volatile T wrappedObject;
-
- /**
- * Returns the wrapped object.
- *
- * @return The wrapped object
- */
- public T get() {
- return wrappedObject;
- }
-
- /**
- * Sets the wrapped object.
- *
- * @param wrappedObject
- * The wrapped object
- */
- public void set(T wrappedObject) {
- this.wrappedObject = wrappedObject;
- }
-
-}