@Test public void test() throws Exception { CountBasedLimiter countLimiter1 = new CountBasedLimiter(3); CountBasedLimiter countLimiter2 = new CountBasedLimiter(1); MultiLimiter multiLimiter = new MultiLimiter(countLimiter1, countLimiter2); Assert.assertNotNull(multiLimiter.acquirePermits(1)); Assert.assertNull(multiLimiter.acquirePermits(1)); Assert.assertNotNull(countLimiter1.acquirePermits(1)); Assert.assertNull(countLimiter1.acquirePermits(1)); Assert.assertNull(countLimiter2.acquirePermits(1)); }
@Override public Closeable acquirePermits(long permits) throws InterruptedException { Closer closer = Closer.create(); for (Limiter limiter : this.underlyingLimiters) { Closeable permit = limiter.acquirePermits(permits); if (permit == null) { try { closer.close(); } catch (IOException ioe) { throw new RuntimeException("Could not return intermediate permits."); } return null; } closer.register(permit); } return closer; }
MultiLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Closer closer = Closer.create(); for (Limiter limiter : this.underlyingLimiters) { Closeable permit = limiter.acquirePermits(permits); if (permit == null) { try { closer.close(); } catch (IOException ioe) { throw new RuntimeException("Could not return intermediate permits."); } return null; } closer.register(permit); } return closer; } }
MultiLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Closer closer = Closer.create(); for (Limiter limiter : this.underlyingLimiters) { Closeable permit = limiter.acquirePermits(permits); if (permit == null) { try { closer.close(); } catch (IOException ioe) { throw new RuntimeException("Could not return intermediate permits."); } return null; } closer.register(permit); } return closer; } MultiLimiter(Limiter... underlyingLimiters); }
MultiLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Closer closer = Closer.create(); for (Limiter limiter : this.underlyingLimiters) { Closeable permit = limiter.acquirePermits(permits); if (permit == null) { try { closer.close(); } catch (IOException ioe) { throw new RuntimeException("Could not return intermediate permits."); } return null; } closer.register(permit); } return closer; } MultiLimiter(Limiter... underlyingLimiters); @Override void start(); @Override Closeable acquirePermits(long permits); @Override void stop(); }
MultiLimiter implements Limiter { @Override public Closeable acquirePermits(long permits) throws InterruptedException { Closer closer = Closer.create(); for (Limiter limiter : this.underlyingLimiters) { Closeable permit = limiter.acquirePermits(permits); if (permit == null) { try { closer.close(); } catch (IOException ioe) { throw new RuntimeException("Could not return intermediate permits."); } return null; } closer.register(permit); } return closer; } MultiLimiter(Limiter... underlyingLimiters); @Override void start(); @Override Closeable acquirePermits(long permits); @Override void stop();  }