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

jp.go.nict.langrid.commons.util.CollectionUtil Maven / Gradle / Ivy

Go to download

Common and utility library for the Service Grid Server Software and java web services.

The newest version!
/*
 * $Id: CollectionUtil.java 639 2013-02-19 10:23:21Z t-nakaguchi $
 *
 * This is a program for Language Grid Core Node. This combines multiple language resources and provides composite language services.
 * Copyright (C) 2005-2008 NICT Language Grid Project.
 *
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation, either version 2.1 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 Lesser 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see .
 */
package jp.go.nict.langrid.commons.util;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jp.go.nict.langrid.commons.transformer.TransformationException;
import jp.go.nict.langrid.commons.transformer.Transformer;
import jp.go.nict.langrid.commons.util.function.Predicate;
import jp.go.nict.langrid.commons.util.stream.IteratorProvider;
import jp.go.nict.langrid.commons.util.stream.Stream;

/**
 * 
 * 
 * @author Takao Nakaguchi
 * @author $Author: t-nakaguchi $
 * @version $Revision: 639 $
 */
public class CollectionUtil {
	/**
	 * 
	 * 
	 */
	public static  boolean equalsAsSet(
			Collection value1, Collection value2)
	{
		Set s1 = new HashSet();
		for(Object v : value1){
			s1.add(v);
		}
		Set s2 = new HashSet();
		for(Object v : value2){
			s2.add(v);
		}
		return s1.equals(s2);
	}

	/**
	 * 
	 * 
	 */
	public static  boolean equalsAsSet(
			Collection value1, Collection value2
			, Class clazz, String equalsMethodName)
		throws IllegalAccessException, InvocationTargetException
		, NoSuchMethodException
	{
		Method m = null;
		try{
			m = clazz.getMethod(equalsMethodName, clazz);
		} catch(NoSuchMethodException e){
			m = clazz.getMethod(equalsMethodName, Object.class);
		}
		return equalsAsSet(value1, value2, m);
	}

	/**
	 * 
	 * 
	 */
	public static  boolean equalsAsSet(
			Collection value1, Collection value2
			, Method equalsMethod)
		throws IllegalAccessException, InvocationTargetException
	{
		if(!equalsMethod.getReturnType().equals(boolean.class)){
			return false;
		}
		List s1 = new LinkedList();
		for(T v : value1){
			s1.add(v);
		}
		for(T v : value2){
			Iterator i2 = s1.iterator();
			boolean found = false;
			while(i2.hasNext()){
				if(((Boolean)equalsMethod.invoke(
						v, i2.next())).booleanValue())
				{
					i2.remove();
					found = true;
					break;
				}
			}
			if(!found) return false;
		}
		return s1.size() == 0;
	}

	/**
	 * 
	 * 
	 */
	public static  Map asMap(Pair... elements){
		Map map = new HashMap();
		for(Pair p : elements){
			map.put(p.getFirst(), p.getSecond());
		}
		return map;
	}

	/**
	 * 
	 * 
	 */
	public static  Map asMap(Map.Entry... elements){
		Map map = new HashMap();
		for(Map.Entry p : elements){
			map.put(p.getKey(), p.getValue());
		}
		return map;
	}

	/**
	 * 
	 * 
	 */
	public static  List collect(
			Iterator iterator, Transformer transformer)
		throws TransformationException
	{
		List r = new ArrayList();
		collect(iterator, r, transformer);
		return r;
	}

	/**
	 * 
	 * 
	 */
	public static  List collect(
			Iterable elements, Transformer transformer)
		throws TransformationException
	{
		List r = new ArrayList();
		collect(elements.iterator(), r, transformer);
		return r;
	}

	/**
	 * 
	 * 
	 */
	public static  void collect(
			Iterator iterator, Collection target, Transformer transformer)
		throws TransformationException
	{
		while(iterator.hasNext()){
			target.add(transformer.transform(iterator.next()));
		}
	}

	/**
	 * 
	 * 
	 */
	public static  T[] toArray(
			Collection collection, Class elementClass){
		return toArray(collection, elementClass, 0, collection.size());
	}

	/**
	 * 
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static  T[] toArray(
			Collection collection, Class elementClass, int index, int count){
		int n = collection.size();
		int end = Math.min(index + count, n);
		Object result = Array.newInstance(elementClass, end - index);
		Iterator it = collection.iterator();
		int resultIndex = 0;
		for(int i = 0; i < end; i++){
			if(!it.hasNext()) break;
			Object value = it.next();
			if(i < index) continue;
			Array.set(result, resultIndex, value);
			resultIndex++;
		}
		return (T[])result;
	}

	/**
	 * 
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static  Collection emptyCollection(){
		return Collections.EMPTY_LIST;
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	public static  Collection filter(Iterable collection, Predicate pred){
		List ret = new ArrayList();
		for(T v : collection){
			if(pred.test(v)) ret.add(v);
		}
		return ret;
	}

	public static  Stream stream(Iterable collection){
		return new Stream(new IteratorProvider(collection.iterator()));
	}
}