krysalis-update/src/java/org/krysalis/depot/update/util/select AllSelector.java,NONE,1.1 ISelector.java,NONE,1.1 SelectionHelper.java,NONE,1.1 CompositeSelector.java,NONE,1.1 ISelectable.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:52 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/util/select
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/util/select

Added Files:
	AllSelector.java ISelector.java SelectionHelper.java 
	CompositeSelector.java ISelectable.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: SelectionHelper.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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 org.krysalis.depot.update.util.select;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.krysalis.depot.update.util.text.Messages;
import org.krysalis.depot.common.log.Logger;

/**
 * @author arb_jack
 */
public class SelectionHelper {

	/**
	 * Apply a "chain" (list) of selectors to a collection
	 * 
	 * @param choices
	 * @param selectors
	 * @return
	 */
	public static List select(List choices, List selectors) {
		List selected = new ArrayList();
		selectInto(choices, selectors, selected);
		return selected;
	}

	/**
	 * Apply a "chain" (list) of selectors to a collection
	 * 
	 * @param choices
	 * @param selectors
	 * @return
	 */
	public static void selectInto(
		List choices,
		List selectors,
		List selected) {
		//
		// Keep applying selectors (so long as there are choices)
		//
		for (Iterator i = selectors.iterator();
			i.hasNext() && !choices.isEmpty();
			) {
			ISelector selector = (ISelector) i.next();

			selectInto(choices, selector, selected);

			choices = selected;
			selected = new ArrayList();  // clean selection
		}
		
		selected = choices;
	}

	/**
	 * Select from a list of choices, into a new list
	 * 
	 * @param choices the original choices
	 * @param selector the selection mechanism
	 * @return  the list of selected choices
	 */
	public static List select(List choices, ISelector selector) {
		List selected = new ArrayList();
		selectInto(choices, selector, selected);
		return selected;
	}

	/**
	 * Select from a list of choices, into a list
	 * 
	 * @param choices the original choices
	 * @param selector the selection mechanism
	 * @param selected the selected list
	 */
	public static void selectInto(
		List choices,
		ISelector selector,
		List selected) {

		for (Iterator i = choices.iterator(); i.hasNext();) {
			Object choice = i.next();
			try {
				if (selector.select(choice))
					selected.add(choice);
				else
					Logger.getLogger().debug(
						Messages.getString(
							"NOT_SELECTED",
							new Object[] { choice, selector }));
			}
			catch (Exception e) {
				Logger.getLogger().error(
					Messages.getString(
						"SELECTOR_CRASHED",
						new Object[] { selector, e }),
					e);
			}
		}
	}
}

--- NEW FILE: ISelectable.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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 org.krysalis.depot.update.util.select;

/**
 * @author arb_jack
 */
public interface ISelectable {
	Object getSelectionObject();
}

--- NEW FILE: ISelector.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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 org.krysalis.depot.update.util.select;

/**
 * @author arb_jack
 */
public interface ISelector {
	boolean select(Object o) throws Exception;
}

--- NEW FILE: AllSelector.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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 org.krysalis.depot.update.util.select;


/**
 * @author arb_jack
 */
public class AllSelector implements ISelector {

	private final static ISelector INSTANCE = new AllSelector();

	/* (non-Javadoc)
	 * @see org.apache.ruper.util.Selector#select(java.lang.Object)
	 */
	public boolean select(Object o) {
		return true;
	}

	public static ISelector getInstance() {
		return AllSelector.INSTANCE;
	}
	
	public String toString() { return Boolean.TRUE.toString(); }
}

--- NEW FILE: CompositeSelector.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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 org.krysalis.depot.update.util.select;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.krysalis.depot.common.util.SystemUtils;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;

public abstract class CompositeSelector implements ISelector, Dumpable {
	// Would be much easier if we could use a LinkedHashSet, but that
	// isn't available until JDK 1.4
	private List m_children = new LinkedList();

	/**
	 * Add a selector to this composite if it is not already present.
	 * If selector has already been added, the add will fail (and
	 * return false).
	 * @param selector Selector to add
	 * @return true if this composite did not already contain the selector
	 * @throws NullPointerException If selector is null
	 */
	public boolean addSelector(ISelector selector) {
		if (selector == null) {
			throw new IllegalArgumentException("Can't add null selector to composite");
		}
		boolean wasAdded;
		if (m_children.contains(selector)) {
			wasAdded = false;
		}
		else {
			wasAdded = m_children.add(selector);
		}
		return wasAdded;
	}

	/**
	 * Get the number of children
	 * @return Number of children
	 */
	public int childCount() {
		return m_children.size();
	}

	/**
	 * Get an iterator to access children
	 * @return Iterator that iterates over children
	 */
	public Iterator childIterator() {
		return m_children.iterator();
	}

	/**
	 * Check if this selector has children
	 * @return true if it has children
	 */
	public boolean hasChildren() {
		return !m_children.isEmpty();
	}

	/**
	 * Get the name of this type of selector.  This is used for
	 * debug output.
	 * By default will return type name based on class name
	 *
	 * @return Selector type name.
	 */
	public String getTypeName() {
		String name = SystemUtils.getShortName(this);
		String selectorSuffix = "Selector";
		if (name.endsWith(selectorSuffix)) {
			name = name.substring(0, name.length() - selectorSuffix.length());
		}
		return name;
	}

	/**
	 * Override of equals method (@see java.lang.Object#equals).
	 * CompositeSelectors are equal if they are of the same
	 * type and have the same children (number and order).
	 * @param obj Object compare with
	 * @return True if objects are equal
	 */
	public boolean equals(Object obj) {
		boolean isEqual = false;

		if (sameType(obj)) {
			CompositeSelector rhs = (CompositeSelector) obj;
			if (childCount() == rhs.childCount()) {
				Iterator lhsIter = childIterator();
				Iterator rhsIter = rhs.childIterator();
				isEqual = true;
				while (lhsIter.hasNext() && rhsIter.hasNext() && isEqual) {
					Object lhsChild = lhsIter.next();
					Object rhsChild = rhsIter.next();
					isEqual = lhsChild.equals(rhsChild);
				}
			}
		}
		return isEqual;
	}

	public int hashCode() {
		int typeHash = getType().hashCode();
		return typeHash * 41 + m_children.hashCode();
	}

	public void dump(PrintWriter out, int depth, boolean verbose) {
		String indent = DebugUtils.getIndent(depth);
		out.print(indent);
		out.print(getTypeName());
		out.println(" (");
		Iterator iter = childIterator();
		while (iter.hasNext()) {
			ISelector selector = (ISelector) iter.next();
			DebugUtils.dump(out, depth + 1, verbose, selector);
		}

		out.print(indent);
		out.println(")");

	}
	public String toString() {
		StringBuffer buffer = new StringBuffer();

		buffer.append(getTypeName());

		buffer.append(" (");
		Iterator iter = childIterator();
		while (iter.hasNext()) {
			ISelector selector = (ISelector) iter.next();
			buffer.append(selector);
		}

		buffer.append(")");

		return buffer.toString();
	}

	/**
	 * Determine if object is same type of selector
	 * @param obj Object to check
	 * @return true if the types are the same
	 */
	private boolean sameType(Object obj) {
		boolean isSame = false;
		if (obj instanceof CompositeSelector) {
			CompositeSelector rhs = (CompositeSelector) obj;
			return getType().equals(rhs.getType());
		}
		return isSame;
	}

	/**
	 * Get an object representing the selector type
	 * @return Type object
	 */
	private Object getType() {
		return getClass();
	}
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/