use of java.util.function.IntSupplier in project alf.io by alfio-event.
the class WaitingQueueManager method distributeAvailableSeats.
private Stream<Triple<WaitingQueueSubscription, TicketReservationWithOptionalCodeModification, ZonedDateTime>> distributeAvailableSeats(Event event, Ticket.TicketStatus status, IntSupplier availableSeatSupplier) {
int availableSeats = availableSeatSupplier.getAsInt();
int eventId = event.getId();
log.debug("processing {} subscribers from waiting list", availableSeats);
List<TicketCategory> unboundedCategories = ticketCategoryRepository.findUnboundedOrderByExpirationDesc(eventId);
Iterator<Ticket> tickets = ticketRepository.selectWaitingTicketsForUpdate(eventId, status.name(), availableSeats).stream().filter(t -> t.getCategoryId() != null || !unboundedCategories.isEmpty()).iterator();
int expirationTimeout = configurationManager.getFor(WAITING_QUEUE_RESERVATION_TIMEOUT, ConfigurationLevel.event(event)).getValueAsIntOrDefault(4);
ZonedDateTime expiration = event.now(clockProvider).plusHours(expirationTimeout).with(WorkingDaysAdjusters.defaultWorkingDays());
if (!tickets.hasNext()) {
log.warn("Unable to assign tickets, returning an empty stream");
return Stream.empty();
}
return waitingQueueRepository.loadWaiting(eventId, availableSeats).stream().map(wq -> Pair.of(wq, tickets.next())).map(pair -> {
TicketReservationModification ticketReservation = new TicketReservationModification();
ticketReservation.setQuantity(1);
Integer categoryId = Optional.ofNullable(pair.getValue().getCategoryId()).orElseGet(() -> findBestCategory(unboundedCategories, pair.getKey()).orElseThrow(RuntimeException::new).getId());
ticketReservation.setTicketCategoryId(categoryId);
return Pair.of(pair.getLeft(), new TicketReservationWithOptionalCodeModification(ticketReservation, Optional.empty()));
}).map(pair -> Triple.of(pair.getKey(), pair.getValue(), expiration));
}
use of java.util.function.IntSupplier in project ambry by linkedin.
the class Utils method partitionList.
/**
* Partition the input list into a List of smaller sublists, each one limited to the specified batch size. This method
* copy elements, so changes to the so changes to the original list will be reflected in the returned list.
* Method inspired by the Guava utility Lists.partition(List<T> list, int size).
* @param inputList the input list to partition.
* @param batchSize the maximum size of the returned sublists.
* @return the partitioned list of sublists.
*/
public static <T> List<List<T>> partitionList(List<T> inputList, int batchSize) {
Objects.requireNonNull(inputList, "Input list cannot be null");
if (batchSize < 1) {
throw new IllegalArgumentException("Invalid batchSize: " + batchSize);
}
IntSupplier numBatches = () -> (inputList.size() + batchSize - 1) / batchSize;
IntFunction<List<T>> batchFetcher = index -> {
int start = index * batchSize;
int end = Math.min(start + batchSize, inputList.size());
return inputList.subList(start, end);
};
return new ListView<>(numBatches, batchFetcher);
}
use of java.util.function.IntSupplier in project aeron by real-logic.
the class SendSelectReceiveUdpPing method run.
private void run() throws IOException {
final Histogram histogram = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
final DatagramChannel receiveChannel = DatagramChannel.open();
Common.init(receiveChannel);
receiveChannel.bind(new InetSocketAddress("localhost", Common.PONG_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 timestampNs = buffer.getLong(SIZE_OF_LONG);
if (receivedSequenceNumber != sequenceNumber) {
throw new IllegalStateException("data Loss:" + sequenceNumber + " to " + receivedSequenceNumber);
}
final long durationNs = System.nanoTime() - timestampNs;
histogram.recordValue(durationNs);
} 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 (running.get()) {
measureRoundTrip(histogram, SEND_ADDRESS, buffer, sendChannel, selector, running);
histogram.reset();
System.gc();
LockSupport.parkNanos(1000 * 1000 * 1000);
}
}
use of java.util.function.IntSupplier in project DrivenByMoss by git-moss.
the class AbstractControllerSetup method addButton.
/**
* Create a hardware button proxy, bind a trigger command to it and bind it to the trigger bind
* type retrieved from {@link #getTriggerBindType(ButtonID)}.
*
* @param surface The control surface
* @param buttonID The ID of the button (for later access)
* @param label The label of the button
* @param supplier Callback for retrieving the state of the light
* @param midiInputChannel The MIDI input channel
* @param midiOutputChannel The MIDI output channel
* @param midiControl The MIDI CC or note
* @param value The specific value of the control to bind to
* @param command The command to bind
* @param hasLight True create and add a light
* @param colorIds The color IDs to map to the states
*/
protected void addButton(final S surface, final ButtonID buttonID, final String label, final TriggerCommand command, final int midiInputChannel, final int midiOutputChannel, final int midiControl, final int value, final boolean hasLight, final IntSupplier supplier, final String... colorIds) {
final IHwButton button = surface.createButton(buttonID, label);
button.bind(command);
if (midiControl < 0)
return;
final IMidiInput midiInput = surface.getMidiInput();
final BindType triggerBindType = this.getTriggerBindType(buttonID);
if (value == -1)
button.bind(midiInput, triggerBindType, midiInputChannel, midiControl);
else
button.bind(midiInput, triggerBindType, midiInputChannel, midiControl, value);
if (hasLight) {
final IntSupplier intSupplier = () -> button.isPressed() ? 1 : 0;
final IntSupplier supp = supplier == null ? intSupplier : supplier;
this.addLight(surface, null, buttonID, button, midiOutputChannel, midiControl, supp, colorIds);
}
}
use of java.util.function.IntSupplier in project Java-Tutorial by gpcodervn.
the class SupplierExample3 method main.
public static void main(String[] args) {
BooleanSupplier bs = NumberUtils::getBooleanValue;
System.out.println("Boolean Value: " + bs.getAsBoolean());
IntSupplier dbs = NumberUtils::getIntValue;
System.out.println("Integer Value: " + dbs.getAsInt());
LongSupplier ls = NumberUtils::getLongValue;
System.out.println("Long Value: " + ls.getAsLong());
DoubleSupplier ds = NumberUtils::getDoubleValue;
System.out.println("Double Value: " + ds.getAsDouble());
}
Aggregations