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

net.sf.gluebooster.java.booster.basic.container.IteratorOfContainer Maven / Gradle / Ivy

Go to download

Basic classes to support the development of applications. There should be as few dependencies on other frameworks as possible.

The newest version!
package net.sf.gluebooster.java.booster.basic.container;

import java.io.BufferedReader;
import java.util.Iterator;

import net.sf.gluebooster.java.booster.basic.transformation.CallableClassConverter;
import net.sf.gluebooster.java.booster.essentials.container.IteratorWithIterable;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.meta.objects.ObjectHolder;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;
import net.sf.gluebooster.java.booster.essentials.utils.ThrowableBoostUtils;

/**
 * Iterates over multiple classes. Simple converts should be done with the CallableClassConverter.
 * 
 * @defaultParamText name the name of the iterator
 * @defaultParamText type the type of the iterator
 * @author cbauer
 *
 */
public class IteratorOfContainer extends IteratorWithIterable {


	/**
	 * The filter of objects (result false: ignore the object)
	 */
	private Callable filter;

	/**
	 * 
	 * @param iterable
	 *            the container
	 */
	private IteratorOfContainer(Object name, Object type, Object iterable) {
		super(name, type, iterable);
	}

	/**
	 * Iterates over the lines of a text
	 * 
	 * @param iterable
	 *            (the container with) the text
	 * @return the created iterator
	 */
	public static IteratorOfContainer createLineIterator(Object name, Object iterable) throws Exception {
		IteratorOfContainer result = new IteratorOfContainer(name, Constants.LINE, CallableClassConverter.TO_BUFFEREDREADER.call(iterable));
		return result;
	}

	/**
	 * Filters another container. The container/source must be set later
	 * 
	 * @param source
	 *            the container
	 * @param filter
	 *            TRUE: keep the object of the source, FALSE: ignore the object of the source
	 * @return the created iterator
	 */
	public static IteratorOfContainer createFilteringIterator(Object name, Iterator source, Callable filter) throws Exception {
		IteratorOfContainer result = new IteratorOfContainer(name, Constants.FILTER, source);
		result.filter = filter;
		return result;
	}

	/**
	 * Filters another container. The container/source must be set later
	 * 
	 * @param filter
	 *            filters the container
	 * @return the created iterator
	 */
	public static IteratorOfContainer createFilteringIterator(Object name, Callable filter) throws Exception {
		return createFilteringIterator(name, null, filter);
	}

	@Override
	protected ObjectHolder computeNextObject() throws Exception {
		Object type = getType();
		if (Constants.LINE.is(type)) {
			String nextLine = ((BufferedReader) getIterable()).readLine();
				if (nextLine == null) {
				return null;
				} else {
				return ObjectHolder.createObjectHolder(nextLine);
				}
			} else if (Constants.FILTER.is(type)) {
			Iterator source = getIterable();
			while (true) {
					if (!source.hasNext()) {
					return null;
					} else {
						Object nextElement = source.next();
					if (Boolean.TRUE.equals(filter.call(nextElement))) {
						return ObjectHolder.createObjectHolder(nextElement);
						}
					}
				}

			} else {
				throw ThrowableBoostUtils.createNotImplementedException();
			}
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy