Created
February 14, 2017 08:24
-
-
Save Satomaru/b44f392536fde00f1fd65053301e3a70 to your computer and use it in GitHub Desktop.
単純なコンパレーターです。
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 tools.util; | |
import java.util.Comparator; | |
import java.util.function.Function; | |
/** | |
* 単純なコンパレーターです。 | |
* | |
* @param <T> ソート対象 | |
* @param <C> ソートに用いる値の型 | |
*/ | |
public class SimpleComparator<T, C extends Comparable<C>> implements Comparator<T> { | |
/** ソートに用いる値を取得する関数。 */ | |
private final Function<T, C> comparableGetter; | |
/** | |
* コンストラクタ。 | |
* | |
* @param comparableGetter ソートに用いる値を取得する関数 | |
*/ | |
public SimpleComparator(Function<T, C> comparableGetter) { | |
this.comparableGetter = comparableGetter; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public int compare(T o1, T o2) { | |
C comparable1 = comparableGetter.apply(o1); | |
C comparable2 = comparableGetter.apply(o2); | |
return comparable1.compareTo(comparable2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment