Transactions { public Transactions copyWithReplacement(Transaction transaction) { boolean found = false; ImmutableList.Builder<Transaction> builder = ImmutableList.<Transaction> builder(); for (Transaction t : transactions) { if (t.getId().equals(transaction.getId())) { found = true; builder.add(transaction); } else { builder.add(t); } } checkArgument(found, "Transaction not present"); return new Transactions(builder.build()); } Transactions();  Transactions(List<Transaction> transactions);  Transactions(Transaction... transactions); List<Transaction> getTransactions(); Transaction byId(String id); Transactions copyWithReplacement(Transaction transaction);  }
@Test public void copyWithReplacement() { Transaction t1 = MockTransaction.create("2015-01-02", "a", 123); Transaction t2 = MockTransaction.create("2015-01-02", "a", 123); Transaction t3 = t1.withCategoryName("a cat"); Transactions transactions = new Transactions(ImmutableList.of(t1, t2)); Transactions copyWithReplacement = transactions.copyWithReplacement(t3); assertNotNull(copyWithReplacement); assertNotSame(transactions, copyWithReplacement); assertSame(t3, copyWithReplacement.byId(t3.getId())); assertSame(t1, transactions.byId(t1.getId())); assertEquals(ImmutableList.of(t1, t2), transactions.getTransactions()); assertEquals(ImmutableList.of(t3, t2), copyWithReplacement.getTransactions()); }