diff --git a/source/net/yacy/cora/ranking/ScoreComparator.java b/source/net/yacy/cora/ranking/ScoreComparator.java new file mode 100644 index 000000000..ae3624af6 --- /dev/null +++ b/source/net/yacy/cora/ranking/ScoreComparator.java @@ -0,0 +1,57 @@ +/** + * ScoreComparator + * Copyright 2011 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany + * First released 23.08.2011 at http://yacy.net + * + * $LastChangedDate: 2011-03-08 02:51:51 +0100 (Di, 08 Mrz 2011) $ + * $LastChangedRevision: 7567 $ + * $LastChangedBy: low012 $ + * + * This library 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 library 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 in the file lgpl21.txt + * If not, see . + */ + +package net.yacy.cora.ranking; + +import java.util.Comparator; + +import net.yacy.cora.storage.ScoreMap; + +public class ScoreComparator> implements Comparator +{ + private final ScoreMap scoreMap; + private final boolean reverse; + + public ScoreComparator(final ScoreMap scoreMap, final boolean reverse) { + this.scoreMap = scoreMap; + this.reverse = reverse; + } + + @Override + public int compare(final E arg0, final E arg1) { + if ( arg0.equals(arg1) ) { + return 0; + } + final int i0 = this.scoreMap.get(arg0); + final int i1 = this.scoreMap.get(arg1); + if ( i0 < i1 ) { + return this.reverse ? 1 : -1; + } + if ( i0 > i1 ) { + return this.reverse ? -1 : 1; + } + return arg0.compareTo(arg1); + } + +}