Skip to content

Commit 9095f5e

Browse files
author
Artem Labazin
committed
add new release
1 parent b1b00a5 commit 9095f5e

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Add more tests.
1313
- Add `JavaDoc`.
1414

15+
## [1.16.4](https://github.com/appulse-projects/utils-java/releases/tag/1.16.4) - 2019-12-05
16+
17+
### Changed
18+
19+
- Added `AutoCloseable` interface to `BytesPool`;
20+
- Refactored initialization for `BytesPool`.
21+
1522
## [1.16.3](https://github.com/appulse-projects/utils-java/releases/tag/1.16.3) - 2019-05-29
1623

1724
### Changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ limitations under the License.
2424

2525
<groupId>io.appulse</groupId>
2626
<artifactId>utils-java</artifactId>
27-
<version>1.16.3</version>
27+
<version>1.16.4</version>
2828
<packaging>jar</packaging>
2929

3030
<properties>
@@ -66,7 +66,7 @@ limitations under the License.
6666
<url>https://github.com/appulse-projects/utils-java</url>
6767
<connection>scm:git:https://github.com/appulse-projects/utils-java.git</connection>
6868
<developerConnection>scm:git:https://github.com/appulse-projects/utils-java.git</developerConnection>
69-
<tag>1.16.3</tag>
69+
<tag>1.16.4</tag>
7070
</scm>
7171

7272
<distributionManagement>

src/main/java/io/appulse/utils/BytesPool.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @since 1.15.0
4545
*/
4646
@FieldDefaults(level = PRIVATE, makeFinal = true)
47-
public class BytesPool {
47+
public class BytesPool implements AutoCloseable {
4848

4949
private static final int DEFAULT_INITIAL_BUFFERS_COUNT = 2;
5050

@@ -66,6 +66,10 @@ public class BytesPool {
6666

6767
Lock createNewLock;
6868

69+
public BytesPool () {
70+
this(null, null, null, null);
71+
}
72+
6973
@Builder
7074
BytesPool (Integer initialBuffersCount,
7175
Integer maximumBuffersCount,
@@ -74,7 +78,6 @@ public class BytesPool {
7478
) {
7579
int initialBuffers = ofNullable(initialBuffersCount)
7680
.filter(it -> it >= 0)
77-
.filter(it -> it <= maximumBuffersCount)
7881
.orElse(DEFAULT_INITIAL_BUFFERS_COUNT);
7982

8083
this.maximumBuffersCount = ofNullable(maximumBuffersCount)
@@ -86,14 +89,15 @@ public class BytesPool {
8689
.filter(it -> it > 0)
8790
.orElse(DEFAULT_INITIAL_BUFFER_SIZE);
8891

89-
this.bufferCreateFunction = bufferCreateFunction;
92+
this.bufferCreateFunction = ofNullable(bufferCreateFunction)
93+
.orElse(Bytes::resizableArray);
9094

9195
totalElements = new LongAdder();
9296
acquiredBuffersCount = new LongAdder();
9397
createNewLock = new ReentrantLock(true);
9498

9599
buffers = IntStream.range(0, initialBuffers)
96-
.mapToObj(it -> bufferCreateFunction.apply(initialBufferSizeBytes))
100+
.mapToObj(it -> this.bufferCreateFunction.apply(this.initialBufferSizeBytes))
97101
.peek(it -> totalElements.increment())
98102
.collect(toCollection(LinkedBlockingQueue::new));
99103
}
@@ -170,6 +174,16 @@ public int getTotalCount () {
170174
return totalElements.intValue();
171175
}
172176

177+
@Override
178+
public void close () {
179+
createNewLock.lock();
180+
try {
181+
buffers.clear();
182+
} finally {
183+
createNewLock.unlock();
184+
}
185+
}
186+
173187
private Bytes getOrCreateBuffer () throws InterruptedException {
174188
val buffer = buffers.poll();
175189
if (buffer != null) {

src/test/java/io/appulse/utils/BytesPoolTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ void twoPools () {
102102
.isInstanceOf(IllegalArgumentException.class)
103103
.hasMessage("The buffer not from this pool");
104104
}
105+
106+
@Test
107+
void instantiation () {
108+
try (val pool = new BytesPool()) {
109+
val buffer = pool.acquire(16);
110+
assertThat(buffer.capacity()).isGreaterThanOrEqualTo(16);
111+
}
112+
}
105113
}

0 commit comments

Comments
 (0)