Search in sources :

Example 31 with IntSupplier

use of java.util.function.IntSupplier in project fmv by f-agu.

the class Bootstrap method basicProgress.

/**
 * @throws Exception
 */
private static void basicProgress() throws Exception {
    AtomicInteger value = new AtomicInteger();
    IntSupplier progressInPercent = value::get;
    try (TextProgressBar bar = TextProgressBar.newBar().fixWidth(25).withText("Basic progress").append(ProgressPart.width(32).build()).buildAndSchedule(progressInPercent)) {
        for (int i = 0; i < 100; i += 3) {
            value.set(i);
            Thread.sleep(100);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntSupplier(java.util.function.IntSupplier) TextProgressBar(org.fagu.fmv.textprogressbar.TextProgressBar)

Example 32 with IntSupplier

use of java.util.function.IntSupplier in project fmv by f-agu.

the class TextProgressBarTest method testWidth.

@Test
public void testWidth() throws Exception {
    final int width = 30;
    AtomicInteger value = new AtomicInteger();
    IntSupplier progressInPercent = value::get;
    AtomicReference<String> line = new AtomicReference<String>();
    try (TextProgressBar bar = TextProgressBar.newBar().displayTo(l -> line.set(l.replace("\r", ""))).append(ProgressPart.width(width).build()).buildAndSchedule(progressInPercent)) {
        Thread.sleep(700);
        assertEquals("[>                           ]", line.get());
        for (int i = 0; i < 100; i += 7) {
            value.set(i);
            Thread.sleep(100);
            String s = line.get();
            s = s.replace("\r", "");
            assertEquals(width, s.length());
            assertEquals('[', s.charAt(0));
            assertEquals(']', s.charAt(s.length() - 1));
        }
    }
    assertEquals("[============================]", line.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntSupplier(java.util.function.IntSupplier) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 33 with IntSupplier

use of java.util.function.IntSupplier in project neo4j by neo4j.

the class RelationshipModifierTest method shouldCreateAndDelete.

// ... other known cases ...
@RepeatedTest(20)
void shouldCreateAndDelete() {
    // given and initial state
    long node = createEmptyNode();
    IntSupplier typeStrategy = randomTypes(10);
    Supplier<RelationshipDirection> directionStrategy = RANDOM_DIRECTION;
    LongSupplier otherNodeStrategy = this::createEmptyNode;
    int maxRelationships = 100;
    List<RelationshipData> expectedRelationships = generateRelationshipData(random.nextInt(0, maxRelationships), node, typeStrategy, otherNodeStrategy, directionStrategy);
    createRelationships(expectedRelationships);
    // ... a set of relationships to create and delete
    List<RelationshipData> relationshipsToCreate = generateRelationshipData((int) random.among(new long[] { 0, 1, 10, 100 }), node, typeStrategy, otherNodeStrategy, directionStrategy);
    int numRelationshipsToDelete = min((int) random.among(new long[] { 0, 1, 10, maxRelationships }), expectedRelationships.size());
    RelationshipData[] relationshipsToDelete = random.selection(expectedRelationships.toArray(RelationshipData[]::new), numRelationshipsToDelete, numRelationshipsToDelete, false);
    // ... and rules for how the world changes "concurrently" while we perform these modifications
    // on locked
    monitors.addMonitorListener(new ResourceTypeLockOrderVerifier());
    // on read
    monitors.addMonitorListener(new ChangeWorldOnReadMonitor(node, typeStrategy, otherNodeStrategy, directionStrategy, expectedRelationships));
    // when
    RelationshipModifications modifications = modifications(relationshipsToCreate.toArray(RelationshipData[]::new), relationshipsToDelete);
    modify(modifications);
    applyModificationsToExpectedRelationships(modifications, expectedRelationships);
    // then
    assertThat(readRelationshipsFromStore(node, store)).isEqualTo(asSet(expectedRelationships));
}
Also used : RelationshipData(org.neo4j.internal.recordstorage.FlatRelationshipModifications.RelationshipData) RelationshipModifications(org.neo4j.storageengine.api.txstate.RelationshipModifications) RelationshipDirection(org.neo4j.storageengine.api.RelationshipDirection) IntSupplier(java.util.function.IntSupplier) LongSupplier(java.util.function.LongSupplier) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 34 with IntSupplier

use of java.util.function.IntSupplier in project Aeron by real-logic.

the class SelectReceiveSendUdpPong method run.

private void run() throws IOException {
    final InetSocketAddress sendAddress = new InetSocketAddress("localhost", Common.PONG_PORT);
    final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
    final DatagramChannel receiveChannel = DatagramChannel.open();
    Common.init(receiveChannel);
    receiveChannel.bind(new InetSocketAddress("localhost", Common.PING_PORT));
    final DatagramChannel sendChannel = DatagramChannel.open();
    Common.init(sendChannel);
    final Selector selector = Selector.open();
    final IntSupplier handler = () -> {
        try {
            buffer.clear();
            receiveChannel.receive(buffer);
            final long receivedSequenceNumber = buffer.getLong(0);
            final long receivedTimestamp = buffer.getLong(SIZE_OF_LONG);
            buffer.clear();
            buffer.putLong(receivedSequenceNumber);
            buffer.putLong(receivedTimestamp);
            buffer.flip();
            sendChannel.send(buffer, sendAddress);
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
        return 1;
    };
    receiveChannel.register(selector, OP_READ, handler);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (true) {
        while (selector.selectNow() == 0) {
            if (!running.get()) {
                return;
            }
            ThreadHints.onSpinWait();
        }
        final Set<SelectionKey> selectedKeys = selector.selectedKeys();
        final Iterator<SelectionKey> iter = selectedKeys.iterator();
        while (iter.hasNext()) {
            final SelectionKey key = iter.next();
            if (key.isReadable()) {
                ((IntSupplier) key.attachment()).getAsInt();
            }
            iter.remove();
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SelectionKey(java.nio.channels.SelectionKey) IntSupplier(java.util.function.IntSupplier) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Selector(java.nio.channels.Selector)

Example 35 with IntSupplier

use of java.util.function.IntSupplier in project aeron by real-logic.

the class SendSelectReceiveUdpPing method measureRoundTrip.

private void measureRoundTrip(final Histogram histogram, final InetSocketAddress sendAddress, final ByteBuffer buffer, final DatagramChannel sendChannel, final Selector selector, final AtomicBoolean running) throws IOException {
    for (sequenceNumber = 0; sequenceNumber < Common.NUM_MESSAGES; sequenceNumber++) {
        final long timestamp = System.nanoTime();
        buffer.clear();
        buffer.putLong(sequenceNumber);
        buffer.putLong(timestamp);
        buffer.flip();
        sendChannel.send(buffer, sendAddress);
        while (selector.selectNow() == 0) {
            if (!running.get()) {
                return;
            }
            ThreadHints.onSpinWait();
        }
        final Set<SelectionKey> selectedKeys = selector.selectedKeys();
        final Iterator<SelectionKey> iter = selectedKeys.iterator();
        while (iter.hasNext()) {
            final SelectionKey key = iter.next();
            if (key.isReadable()) {
                ((IntSupplier) key.attachment()).getAsInt();
            }
            iter.remove();
        }
    }
    histogram.outputPercentileDistribution(System.out, 1000.0);
}
Also used : SelectionKey(java.nio.channels.SelectionKey) IntSupplier(java.util.function.IntSupplier)

Aggregations

IntSupplier (java.util.function.IntSupplier)39 Test (org.junit.Test)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 UnitTest (org.apache.geode.test.junit.categories.UnitTest)6 IOException (java.io.IOException)5 ByteBuffer (java.nio.ByteBuffer)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 InetSocketAddress (java.net.InetSocketAddress)4 DatagramChannel (java.nio.channels.DatagramChannel)4 Selector (java.nio.channels.Selector)4 IHwButton (de.mossgrabers.framework.controller.hardware.IHwButton)3 SelectionKey (java.nio.channels.SelectionKey)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Supplier (java.util.function.Supplier)3 Collectors (java.util.stream.Collectors)3 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)2 ButtonID (de.mossgrabers.framework.controller.ButtonID)2 IHwLight (de.mossgrabers.framework.controller.hardware.IHwLight)2 WeakReference (java.lang.ref.WeakReference)2