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);
}
}
}
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());
}
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));
}
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();
}
}
}
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);
}
Aggregations