@Override public HardSoftScore calculateScore() { return HardSoftScore.of(hardScore, softScore); }
@Test public void compareTo() { PlannerAssert.assertCompareToOrder( HardSoftScore.ofUninitialized(-8, 0, 0), HardSoftScore.ofUninitialized(-7, -20, -20), HardSoftScore.ofUninitialized(-7, -1, -300), HardSoftScore.ofUninitialized(-7, 0, 0), HardSoftScore.ofUninitialized(-7, 0, 1), HardSoftScore.of(-20, Integer.MIN_VALUE), HardSoftScore.of(-20, -20), HardSoftScore.of(-1, -300), HardSoftScore.of(-1, 4000), HardSoftScore.of(0, -1), HardSoftScore.of(0, 0), HardSoftScore.of(0, 1) ); }
@Test public void parseScore() { assertEquals(HardSoftScore.of(-147, -258), HardSoftScore.parseScore("-147hard/-258soft")); assertEquals(HardSoftScore.ofUninitialized(-7, -147, -258), HardSoftScore.parseScore("-7init/-147hard/-258soft")); assertEquals(HardSoftScore.of(-147, Integer.MIN_VALUE), HardSoftScore.parseScore("-147hard/*soft")); }
@Override public int compare(Score s1, Score s2) { HardSoftScore score1 = (HardSoftScore) s1; HardSoftScore score2 = (HardSoftScore) s2; long score1Side = (long) score1.getHardScore() * (long) hardWeight + (long) score1.getSoftScore(); long score2Side = (long) score2.getHardScore() * (long) hardWeight + (long) score2.getSoftScore(); return score1Side < score2Side ? -1 : (score1Side == score2Side ? 0 : 1); }
@Override public HardSoftScore add(HardSoftScore augment) { return new HardSoftScore( initScore + augment.getInitScore(), hardScore + augment.getHardScore(), softScore + augment.getSoftScore()); }
public static HardSoftScore parseScore(String scoreString) { String[] scoreTokens = parseScoreTokens(HardSoftScore.class, scoreString, HARD_LABEL, SOFT_LABEL); int initScore = parseInitScore(HardSoftScore.class, scoreString, scoreTokens[0]); int hardScore = parseLevelAsInt(HardSoftScore.class, scoreString, scoreTokens[1]); int softScore = parseLevelAsInt(HardSoftScore.class, scoreString, scoreTokens[2]); return ofUninitialized(initScore, hardScore, softScore); }
@Test public void serializeAndDeserialize() { PlannerTestUtils.serializeAndDeserializeWithAll( HardSoftScore.of(-12, 3400), output -> { assertEquals(0, output.getInitScore()); assertEquals(-12, output.getHardScore()); assertEquals(3400, output.getSoftScore()); } ); PlannerTestUtils.serializeAndDeserializeWithAll( HardSoftScore.ofUninitialized(-7, -12, 3400), output -> { assertEquals(-7, output.getInitScore()); assertEquals(-12, output.getHardScore()); assertEquals(3400, output.getSoftScore()); } ); }
@Override public HardSoftScore buildOptimisticBound(InitializingScoreTrend initializingScoreTrend, HardSoftScore score) { InitializingScoreTrendLevel[] trendLevels = initializingScoreTrend.getTrendLevels(); return HardSoftScore.ofUninitialized(0, trendLevels[0] == InitializingScoreTrendLevel.ONLY_DOWN ? score.getHardScore() : Integer.MAX_VALUE, trendLevels[1] == InitializingScoreTrendLevel.ONLY_DOWN ? score.getSoftScore() : Integer.MAX_VALUE); }
@Test public void subtract() { assertEquals(HardSoftScore.of(21, 280), HardSoftScore.of(20, -20).subtract( HardSoftScore.of(-1, -300))); assertEquals(HardSoftScore.ofUninitialized(-63, 21, 280), HardSoftScore.ofUninitialized(-70, 20, -20).subtract( HardSoftScore.ofUninitialized(-7, -1, -300))); }
@Test public void add() { assertEquals(HardSoftScore.of(19, -320), HardSoftScore.of(20, -20).add( HardSoftScore.of(-1, -300))); assertEquals(HardSoftScore.ofUninitialized(-77, 19, -320), HardSoftScore.ofUninitialized(-70, 20, -20).add( HardSoftScore.ofUninitialized(-7, -1, -300))); }
@Test public void multiply() { assertEquals(HardSoftScore.of(6, -6), HardSoftScore.of(5, -5).multiply(1.2)); assertEquals(HardSoftScore.of(1, -2), HardSoftScore.of(1, -1).multiply(1.2)); assertEquals(HardSoftScore.of(4, -5), HardSoftScore.of(4, -4).multiply(1.2)); assertEquals(HardSoftScore.ofUninitialized(-14, 8, -10), HardSoftScore.ofUninitialized(-7, 4, -5).multiply(2.0)); }
@Test public void divide() { assertEquals(HardSoftScore.of(5, -5), HardSoftScore.of(25, -25).divide(5.0)); assertEquals(HardSoftScore.of(4, -5), HardSoftScore.of(21, -21).divide(5.0)); assertEquals(HardSoftScore.of(4, -5), HardSoftScore.of(24, -24).divide(5.0)); assertEquals(HardSoftScore.ofUninitialized(-7, 4, -5), HardSoftScore.ofUninitialized(-14, 8, -10).divide(2.0)); }
@Test public void power() { assertEquals(HardSoftScore.of(16, 25), HardSoftScore.of(-4, 5).power(2.0)); assertEquals(HardSoftScore.of(4, 5), HardSoftScore.of(16, 25).power(0.5)); assertEquals(HardSoftScore.ofUninitialized(-343, 64, 125), HardSoftScore.ofUninitialized(-7, 4, 5).power(3.0)); }
@Test public void toInitializedScore() { assertEquals(HardSoftScore.of(-147, -258), HardSoftScore.of(-147, -258).toInitializedScore()); assertEquals(HardSoftScore.of(-147, -258), HardSoftScore.ofUninitialized(-7, -147, -258).toInitializedScore()); }
@Test public void toShortString() { assertEquals("0", HardSoftScore.of(0, 0).toShortString()); assertEquals("-258soft", HardSoftScore.of(0, -258).toShortString()); assertEquals("-147hard", HardSoftScore.of(-147, 0).toShortString()); assertEquals("-147hard/-258soft", HardSoftScore.of(-147, -258).toShortString()); assertEquals("-7init", HardSoftScore.ofUninitialized(-7, 0, 0).toShortString()); assertEquals("-7init/-258soft", HardSoftScore.ofUninitialized(-7, 0, -258).toShortString()); assertEquals("-7init/-147hard/-258soft", HardSoftScore.ofUninitialized(-7, -147, -258).toShortString()); }
@Test public void withInitScore() { assertEquals(HardSoftScore.ofUninitialized(-7, -147, -258), HardSoftScore.of(-147, -258).withInitScore(-7)); }
@Override public HardSoftScore parseScore(String scoreString) { return HardSoftScore.parseScore(scoreString); }
@Override public boolean equals(Object o) { // A direct implementation (instead of EqualsBuilder) to avoid dependencies if (this == o) { return true; } else if (o instanceof HardSoftScore) { HardSoftScore other = (HardSoftScore) o; return initScore == other.getInitScore() && hardScore == other.getHardScore() && softScore == other.getSoftScore(); } else { return false; } }