@Test public void compare() { LimitViolation line1Violation1 = new LimitViolation("NHV1_NHV2_1", LimitViolationType.CURRENT, null, Integer.MAX_VALUE, 1000.0, 0.95f, 1100.0, Branch.Side.ONE); LimitViolation line1Violation2 = new LimitViolation("NHV1_NHV2_1", LimitViolationType.CURRENT, null, Integer.MAX_VALUE, 1000.0, 0.95f, 1100.0, Branch.Side.TWO); LimitViolation line2Violation = new LimitViolation("NHV1_NHV2_2", LimitViolationType.CURRENT, null, Integer.MAX_VALUE, 900.0, 0.95f, 950.0, Branch.Side.ONE); LimitViolation vl1Violation1 = new LimitViolation("VL1", LimitViolationType.HIGH_VOLTAGE, 200.0, 1, 250.0); LimitViolation vl1Violation2 = new LimitViolation("VL1", LimitViolationType.HIGH_SHORT_CIRCUIT_CURRENT, 200.0, 1, 250.0); List<LimitViolation> violations = Arrays.asList(line1Violation2, vl1Violation1, line2Violation, line1Violation1, vl1Violation2); Collections.sort(violations, new LimitViolationComparator()); assertEquals(line1Violation1, violations.get(0)); assertEquals(line1Violation2, violations.get(1)); assertEquals(line2Violation, violations.get(2)); assertEquals(vl1Violation1, violations.get(3)); assertEquals(vl1Violation2, violations.get(4)); }
@Override public int compare(LimitViolation violation1, LimitViolation violation2) { Objects.requireNonNull(violation1); Objects.requireNonNull(violation2); if (violation1.getSubjectId().compareTo(violation2.getSubjectId()) == 0) { if (violation1.getLimitType().compareTo(violation2.getLimitType()) == 0) { if (LimitViolationType.CURRENT == violation1.getLimitType()) { return violation1.getSide().compareTo(violation2.getSide()); } return 0; } return violation1.getLimitType().compareTo(violation2.getLimitType()); } return violation1.getSubjectId().compareTo(violation2.getSubjectId()); }
LimitViolationComparator implements Comparator<LimitViolation> { @Override public int compare(LimitViolation violation1, LimitViolation violation2) { Objects.requireNonNull(violation1); Objects.requireNonNull(violation2); if (violation1.getSubjectId().compareTo(violation2.getSubjectId()) == 0) { if (violation1.getLimitType().compareTo(violation2.getLimitType()) == 0) { if (LimitViolationType.CURRENT == violation1.getLimitType()) { return violation1.getSide().compareTo(violation2.getSide()); } return 0; } return violation1.getLimitType().compareTo(violation2.getLimitType()); } return violation1.getSubjectId().compareTo(violation2.getSubjectId()); } }
LimitViolationComparator implements Comparator<LimitViolation> { @Override public int compare(LimitViolation violation1, LimitViolation violation2) { Objects.requireNonNull(violation1); Objects.requireNonNull(violation2); if (violation1.getSubjectId().compareTo(violation2.getSubjectId()) == 0) { if (violation1.getLimitType().compareTo(violation2.getLimitType()) == 0) { if (LimitViolationType.CURRENT == violation1.getLimitType()) { return violation1.getSide().compareTo(violation2.getSide()); } return 0; } return violation1.getLimitType().compareTo(violation2.getLimitType()); } return violation1.getSubjectId().compareTo(violation2.getSubjectId()); }  }
LimitViolationComparator implements Comparator<LimitViolation> { @Override public int compare(LimitViolation violation1, LimitViolation violation2) { Objects.requireNonNull(violation1); Objects.requireNonNull(violation2); if (violation1.getSubjectId().compareTo(violation2.getSubjectId()) == 0) { if (violation1.getLimitType().compareTo(violation2.getLimitType()) == 0) { if (LimitViolationType.CURRENT == violation1.getLimitType()) { return violation1.getSide().compareTo(violation2.getSide()); } return 0; } return violation1.getLimitType().compareTo(violation2.getLimitType()); } return violation1.getSubjectId().compareTo(violation2.getSubjectId()); }  @Override int compare(LimitViolation violation1, LimitViolation violation2); }
LimitViolationComparator implements Comparator<LimitViolation> { @Override public int compare(LimitViolation violation1, LimitViolation violation2) { Objects.requireNonNull(violation1); Objects.requireNonNull(violation2); if (violation1.getSubjectId().compareTo(violation2.getSubjectId()) == 0) { if (violation1.getLimitType().compareTo(violation2.getLimitType()) == 0) { if (LimitViolationType.CURRENT == violation1.getLimitType()) { return violation1.getSide().compareTo(violation2.getSide()); } return 0; } return violation1.getLimitType().compareTo(violation2.getLimitType()); } return violation1.getSubjectId().compareTo(violation2.getSubjectId()); }  @Override int compare(LimitViolation violation1, LimitViolation violation2);  }