Util { static Comparator<Rating> getRatingByDemandComparator(final Set<Rating> ratingsInOrderOfPreference) { Map<Rating, Integer> ratingRanking = new EnumMap<>(Rating.class); AtomicInteger rank = new AtomicInteger(); ratingsInOrderOfPreference.forEach(r -> ratingRanking.computeIfAbsent(r, key -> rank.getAndIncrement())); return Comparator.comparingInt(o -> ratingRanking.getOrDefault(o, Integer.MAX_VALUE)); } private  Util();   }
@Test void comparator() { Comparator<Rating> comparator = Util.getRatingByDemandComparator(inOrder(Rating.A, Rating.D, Rating.AAAAA)); assertSoftly(softly -> softly.assertThat(Rating.D) .usingComparator(comparator) .isGreaterThan(Rating.A) .isLessThan(Rating.AAAAA) .isLessThan(Rating.AAAAAA) .isLessThan(Rating.C)); }