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

ch.inftec.ju.util.helper.FindHelper Maven / Gradle / Ivy

There is a newer version: 6.1-4
Show newest version
package ch.inftec.ju.util.helper;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import ch.inftec.ju.util.AssertUtil;
import ch.inftec.ju.util.JuCollectionUtils;
import ch.inftec.ju.util.JuRuntimeException;

/**
 * Helper class that wraps a collection and provides convenience methods to access
 * their elements.
 * @author [email protected]
 *
 * @param  Type of the elements
 */
public class FindHelper {
	private final Iterable items;
	
	public FindHelper(Iterable items) {
		this.items = items != null
				? items
				: Collections.emptyList();
	}
	
	/**
	 * Returns a list of all items of the collection.
	 * @return List of all items
	 */
	public List all() {
		return JuCollectionUtils.asList(this.items);
	}
	
	/**
	 * Returns exactly one item.
	 * 

* If none or more than one exist, an exception is thrown * @return One item */ public T one() { T one = this.oneOrNull(); AssertUtil.assertNotNull("No element available", one); return one; } /** * Returns one item or null if none exists. *

* If more than 1 exist, an exception is thrown * @return One item or null */ public T oneOrNull() { Iterator iterator = this.items.iterator(); if (iterator.hasNext()) { // At least one item in iterable T val = iterator.next(); // Make sure there are no more values... if (iterator.hasNext()) { throw new JuRuntimeException("More than 1 item available"); } else { return val; } } else { return null; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy