Skip to content

Commit

Permalink
#41 API for asynchronous mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir.bukhtoyarov committed Aug 9, 2017
1 parent b9fc837 commit df56b39
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bucket4j-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>
<artifactId>bucket4j-benchmarks</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bucket4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.bucket4j;

/**
* Created by vladimir.bukhtoyarov on 09.08.2017.
*/
public class AsyncBucket {
}
11 changes: 11 additions & 0 deletions bucket4j-core/src/main/java/io/github/bucket4j/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@
*/
public interface Bucket {

/**
* Gets asynchronous view of this bucket.
*
* <p>If asynchronous mode is not supported by extension(see {@link Extension#isAsyncModeSupported()}) any attempt to call {@link Bucket#asAsync()} will fail with {@link UnsupportedOperationException}
*
* @return Instance of this bucket with asynchronous mode enabled.
*
* @throws UnsupportedOperationException if particular extension behind the bucket does not support asynchronous mode.
*/
AsyncBucket asAsync() throws UnsupportedOperationException;

/**
* Tries to consume a specified number of tokens from this bucket.
*
Expand Down
3 changes: 2 additions & 1 deletion bucket4j-core/src/main/java/io/github/bucket4j/Bucket4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.github.bucket4j;

import io.github.bucket4j.local.InMemoryExtension;
import io.github.bucket4j.local.LocalBucketBuilder;

import java.util.HashMap;
Expand Down Expand Up @@ -48,7 +49,7 @@ private Bucket4j() {
* @return new instance of {@link LocalBucketBuilder}
*/
public static LocalBucketBuilder builder() {
return new LocalBucketBuilder();
return InMemoryExtension.INSTANCE.builder();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ public T addLimit(long initialTokens, Bandwidth bandwidth) {
* Creates instance of {@link ConfigurationBuilder} which will create buckets with {@link TimeMeter#SYSTEM_NANOTIME} as time meter.
*
* @return this builder instance
*
* @throws UnsupportedOperationException if particular extension behind the bucket does not support the time measurement customization.
* @see Extension#isCustomTimeMeasurementSupported()
*/
public T withNanosecondPrecision() {
public T withNanosecondPrecision() throws UnsupportedOperationException {
this.timeMeter = TimeMeter.SYSTEM_NANOTIME;
return (T) this;
}
Expand All @@ -80,8 +83,10 @@ public T withNanosecondPrecision() {
* Creates instance of {@link ConfigurationBuilder} which will create buckets with {@link TimeMeter#SYSTEM_MILLISECONDS} as time meter.
*
* @return this builder instance
* @throws UnsupportedOperationException if particular extension behind the bucket does not support the time measurement customization.
* @see Extension#isCustomTimeMeasurementSupported()
*/
public T withMillisecondPrecision() {
public T withMillisecondPrecision() throws UnsupportedOperationException {
this.timeMeter = TimeMeter.SYSTEM_MILLISECONDS;
return (T) this;
}
Expand All @@ -92,8 +97,10 @@ public T withMillisecondPrecision() {
* @param customTimeMeter object which will measure time.
*
* @return this builder instance
* @throws UnsupportedOperationException if particular extension behind the bucket does not support the time measurement customization.
* @see Extension#isCustomTimeMeasurementSupported()
*/
public T withCustomTimePrecision(TimeMeter customTimeMeter) {
public T withCustomTimePrecision(TimeMeter customTimeMeter) throws UnsupportedOperationException {
if (customTimeMeter == null) {
throw BucketExceptions.nullTimeMeter();
}
Expand Down
18 changes: 18 additions & 0 deletions bucket4j-core/src/main/java/io/github/bucket4j/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@ public interface Extension<T extends ConfigurationBuilder<T>> {
*/
T builder();

/**
* Describes whether or not this extension supports asynchronous mode.
*
* <p>If asynchronous mode is not supported any attempt to call {@link Bucket#asAsync()} will fail with {@link UnsupportedOperationException}
*
* @return true if this extension supports asynchronous mode.
*/
boolean isAsyncModeSupported();

/**
* Describes whether or not this extension supports the customization of time measurement.
*
* <p>If customization of time measurement is not supported any attempt to call {@link ConfigurationBuilder#withCustomTimePrecision(TimeMeter)}, {@link ConfigurationBuilder#withMillisecondPrecision()}, {@link ConfigurationBuilder#withNanosecondPrecision()} will fail with {@link UnsupportedOperationException}
*
* @return true if this extension supports the customization of time measurement.
*/
boolean isCustomTimeMeasurementSupported();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.bucket4j.local;

import io.github.bucket4j.Extension;

/**
*
*/
public class InMemoryExtension implements Extension<LocalBucketBuilder> {

public static InMemoryExtension INSTANCE = new InMemoryExtension();

@Override
public LocalBucketBuilder builder() {
return new LocalBucketBuilder();
}

@Override
public boolean isAsyncModeSupported() {
return true;
}

@Override
public boolean isCustomTimeMeasurementSupported() {
return true;
}

}
19 changes: 19 additions & 0 deletions bucket4j-hazelcast/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>2.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>

<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-hazelcast</artifactId>
<version>2.2.0</version>


</project>
19 changes: 19 additions & 0 deletions bucket4j-ignite/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>2.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>

<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-ignite</artifactId>
<version>2.2.0</version>


</project>
18 changes: 18 additions & 0 deletions bucket4j-infinispan/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>2.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>

<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-infinispan</artifactId>
<version>2.2.0</version>

</project>
2 changes: 1 addition & 1 deletion bucket4j-jcache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down

0 comments on commit df56b39

Please sign in to comment.