From: David ‘Bombe’ Roden Date: Wed, 18 Nov 2009 19:10:04 +0000 (+0100) Subject: Move utility classes to utils. X-Git-Tag: v0.1.1~3 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b5be530cbd21398864b282a9d77cc79f3dd6941c;p=jFCPlib.git Move utility classes to utils. --- diff --git a/pom.xml b/pom.xml index 12eda1a..11c2ee0 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,11 @@ 3.8.2 test + + net.pterodactylus + utils + 0.1.1 + UTF-8 diff --git a/src/main/java/net/pterodactylus/util/event/AbstractListenerManager.java b/src/main/java/net/pterodactylus/util/event/AbstractListenerManager.java deleted file mode 100644 index fb2d43c..0000000 --- a/src/main/java/net/pterodactylus/util/event/AbstractListenerManager.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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 . - */ - -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 - * The type of the source - *@param - * The type of the event listeners - * @author David ‘Bombe’ Roden - */ -public abstract class AbstractListenerManager { - - /** The source that emits the events. */ - private final S source; - - /** The list of listeners. */ - private final List listeners = new CopyOnWriteArrayList(); - - /** 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 getListeners() { - return listeners; - } - -} diff --git a/src/main/java/net/pterodactylus/util/filter/Filter.java b/src/main/java/net/pterodactylus/util/filter/Filter.java deleted file mode 100644 index 90d7699..0000000 --- a/src/main/java/net/pterodactylus/util/filter/Filter.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 - * The type of the filtered object - * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - */ -public interface Filter { - - /** - * Runs the given object through this filter and return whether the object - * matches this filter or not. - * - * @param object - * The object to analyse - * @return true if the object matched this filter, - * false otherwise - */ - public boolean filterObject(T object); - -} diff --git a/src/main/java/net/pterodactylus/util/filter/Filters.java b/src/main/java/net/pterodactylus/util/filter/Filters.java deleted file mode 100644 index d2bbb49..0000000 --- a/src/main/java/net/pterodactylus/util/filter/Filters.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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 - * The type of the list elements - * @param list - * The list to filter - * @param listFilter - * The list filter - * @return The filtered list - */ - public static List filteredList(List list, Filter listFilter) { - List filteredList = new ArrayList(); - 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 - * The type of the set elements - * @param set - * The set to filter - * @param setFilter - * The set filter - * @return The filtered set - */ - public static Set filteredSet(Set set, Filter setFilter) { - Set filteredSet = new HashSet(); - 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 - * The type of the map keys - * @param - * The type of the map values - * @param map - * The map to filter - * @param mapFilter - * The map filter - * @return The filtered map - */ - public static Map filteredMap(Map map, Filter> mapFilter) { - Map filteredMap = new HashMap(); - for (Entry 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 - * The type of the collection values - * @param collection - * The collection to filter - * @param collectionFilter - * The collection filter - * @return The filtered collection - */ - public static Collection filteredCollection(Collection collection, Filter collectionFilter) { - return filteredList(new ArrayList(collection), collectionFilter); - } - - /** - * Returns an iterator that contains only the elements from the given - * iterator that match the given filter. - * - * @param - * The type of the iterator elements - * @param iterator - * The iterator to filter - * @param iteratorFilter - * The iterator filter - * @return The filtered iterator - */ - public static Iterator filteredIterator(final Iterator iterator, final Filter iteratorFilter) { - return new Iterator() { - - 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"); - } - - }; - } - -} diff --git a/src/main/java/net/pterodactylus/util/thread/CurrentThreadExecutor.java b/src/main/java/net/pterodactylus/util/thread/CurrentThreadExecutor.java deleted file mode 100644 index 71d236d..0000000 --- a/src/main/java/net/pterodactylus/util/thread/CurrentThreadExecutor.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 . - */ - -package net.pterodactylus.util.thread; - -import java.util.concurrent.Executor; - -/** - * An {@link Executor} that executes {@link Runnable}s in the current thread. - * - * @author David ‘Bombe’ Roden - */ -public class CurrentThreadExecutor implements Executor { - - /** - * {@inheritDoc} - * - * @see java.util.concurrent.Executor#execute(java.lang.Runnable) - */ - public void execute(Runnable command) { - command.run(); - } - -} diff --git a/src/main/java/net/pterodactylus/util/thread/ObjectWrapper.java b/src/main/java/net/pterodactylus/util/thread/ObjectWrapper.java deleted file mode 100644 index a95ec21..0000000 --- a/src/main/java/net/pterodactylus/util/thread/ObjectWrapper.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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. - * - *
- * final ObjectWrapper<Object> objectWrapper = new ObjectWrapper<Object>();
- * new Runnable() {
- *     public void run() {
- *         ...
- *         objectWrapper.set(someResult);
- *     }
- * }.run();
- * Object result = objectWrapper.get();
- * 
- * - * @param - * The type of the wrapped object - * @author David ‘Bombe’ Roden <bombe@pterodactylus.net> - */ -public class ObjectWrapper { - - /** 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; - } - -}