Basic comparator simplifications.

This commit is contained in:
Michael Pope 2015-10-07 09:25:00 +10:30 committed by Mike Pope
parent daeeaef04b
commit 010064f103
11 changed files with 77 additions and 133 deletions

View File

@ -61,13 +61,8 @@ public class ConceptDetailPanel extends FreeColPanel
};
private static final Comparator<DefaultMutableTreeNode> nodeComparator
= new Comparator<DefaultMutableTreeNode>() {
@Override
public int compare(DefaultMutableTreeNode node1, DefaultMutableTreeNode node2) {
return ((ColopediaTreeItem) node1.getUserObject()).getText()
.compareTo(((ColopediaTreeItem) node2.getUserObject()).getText());
}
};
= Comparator.comparing(tn ->
((ColopediaTreeItem)tn.getUserObject()).getText());
private ColopediaPanel colopediaPanel;

View File

@ -54,7 +54,7 @@ public class NewUnitPanel extends FreeColPanel {
/** The unit types corresponding to the buttons. */
private final List<UnitType> units = new ArrayList<>();
/** A comparator for unit prices. */
/** A comparator by ascending unit price. */
private final Comparator<UnitType> priceComparator;
/** Is there at least one available unit? */
@ -73,16 +73,11 @@ public class NewUnitPanel extends FreeColPanel {
String label, List<UnitType> units) {
super(freeColClient, layout);
final Europe europe = getMyPlayer().getEurope();
this.question = new JLabel(label);
this.units.addAll(units);
this.priceComparator = new Comparator<UnitType>() {
@Override
public int compare(final UnitType u1, final UnitType u2) {
return europe.getUnitPrice(u1) - europe.getUnitPrice(u2);
}
};
final Europe europe = getMyPlayer().getEurope();
this.priceComparator = Comparator.comparingInt((UnitType ut) ->
europe.getUnitPrice(ut));
okButton.setText(Messages.message("close"));

View File

@ -62,12 +62,7 @@ public final class TradeRoutePanel extends FreeColPanel {
/** Compare trade routes by name. */
private static final Comparator<TradeRoute> tradeRouteComparator
= new Comparator<TradeRoute>() {
@Override
public int compare(TradeRoute r1, TradeRoute r2) {
return r1.getName().compareTo(r2.getName());
}
};
= Comparator.comparing(TradeRoute::getName);
/** The unit to assign/deassign trade routes for. */
private final Unit unit;

View File

@ -32,12 +32,9 @@ import java.util.Set;
*/
public interface Consumer {
public static final Comparator<Consumer> COMPARATOR = new Comparator<Consumer>() {
@Override
public int compare(Consumer c1, Consumer c2) {
return c2.getPriority() - c1.getPriority();
}
};
/** Compare consumers by descending priority. */
public static final Comparator<Consumer> COMPARATOR
= Comparator.comparingInt(Consumer::getPriority).reversed();
/**
* Default consumption priority for the Colony when producing new

View File

@ -64,42 +64,8 @@ public class Role extends BuildableType {
* effectiveness.
*/
public static final Comparator<Role> militaryComparator
= new Comparator<Role>() {
@Override
public int compare(Role role1, Role role2) {
double amount1 = role1.getOffence() + role1.getDefence();
double amount2 = role2.getOffence() + role2.getDefence();
return (amount1 < amount2) ? 1
: (amount1 > amount2) ? -1
: 0;
}
};
/** A comparator to sort roles by descending defensive power. */
public static final Comparator<Role> defensiveComparator
= new Comparator<Role>() {
@Override
public int compare(Role role1, Role role2) {
double defence1 = role1.getDefence();
double defence2 = role2.getDefence();
return (defence1 > defence2) ? 1
: (defence1 < defence2) ? -1
: 0;
}
};
/** A comparator to sort roles by descending offensive power. */
public static final Comparator<Role> offensiveComparator
= new Comparator<Role>() {
@Override
public int compare(Role role1, Role role2) {
double offence1 = role1.getOffence();
double offence2 = role2.getOffence();
return (offence1 > offence2) ? 1
: (offence1 < offence2) ? -1
: 0;
}
};
= Comparator.comparingDouble((Role r) ->
r.getOffence() + r.getDefence()).reversed();
/**
* The Role to downgrade to after losing a battle. Defaults to

View File

@ -51,39 +51,18 @@ public class CollectionUtils {
= (d1, d2) -> d1 + d2;
/** Useful comparators for mapEntriesBy* */
public static final Comparator<Integer> descendingIntegerComparator
= new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i2 - i1;
}
};
public static final Comparator<Integer> ascendingIntegerComparator
= new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i1 - i2;
}
};
public static final Comparator<Double> descendingDoubleComparator
= new Comparator<Double>() {
public int compare(Double d1, Double d2) {
return (d2 > d1) ? 1 : (d2 < d1) ? -1 : 0;
}
};
= Comparator.comparingInt(i -> i);
public static final Comparator<Integer> descendingIntegerComparator
= ascendingIntegerComparator.reversed();
public static final Comparator<Double> ascendingDoubleComparator
= new Comparator<Double>() {
public int compare(Double d1, Double d2) {
return (d1 > d2) ? 1 : (d1 < d2) ? -1 : 0;
}
};
/** Comparator to order lists by decreasing length. */
public static final Comparator<List<?>> listLengthComparator
= new Comparator<List<?>>() {
@Override
public int compare(List<?> l1, List<?> l2) {
return l2.size() - l1.size();
}
};
= Comparator.comparingDouble(d -> d);
public static final Comparator<Double> descendingDoubleComparator
= ascendingDoubleComparator.reversed();
public static final Comparator<List<?>> ascendingListLengthComparator
= Comparator.comparingInt(l -> l.size());
public static final Comparator<List<?>> descendingListLengthComparator
= ascendingListLengthComparator.reversed();
/**
* Make an unmodifiable set with specified members.

View File

@ -36,14 +36,9 @@ import org.w3c.dom.Element;
*/
public abstract class ValuedAIObject extends AIObject {
/** A comparator for the AI object value. */
/** A comparator by descending AI object value. */
public static final Comparator<ValuedAIObject> valuedComparator
= new Comparator<ValuedAIObject>() {
@Override
public int compare(ValuedAIObject v1, ValuedAIObject v2) {
return v2.getValue() - v1.getValue();
}
};
= Comparator.comparingInt(ValuedAIObject::getValue).reversed();
/** The value of this AIObject. */
private int value;

View File

@ -59,6 +59,10 @@ import org.w3c.dom.NamedNodeMap;
*/
public class ChangeSet {
/** Compare changes by ascending priority. */
private static final Comparator<Change> changeComparator
= Comparator.comparingInt(Change::getPriority);
// Convenient way to specify the relative priorities of the fixed
// change types in one place.
public static enum ChangePriority {
@ -86,13 +90,6 @@ public class ChangeSet {
private final ArrayList<Change> changes;
private static final Comparator<Change> changeComparator
= new Comparator<Change>() {
@Override
public int compare(final Change c1, final Change c2) {
return c1.getPriority() - c2.getPriority();
}
};
/**
* Class to control the visibility of a change.

View File

@ -557,7 +557,7 @@ public class SimpleMapGenerator implements MapGenerator {
// For each missing skill...
while (!expertsNeeded.isEmpty()) {
UnitType neededSkill = expertsNeeded.remove(0);
Collections.sort(isList, listLengthComparator);
Collections.sort(isList, descendingListLengthComparator);
List<IndianSettlement> extras = isList.remove(0);
UnitType extraSkill = extras.get(0).getLearnableSkill();
List<RandomChoice<IndianSettlement>> choices = new ArrayList<>();

View File

@ -47,7 +47,7 @@ public class RoleTest extends FreeColTestCase {
= spec().getRole("model.role.dragoon");
private static final Role pioneer
= spec().getRole("model.role.pioneer");
private static final Role missionary
private static final Role mission
= spec().getRole("model.role.missionary");
private static final Role infantry
= spec().getRole("model.role.infantry");
@ -68,48 +68,46 @@ public class RoleTest extends FreeColTestCase {
= spec().getUnitType("model.unit.kingsRegular");
public void testComparators() {
public void testRoleComparator() {
List<Role> roles = new ArrayList<>();
roles.add(soldier);
roles.add(dragoon);
roles.add(missionary);
roles.add(mission);
Collections.sort(roles);
assertEquals(dragoon, roles.get(0));
assertEquals(soldier, roles.get(1));
assertEquals(missionary, roles.get(2));
assertEquals(mission, roles.get(2));
assertEquals(0, Role.defensiveComparator.compare(soldier, soldier));
assertEquals(0, Role.defensiveComparator.compare(dragoon, dragoon));
assertEquals(0, Role.defensiveComparator.compare(missionary, missionary));
assertEquals(1, Role.defensiveComparator.compare(dragoon, soldier));
assertEquals(-1, Role.defensiveComparator.compare(soldier, dragoon));
assertEquals(-1, Role.defensiveComparator.compare(missionary, soldier));
assertEquals(-1, Role.defensiveComparator.compare(missionary, dragoon));
assertEquals(0, Role.militaryComparator.compare(soldier, soldier));
assertEquals(0, Role.militaryComparator.compare(dragoon, dragoon));
assertEquals(0, Role.militaryComparator.compare(mission, mission));
assertEquals(-1, Role.militaryComparator.compare(dragoon, soldier));
assertEquals(1, Role.militaryComparator.compare(soldier, dragoon));
assertEquals(1, Role.militaryComparator.compare(mission, soldier));
assertEquals(-1, Role.militaryComparator.compare(dragoon, mission));
}
public void testCompatibleRoles() {
assertFalse(soldier.isCompatibleWith(none));
assertFalse(soldier.isCompatibleWith(pioneer));
assertFalse(soldier.isCompatibleWith(missionary));
assertFalse(soldier.isCompatibleWith(mission));
assertTrue(soldier.isCompatibleWith(soldier));
assertFalse(soldier.isCompatibleWith(scout));
assertTrue(soldier.isCompatibleWith(dragoon));
assertFalse(missionary.isCompatibleWith(none));
assertFalse(missionary.isCompatibleWith(pioneer));
assertTrue(missionary.isCompatibleWith(missionary));
assertFalse(missionary.isCompatibleWith(soldier));
assertFalse(missionary.isCompatibleWith(scout));
assertFalse(missionary.isCompatibleWith(dragoon));
assertFalse(mission.isCompatibleWith(none));
assertFalse(mission.isCompatibleWith(pioneer));
assertTrue(mission.isCompatibleWith(mission));
assertFalse(mission.isCompatibleWith(soldier));
assertFalse(mission.isCompatibleWith(scout));
assertFalse(mission.isCompatibleWith(dragoon));
}
public void testGoodsDifference() {
assertTrue(Role.getGoodsDifference(null, 1, none, 1).isEmpty());
assertTrue(Role.getGoodsDifference(none, 1, none, 1).isEmpty());
assertTrue(Role.getGoodsDifference(none, 1, missionary, 1).isEmpty());
assertTrue(Role.getGoodsDifference(missionary, 1, none, 1).isEmpty());
assertTrue(Role.getGoodsDifference(none, 1, mission, 1).isEmpty());
assertTrue(Role.getGoodsDifference(mission, 1, none, 1).isEmpty());
List<AbstractGoods> goods
= Role.getGoodsDifference(none, 1, soldier, 1);
@ -120,7 +118,7 @@ public class RoleTest extends FreeColTestCase {
checkGoods("soldier->dragoon", goods,
new AbstractGoods(horses, 50));
goods = Role.getGoodsDifference(missionary, 1, dragoon, 1);
goods = Role.getGoodsDifference(mission, 1, dragoon, 1);
checkGoods("missionary->dragoon", goods,
new AbstractGoods(horses, 50),
new AbstractGoods(muskets, 50));

View File

@ -20,6 +20,7 @@
package net.sf.freecol.common.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.sf.freecol.common.util.CollectionUtils;
@ -54,4 +55,30 @@ public class UtilsTest extends FreeColTestCase {
assertEquals(p.get(4), makeList(3,1,2));
assertEquals(p.get(5), makeList(3,2,1));
}
public void testComparator() {
// This is more to prove that I know what I am doing with some
// trivial comparators than to actually test the code:-), MTP.
List<Double> d = new ArrayList<>();
d.add(1.0);
d.add(2.0);
d.add(3.0);
Collections.sort(d, CollectionUtils.descendingDoubleComparator);
assertEquals(d.get(0), 3.0);
Collections.sort(d, CollectionUtils.ascendingDoubleComparator);
assertEquals(d.get(0), 1.0);
List<List<Object>> o = new ArrayList<>();
List<Object> o1 = new ArrayList<Object>();
List<Object> o2 = new ArrayList<Object>();
List<Object> o3 = new ArrayList<Object>();
o.add(o1);
o.add(o2);
o.add(o3);
o1.add(1);
o2.add(1);o2.add(2);
o3.add(1);o3.add(2);o3.add(3);
Collections.sort(o, CollectionUtils.descendingListLengthComparator);
assertEquals(o.get(0), o3);
}
}