Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Java 19 build #2478

Merged
merged 10 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-parent</artifactId>
<version>9</version>
<version>10</version>
<relativePath/>
</parent>

Expand Down Expand Up @@ -106,7 +106,7 @@
<findbugs-jsr305.version>3.0.2</findbugs-jsr305.version>
<gdata.version>1.41.1_1</gdata.version>
<graphviz-builder.version>1.0.12</graphviz-builder.version>
<groovy.version>3.0.9</groovy.version>
<groovy.version>3.0.15</groovy.version>
<guava.version>31.1-jre</guava.version>
<jackson.version>2.14.1</jackson.version>
<jackson-databind.version>2.14.1</jackson-databind.version>
Expand All @@ -119,7 +119,7 @@
<maven.core.version>3.8.5</maven.core.version>
<maven.plugin.annotations.version>3.6.0</maven.plugin.annotations.version>
<mfl.version>0.5.9</mfl.version>
<mockito.version>4.4.0</mockito.version>
<mockito.version>5.1.1</mockito.version>
<nativelibloader.version>2.4.0</nativelibloader.version>
<poi.version>5.2.3</poi.version>
<rdf4j.version>4.2.2</rdf4j.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.DoubleBuffer;
import java.util.Objects;
import java.util.function.IntFunction;
import java.util.function.IntConsumer;

/**
* @author Jon Harper <jon.harper at rte-france.com>
Expand All @@ -23,8 +24,13 @@ public class BigDoubleBuffer {
private DoubleBuffer[] buffers;
private long size;

public BigDoubleBuffer(IntFunction<ByteBuffer> byteBufferAllocator, long size) {
Objects.requireNonNull(byteBufferAllocator);
//To remove if we ever get it from somewhere else
//package private for tests
@FunctionalInterface interface IntIntBiConsumer { public void accept(int a, int b); }

//using a lambda to test independently from java.nio.ByteBuffer
//package private for tests
static void withSizes(long size, IntConsumer bufferContainerInitializer, IntIntBiConsumer bufferInitializer) {
if (size < 0) {
throw new IllegalArgumentException("Invalid buffer size: " + size);
}
Expand All @@ -38,30 +44,49 @@ public BigDoubleBuffer(IntFunction<ByteBuffer> byteBufferAllocator, long size) {
if (size > 0 && lastBufferSizeBytes == 0) {
lastBufferSizeBytes = BUFFER_SIZE_BYTES;
}
buffers = new DoubleBuffer[bufferCount];
bufferContainerInitializer.accept(bufferCount);
for (int i = 0; i < bufferCount - 1; i++) {
buffers[i] = byteBufferAllocator.apply(BUFFER_SIZE_BYTES).asDoubleBuffer();
bufferInitializer.accept(i, BUFFER_SIZE_BYTES);
}
if (lastBufferSizeBytes > 0) {
buffers[bufferCount - 1] = byteBufferAllocator.apply(lastBufferSizeBytes).asDoubleBuffer();
bufferInitializer.accept(bufferCount - 1, lastBufferSizeBytes);
}
}

public BigDoubleBuffer(IntFunction<ByteBuffer> byteBufferAllocator, long size) {
Objects.requireNonNull(byteBufferAllocator);
withSizes(size,
bufferCount -> buffers = new DoubleBuffer[bufferCount],
(i, bufferSize) -> buffers[i] = byteBufferAllocator.apply(bufferSize).asDoubleBuffer()
);
this.size = size;
}

public void put(long index, double value) {
//To remove if we ever get it from somewhere else
//package private for tests
@FunctionalInterface interface IntIntToDoubleBiFunction { public double applyAsDouble(int a, int b); }

//using a lambda to test independently from java.nio.ByteBuffer
//package private for tests
static double withIndices(long index, IntIntToDoubleBiFunction indicesBiFunction) {
long computedBufferIndex = index >> BUFFER_SHIFT;
long computedSecondIndex = index & BUFFER_MASK;
int bufferIndex = (int) computedBufferIndex;
int secondIndex = (int) computedSecondIndex;
buffers[bufferIndex].put(secondIndex, value);
return indicesBiFunction.applyAsDouble(bufferIndex, secondIndex);
}

public void put(long index, double value) {
withIndices(index, (bufferIndex, secondIndex) -> {
buffers[bufferIndex].put(secondIndex, value);
return Double.NaN; // just to reuse the withIndices code
});
}

public double get(long index) {
long computedBufferIndex = index >> BUFFER_SHIFT;
long computedSecondIndex = index & BUFFER_MASK;
int bufferIndex = (int) computedBufferIndex;
int secondIndex = (int) computedSecondIndex;
return buffers[bufferIndex].get(secondIndex);
return withIndices(index, (bufferIndex, secondIndex) ->
buffers[bufferIndex].get(secondIndex)
);
}

public long capacity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.function.IntFunction;
import java.util.function.IntConsumer;

/**
* @author Jon Harper <jon.harper at rte-france.com>
Expand All @@ -21,8 +22,13 @@ public class BigStringBuffer {
private CompactStringBuffer[] buffers;
private long size;

public BigStringBuffer(IntFunction<ByteBuffer> byteBufferAllocator, long size) {
Objects.requireNonNull(byteBufferAllocator);
//To remove if we ever get it from somewhere else
//package private for tests
@FunctionalInterface interface IntIntBiConsumer { public void accept(int a, int b); }

//using a lambda to test independently from java.nio.ByteBuffer
//package private for tests
static void withSizes(long size, IntConsumer bufferContainerInitializer, IntIntBiConsumer bufferInitializer) {
if (size < 0) {
throw new IllegalArgumentException("Invalid buffer size: " + size);
}
Expand All @@ -36,30 +42,49 @@ public BigStringBuffer(IntFunction<ByteBuffer> byteBufferAllocator, long size) {
if (size > 0 && lastBufferSizeInts == 0) {
lastBufferSizeInts = BUFFER_SIZE_INTS;
}
buffers = new CompactStringBuffer[bufferCount];
bufferContainerInitializer.accept(bufferCount);
for (int i = 0; i < bufferCount - 1; i++) {
buffers[i] = new CompactStringBuffer(byteBufferAllocator, BUFFER_SIZE_INTS);
bufferInitializer.accept(i, BUFFER_SIZE_INTS);
}
if (lastBufferSizeInts > 0) {
buffers[bufferCount - 1] = new CompactStringBuffer(byteBufferAllocator, lastBufferSizeInts);
bufferInitializer.accept(bufferCount - 1, lastBufferSizeInts);
}
}

public BigStringBuffer(IntFunction<ByteBuffer> byteBufferAllocator, long size) {
Objects.requireNonNull(byteBufferAllocator);
withSizes(size,
bufferCount -> buffers = new CompactStringBuffer[bufferCount],
(i, bufferSize) -> buffers[i] = new CompactStringBuffer(byteBufferAllocator, bufferSize)
);
this.size = size;
}

public void putString(long index, String value) {
//To remove if we ever get it from somewhere else
//package private for tests
@FunctionalInterface interface IntIntBiFunction { public String apply(int a, int b); }

//using a lambda to test independently from java.nio.ByteBuffer
//package private for tests
static String withIndices(long index, IntIntBiFunction indicesBiFunction) {
long computedBufferIndex = index >> BUFFER_SHIFT;
long computedSecondIndex = index & BUFFER_MASK;
int bufferIndex = (int) computedBufferIndex;
int secondIndex = (int) computedSecondIndex;
buffers[bufferIndex].putString(secondIndex, value);
return indicesBiFunction.apply(bufferIndex, secondIndex);
}

public void putString(long index, String value) {
withIndices(index, (bufferIndex, secondIndex) -> {
buffers[bufferIndex].putString(secondIndex, value);
return null; // just to reuse the withIndices code
});
}

public String getString(long index) {
long computedBufferIndex = index >> BUFFER_SHIFT;
long computedSecondIndex = index & BUFFER_MASK;
int bufferIndex = (int) computedBufferIndex;
int secondIndex = (int) computedSecondIndex;
return buffers[bufferIndex].getString(secondIndex);
return withIndices(index, (bufferIndex, secondIndex) ->
buffers[bufferIndex].getString(secondIndex)
);
}

public long capacity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import org.mockito.Mockito;

import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.function.IntConsumer;
import com.powsybl.timeseries.BigDoubleBuffer.IntIntBiConsumer;
import com.powsybl.timeseries.BigDoubleBuffer.IntIntToDoubleBiFunction;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
Expand All @@ -28,91 +28,97 @@ class BigDoubleBufferTest {
private int allocatorCount;

private ByteBuffer testDoubleAllocator(int capacity) {
allocatorCount++;
ByteBuffer mockbyte = Mockito.mock(ByteBuffer.class);
DoubleBuffer mockdouble = Mockito.mock(DoubleBuffer.class);
when(mockbyte.asDoubleBuffer()).thenReturn(mockdouble);
Map<Integer, Double> map = new HashMap<>();
when(mockdouble.put(anyInt(), anyDouble())).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
DoubleBuffer mock = (DoubleBuffer) invocation.getMock();
map.put((int) args[0], (double) args[1]);
return mock;
});
when(mockdouble.get(anyInt())).thenAnswer(invocation -> {
return map.get(invocation.getArguments()[0]);
});
return mockbyte;
try {
ByteBuffer bytebuffer = ByteBuffer.allocate(capacity);
allocatorCount++;
return bytebuffer;
} catch (Exception e) {
throw new RuntimeException("error in allocator test", e);
}
}

@BeforeEach
void before() {
allocatorCount = 0;
}

private void bufferTester(long size) {
@Test
void testSimple() {
long size = 10L;
BigDoubleBuffer buffer = new BigDoubleBuffer(this::testDoubleAllocator, size);
assertEquals(1, allocatorCount);
//Simple writes at the begining
for (int i = 0; i < 10; i++) {
buffer.put(i, i);
}
//writes around first buffer change
if (size > BUFFER_SIZE_DOUBLES + 10) {
for (int i = BUFFER_SIZE_DOUBLES - 10; i < BUFFER_SIZE_DOUBLES + 10; i++) {
buffer.put(i, i);
}
}
//writes at the end
for (long i = size - 10; i < size; i++) {
buffer.put(i, i);
}

for (int i = 0; i < 10; i++) {
assertEquals(i, buffer.get(i), 0);
}
if (size > BUFFER_SIZE_DOUBLES + 10) {
for (int i = BUFFER_SIZE_DOUBLES - 10; i < BUFFER_SIZE_DOUBLES + 10; i++) {
assertEquals(i, buffer.get(i), 0);
}
}
for (long i = size - 10; i < size; i++) {
assertEquals(i, buffer.get(i), 0);
}
}

@Test
void testSimple() {
bufferTester(10);
assertEquals(1, allocatorCount);
void testWithIndices() {
IntIntToDoubleBiFunction indicesBiFunction = Mockito.mock(IntIntToDoubleBiFunction.class);
BigDoubleBuffer.withIndices(10, indicesBiFunction);
verify(indicesBiFunction).applyAsDouble(0, 10);
verifyNoMoreInteractions(indicesBiFunction);

//writes around first buffer change
BigDoubleBuffer.withIndices(BUFFER_SIZE_DOUBLES - 1, indicesBiFunction);
verify(indicesBiFunction).applyAsDouble(0, BUFFER_SIZE_DOUBLES - 1);
verifyNoMoreInteractions(indicesBiFunction);
BigDoubleBuffer.withIndices(BUFFER_SIZE_DOUBLES, indicesBiFunction);
verify(indicesBiFunction).applyAsDouble(1, 0);
verifyNoMoreInteractions(indicesBiFunction);

//writes around random buffer change
BigDoubleBuffer.withIndices(7 * BUFFER_SIZE_DOUBLES - 1, indicesBiFunction);
verify(indicesBiFunction).applyAsDouble(6, BUFFER_SIZE_DOUBLES - 1);
verifyNoMoreInteractions(indicesBiFunction);
BigDoubleBuffer.withIndices(7 * BUFFER_SIZE_DOUBLES, indicesBiFunction);
verify(indicesBiFunction).applyAsDouble(7, 0);
verifyNoMoreInteractions(indicesBiFunction);
}

void indicesTester(long size, int bufferCount) {
IntConsumer bufferContainerInitializer = Mockito.mock(IntConsumer.class);
IntIntBiConsumer bufferInitializer = Mockito.mock(IntIntBiConsumer.class);
BigDoubleBuffer.withSizes(size,
bufferContainerInitializer,
bufferInitializer
);
verify(bufferContainerInitializer).accept(bufferCount);
verifyNoMoreInteractions(bufferContainerInitializer);
for (int i = 0; i < bufferCount - 1; i++) {
verify(bufferInitializer).accept(i, BUFFER_SIZE_DOUBLES * Double.BYTES);
}
verify(bufferInitializer).accept(bufferCount - 1, (int) (size - (bufferCount - 1) * BUFFER_SIZE_DOUBLES) * Double.BYTES);
verifyNoMoreInteractions(bufferInitializer);
}

@Test
void testMultipleBuffers() {
bufferTester(200000000);
assertEquals(2, allocatorCount);
indicesTester(200000000, 2);
}

@Test
void testHuge() {
bufferTester(10000000000L);
assertEquals(75, allocatorCount);
indicesTester(10000000000L, 75);
}

@Test
void testSizeBufferMinus1() {
bufferTester(BUFFER_SIZE_DOUBLES - 1);
assertEquals(1, allocatorCount);
indicesTester(BUFFER_SIZE_DOUBLES - 1, 1);
}

@Test
void testSizeBufferExact() {
bufferTester(BUFFER_SIZE_DOUBLES);
assertEquals(1, allocatorCount);
indicesTester(BUFFER_SIZE_DOUBLES, 1);
}

@Test
void testSizeBufferPlus1() {
bufferTester(BUFFER_SIZE_DOUBLES + 1);
assertEquals(2, allocatorCount);
indicesTester(BUFFER_SIZE_DOUBLES + 1, 2);
}
}
Loading