MovieComparatorFactory { public MovieComparator make(MovieComparator.Type type) { switch (type) { case YEAR_EQUALS: return new EqualsYearComparator(); case YEAR_DEVIATION: return new DeviationYearComparator(config); case TYPE_EQUALS: return new EqualsTypeComparator(); case TYPE_ANY: return new AnyTypeComparator(); case TITLE_EQUALS: return new EqualsTitleComparator(); case TITLE_CONTAINS: return new ContainsTitleComparator(); case TITLE_STARTS: return new StartsWithTitleComparator(); case TITLE_ENDS: return new EndsWithTitleComparator(); case TITLE_SMART: return new SmartTitleComparator(); default: throw new IllegalArgumentException("Comparator of this type not found!"); } } @Inject  MovieComparatorFactory(Config config); MovieComparator make(MovieComparator.Type type); MovieComparator make(MovieComparator.Type... types);  }
@Test public void makeSingle() throws Exception { val classMap = new ImmutableMap.Builder<Type, Class<? extends MovieComparator>>() .put(YEAR_EQUALS, EqualsYearComparator.class) .put(YEAR_DEVIATION, DeviationYearComparator.class) .put(TYPE_EQUALS, EqualsTypeComparator.class) .put(TYPE_ANY, AnyTypeComparator.class) .put(TITLE_CONTAINS, ContainsTitleComparator.class) .put(TITLE_ENDS, EndsWithTitleComparator.class) .put(TITLE_STARTS, StartsWithTitleComparator.class) .put(TITLE_EQUALS, EqualsTitleComparator.class) .put(TITLE_SMART, SmartTitleComparator.class) .build(); classMap.forEach((type, clazz) -> { MovieComparator instance = factory.make(type); assertTrue(clazz.isInstance(instance)); }); }
@Test public void makeMultiple() throws Exception { factory.make(TYPE_ANY, TITLE_SMART, YEAR_DEVIATION); assertTrue(factory.make(new Type[]{TYPE_ANY}) instanceof AnyTypeComparator); }