SenderCache { @SuppressWarnings("unchecked") public <T extends Client> Sender<T> get(DataSource source, T client) { SenderKey key = new SenderKey(source, client); if (!senderMap.containsKey(key)) senderMap.put(key, client.accept(senderCreatorVisitor, source)); return (Sender<T>) senderMap.get(key); } @Inject  SenderCache(SenderFactory senderFactory); @SuppressWarnings("unchecked") Sender<T> get(DataSource source, T client); void release(DataSource source, Client client);  }
@Test public void testGetAndRelease() { SenderCache cache = new SenderCache(senderFactory); final HttpClient httpClient = new HttpClient("someId", "someUrl", false); final GcmClient gcmClient = new GcmClient("someId", "someDeviceId"); final DataSource source = new DataSource("someId", null, null, null); cache.get(source, httpClient); cache.get(source, httpClient); cache.get(source, gcmClient); cache.get(source, gcmClient); new Verifications() {{ senderFactory.createHttpSender(source, httpClient); times = 1; senderFactory.createGcmSender(source, gcmClient); times = 1; }}; }