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

at.molindo.utils.collections.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/**
 * Copyright 2010 Molindo GmbH
 *
 * 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 at.molindo.utils.collections;

import java.util.ArrayList;
import java.util.Arrays;
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 java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap;

import at.molindo.utils.data.Function;

public class CollectionUtils {

	private CollectionUtils() {
	}

	public static  Set unmodifiableSet(Iterable e) {
		return Collections.unmodifiableSet(set(e));
	}

	public static  Set unmodifiableSet(E... e) {
		return Collections.unmodifiableSet(set(e));
	}

	public static  Set transformUnmodifiableSet(Iterable e, Function f) {
		return Collections.unmodifiableSet(transformSet(e, f));
	}

	public static  HashSet set(E... e) {
		return set(Arrays.asList(e));
	}

	public static  HashSet set(Iterable e) {
		return IteratorUtils.addAll(new HashSet(), IteratorUtils.iterator(e));
	}

	public static  HashSet transformSet(Iterable e, Function f) {
		return IteratorUtils.addAll(new HashSet(), IteratorUtils.transform(IteratorUtils.iterator(e), f));
	}

	public static  SortedSet unmodifiableSortedSet(E... e) {
		return Collections.unmodifiableSortedSet(sortedSet(e));
	}

	public static  SortedSet unmodifiableSortedSet(Iterable e) {
		return Collections.unmodifiableSortedSet(sortedSet(e));
	}

	public static  SortedSet transformUnmodifiableSortedSet(Iterable e, Function f) {
		return Collections.unmodifiableSortedSet(transformSortedSet(e, f));
	}

	public static  TreeSet sortedSet(E... e) {
		return sortedSet(Arrays.asList(e));
	}

	public static  TreeSet sortedSet(Iterable e) {
		return IteratorUtils.addAll(new TreeSet(), IteratorUtils.iterator(e));
	}

	public static  TreeSet transformSortedSet(Iterable e, Function f) {
		return IteratorUtils.addAll(new TreeSet(), IteratorUtils.transform(IteratorUtils.iterator(e), f));
	}

	public static  List unmodifiableList(E... e) {
		return Collections.unmodifiableList(list(e));
	}

	public static  List unmodifiableList(Iterable e) {
		return Collections.unmodifiableList(list(e));
	}

	public static  List transformUnmodifiableList(Iterable e, Function f) {
		return Collections.unmodifiableList(transformList(e, f));
	}

	public static  ArrayList list(E... e) {
		return new ArrayList(Arrays.asList(e));
	}

	public static  ArrayList list(Iterable e) {
		return IteratorUtils.addAll(new ArrayList(), IteratorUtils.iterator(e));
	}

	public static  ArrayList transformList(Iterable e, Function f) {
		return IteratorUtils.addAll(new ArrayList(), IteratorUtils.transform(IteratorUtils.iterator(e), f));
	}

	public static  HashMap mapKeys(Iterable e, Function f) {
		return IteratorUtils.putKeys(new HashMap(), IteratorUtils.iterator(e), f);
	}

	public static  HashMap mapValues(Iterable e, Function f) {
		return IteratorUtils.putValues(new HashMap(), IteratorUtils.iterator(e), f);
	}

	/**
	 * a sublist implementation that is diffrent from
	 * {@link List#subList(int, int)} as it handles out of bounds indexes
	 * gracefully
	 */
	public static  List subList(final List list, final int first, final int count) {
		if (list.size() < first) {
			return new ArrayList(0);
		}
		return list.subList(first, Math.min(first + count, list.size()));
	}

	public static boolean empty(Collection c) {
		return c == null || c.isEmpty();
	}

	public static boolean empty(Map c) {
		return c == null || c.isEmpty();
	}

	public static  T first(Iterable c) {
		return c == null ? null : IteratorUtils.next(IteratorUtils.iterator(c));
	}

	public static  Map.Entry first(Map c) {
		return c == null ? null : first(c.entrySet());
	}

	public static  K firstKey(Map c) {
		return c == null ? null : first(c.keySet());
	}

	public static  V firstValue(Map c) {
		return c == null ? null : first(c.values());
	}

	public static  V putIfAbsent(ConcurrentMap map, K key, V value) {
		V current = map.putIfAbsent(key, value);
		return current != null ? current : value;
	}

	public static  T find(Collection c, Function f) {
		for (T t : c) {
			if (f.apply(t) == Boolean.TRUE) {
				return t;
			}
		}
		return null;
	}

	public static  List findAll(Collection c, Function f) {
		List list = new LinkedList();
		for (T t : c) {
			if (f.apply(t) == Boolean.TRUE) {
				list.add(t);
			}
		}
		return list;
	}

	public static  T find(Collection c, Function f, V match) {
		for (T t : c) {
			if (match.equals(f.apply(t))) {
				return t;
			}
		}
		return null;
	}

	public static  List findAll(Collection c, Function f, V match) {
		List list = new LinkedList();
		for (T t : c) {
			if (match.equals(f.apply(t))) {
				list.add(t);
			}
		}
		return list;
	}

	public static , T, F> C add(C to, F o, Function f) {
		to.add(f.apply(o));
		return to;
	}

	public static , T, F> C addAll(C to, Collection from, Function f) {
		for (F o : from) {
			to.add(f.apply(o));
		}
		return to;
	}

	public static , T, F> M put(M to, F o, Function f) {
		to.put(f.apply(o), o);
		return to;
	}

	public static , T, F> M putAll(M to, Collection from, Function f) {
		for (F o : from) {
			to.put(f.apply(o), o);
		}
		return to;
	}

	public static  List resize(List list, int size) {
		return resize(list, size, null, null);
	}

	public static  List resize(List list, int size, T defaultValue) {
		return resize(list, size, defaultValue, null);
	}

	public static  List resize(List list, int size, T defaultValue, Collection obsolete) {
		int currentSize = list.size();
		while (currentSize > size) {
			T old = list.remove(--currentSize);
			if (old != null && obsolete != null) {
				obsolete.add(old);
			}
		}
		while (currentSize++ < size) {
			list.add(defaultValue);
		}
		return list;
	}

	/**
	 * set index of list to obj, resizing list if necessary
	 * 
	 * @param 
	 * @param list
	 * @param idx
	 * @param obj
	 * @return
	 */
	public static  List set(List list, int idx, T obj) {
		return set(list, idx, obj, null);
	}

	public static  List set(List list, int idx, T obj, T defaultValue) {
		int currentSize = list.size();
		while (currentSize++ <= idx) {
			list.add(defaultValue);
		}
		list.set(idx, obj);
		return list;
	}

	public static  void remove(Iterable collection, Function function) {
		Iterator iter = collection.iterator();
		while (iter.hasNext()) {
			if (function.apply(iter.next())) {
				iter.remove();
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy