Category { public Category withMatchRule(MatchRule rule) { checkNotNull(rule); ImmutableList.Builder<MatchRule> newRules = ImmutableList.builder(); newRules.addAll(getMatchRules()); newRules.add(rule); return new Category(name, newRules.build()); } Category(String name);  Category(String name, List<MatchRule> matchRules); String getName(); List<MatchRule> getMatchRules(); boolean matches(Transaction transaction); Category withMatchRule(MatchRule rule); @Override String toString();  }
@Test public void withMatchRule() { MatchRule rule = MatchRule.withPattern("abc"); MatchRule rule2 = MatchRule.withPattern("abc2"); Category category = new Category("a name"); Category category2 = category.withMatchRule(rule); assertNotNull(category2); assertNotSame(category, category2); assertEquals(category.getName(), category2.getName()); assertEquals(ImmutableList.of(), category.getMatchRules()); assertEquals(ImmutableList.of(rule), category2.getMatchRules()); assertEquals(ImmutableList.of(rule, rule2), category2.withMatchRule(rule2).getMatchRules()); }