Created
March 25, 2012 12:31
-
-
Save bxt/2193282 to your computer and use it in GitHub Desktop.
Java utility class for comparable and collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.bxt.util; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Some generic methods on {@link Collection}s and {@link Comparable}s. | |
*/ | |
public abstract class Util { | |
/** | |
* Get a map containing the collections items as values and the object | |
* returned by an {@link Indexer} as key. | |
* @param collection Value list | |
* @param indexer Builds the keys for the values | |
* @return Map of the collection's items | |
*/ | |
public static <K,V> Map<K,V> indexBy(Collection<V> collection, | |
Indexer<K,V> indexer) { | |
Map<K, V> map = new HashMap<K, V>(); | |
for (V v : collection) { | |
map.put(indexer.index(v), v); | |
} | |
return map; | |
} | |
/** | |
* Builds keys for the values to be indexed | |
* @param <K> Key type | |
* @param <V> Value type | |
*/ | |
public interface Indexer<K,V> { | |
/** | |
* Key for a value | |
* @param v The value | |
* @return The corresponding key | |
*/ | |
public K index(V v); | |
} | |
/** | |
* Transform a collection by applying a {@link Mapper} to each item. | |
* @param ts Input items collection | |
* @param mapper Transformer | |
* @return Collection of transformed items | |
*/ | |
public static <T,U> List<U> map(Collection<T> ts, Mapper<T,U> mapper) { | |
List<U> result = new ArrayList<U>(); | |
for (T t : ts) { | |
result.add(mapper.map(t)); | |
} | |
return result; | |
} | |
/** | |
* Transform one value to another. | |
* @param <T> Source | |
* @param <U> Transformed | |
*/ | |
public interface Mapper<T,U> { | |
/** | |
* Transformation. | |
* @param t Input source value | |
* @return Output transformed value | |
*/ | |
public U map(T t); | |
} | |
/** | |
* Transform a collection by applying an {@link IndexMapper} to each item. | |
* @param ts Input items collection | |
* @param mapper Transformer | |
* @return Collection of transformed items | |
*/ | |
public static <T,U> List<U> map(Collection<T> ts, IndexMapper<T,U> mapper) { | |
List<U> result = new ArrayList<U>(ts.size()); | |
int i = 0; | |
for (T t : ts) { | |
result.add(mapper.map(i++, t)); | |
} | |
return result; | |
} | |
/** | |
* Transform one value to another using an index | |
* @param <T> Source | |
* @param <U> Transformed | |
*/ | |
public interface IndexMapper<T, U> { | |
/** | |
* Transformation | |
* @param i Index in input collection | |
* @param t Input source value | |
* @return Output transformed value | |
*/ | |
public U map(int i, T t); | |
} | |
/** | |
* Get the larger of two comparable objects. | |
* <p> | |
* If they are equal, the first is returned. This ensures that calling both, | |
* {@link #max(Comparable, Comparable)} and | |
* {@link #min(Comparable, Comparable)} returns always both objects. | |
* @param t First object | |
* @param u Second object | |
* @return Bigger object | |
*/ | |
public static <T extends Comparable<U>,U extends T> T max(T t, U u) { | |
if (t.compareTo(u) < 0) { | |
return u; | |
} else { | |
return t; | |
} | |
} | |
/** | |
* Get the smaller of two comparable objects. | |
* <p> | |
* If they are equal, the second is returned. This ensures that calling both, | |
* {@link #max(Comparable, Comparable)} and | |
* {@link #min(Comparable, Comparable)} returns always both objects. | |
* @param t First object | |
* @param u Second object | |
* @return Smaller object | |
*/ | |
public static <T extends Comparable<U>,U extends T> T min(T t, U u) { | |
if (t.compareTo(u) >= 0) { | |
return u; | |
} else { | |
return t; | |
} | |
} | |
/** | |
* Comparator sorting {@link Comparable}s inverted. | |
* @param <T> Type to sort | |
*/ | |
public static class InverseComparator<T extends Comparable<T>> | |
implements Comparator<T> { | |
private Comparator<? super T> comparator = null; | |
/** | |
* Invert natural order. | |
*/ | |
public InverseComparator() { | |
} | |
/** | |
* Invert given comparator. | |
* @param comparator Comparator to invert. | |
*/ | |
public InverseComparator(final Comparator<? super T> comparator) { | |
this.comparator = comparator; | |
} | |
@Override | |
public int compare(T t1, T t2) { | |
if (comparator != null) return comparator.compare(t1, t2); | |
return -t1.compareTo(t2); | |
} | |
} | |
/** | |
* Add the items of two collections into one list | |
* @param c1 Input collections whose items are first in the list | |
* @param c2 Input collections whose items follow in the list | |
* @return List of both collections' items | |
*/ | |
public static <T> List<T> concat(Collection<? extends T> c1, | |
Collection<? extends T> c2) { | |
List<T> merged = new ArrayList<T>(); | |
merged.addAll(c1); | |
merged.addAll(c2); | |
return merged; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment