Java Code Examples for io.vavr.collection.Seq#isEmpty()

The following examples show how to use io.vavr.collection.Seq#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataRowComparator.java    From java-datatable with Apache License 2.0 5 votes vote down vote up
/**
 * Recursive sort method, handles multi-sort on columns.
 *
 * @param rowOne The first row to compare.
 * @param rowTwo The second row to compare.
 * @param sortItems The collection of Sort Item details.
 * @return Returns a single integer for the sort.
 */
private static int compareBySortItem(DataRow rowOne, DataRow rowTwo, Seq<SortItem> sortItems) {

    // No more items to sort, then return 0;
    if (sortItems.isEmpty()) return 0;

    // Compare the values in the two rows for the current sort item.
    Integer compareResult = compareValues(rowOne, rowTwo, sortItems.head());

    // If it's the same, then compare again on the next item until finished.
    return compareResult == 0
            ? compareBySortItem(rowOne, rowTwo, sortItems.tail())
            : compareResult;
}
 
Example 2
Source File: MaterializerActor.java    From ts-reaktive with MIT License 5 votes vote down vote up
private CompletionStage<Done> persistSequential(int workerIndex, Seq<E> seq) {
    if (seq.isEmpty()) {
        return done;
    } else {
        return materialize(seq.head())
            .thenCompose(done -> {
                metrics.getEvents(workerIndex).increment();
                return persistSequential(workerIndex, seq.tail());
            });
    }
}
 
Example 3
Source File: AbstractStatefulPersistentActor.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Applies the results that came in from a handler, emitting any events, and responding to sender().
 */
protected void handleResults(CommandHandler.Results<E> results) {
    Option<Object> error = results.getValidationError(lastSequenceNr());
    if (error.isDefined()) {
        log.debug("  invalid: {}", error.get());
        sender().tell(error.get(), self());
    } else if (results.isAlreadyApplied()) {
        log.debug("  was already applied.");
        sender().tell(results.getIdempotentReply(lastSequenceNr()), self());
    } else {
        Seq<E> events = results.getEventsToEmit();
        log.debug("  emitting {}", events);
        if (events.isEmpty()) {
            sender().tell(results.getReply(events, lastSequenceNr()), self());
        } else {
            if (lastSequenceNr() == 0) {
                validateFirstEvent(events.head());
            }
            AtomicInteger need = new AtomicInteger(events.size());
            persistAllEvents(events, evt -> {
                if (need.decrementAndGet() == 0) {
                    sender().tell(results.getReply(events, lastSequenceNr()), self());
                }
            });
        }
    }
}
 
Example 4
Source File: TagWriteProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
@Override
 public String toString() {
     StringBuilder msg = new StringBuilder("<");
     msg.append(name.map(Object::toString).getOrElse("*"));
     msg.append(">");
     Seq<WriteProtocol<XMLEvent,?>> protocols = attrProtocols.appendAll(otherProtocols);
     if (!protocols.isEmpty()) {
         msg.append(" with ");
         msg.append(protocols.map(p -> p.toString()).mkString(", "));
     }
     return msg.toString();
}
 
Example 5
Source File: CassandraSession.java    From ts-reaktive with MIT License 5 votes vote down vote up
public static CompletionStage<Done> executeInitStatements(Session session, Seq<String> statements) {
    if (statements.isEmpty()) {
        return CompletableFuture.completedFuture(Done.getInstance());
    }

    return toJava(session.executeAsync(statements.head())).thenCompose(rs -> executeInitStatements(session, statements.tail()));
}
 
Example 6
Source File: MultiWriteProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
@Override
public Writer<CsvEvent, T> writer() {
    @SuppressWarnings("unchecked")
    Seq<Writer<CsvEvent, Object>> writers = protocols.map(c -> (Writer<CsvEvent,Object>) c.writer());
    
    return new Writer<CsvEvent,T>() {
        @Override
        public Seq<CsvEvent> apply(T value) {
            return emit(i -> writers.apply(i).apply(getters.apply(i).apply(value)));
        }

        @Override
        public Seq<CsvEvent> reset() {
            return emit(i -> writers.apply(i).reset());
        }
        
        private Seq<CsvEvent> emit(Function1<Integer, Seq<CsvEvent>> getEvents) {
            List<Seq<CsvEvent>> resultRows = new ArrayList<>();
            for (int writerIdx = 0; writerIdx < writers.size(); writerIdx++) {
                Seq<CsvEvent> events = getEvents.apply(writerIdx);
                int row = 0;
                if (events.isEmpty()) {
                    log.warn("{} did not emit any events. Emitting empty column instead.", protocols.apply(writerIdx));
                    events = Vector.of(CsvEvent.endRecord());
                } else {
                    if (!events.endsWith(Vector.of(CsvEvent.endValue(), CsvEvent.endRecord()))) {
                        throw new IllegalArgumentException("Expecting nested writer to end its write with endValue, endRecord but did not: " + events);
                    }
                    events = events.dropRight(1);
                    while (!events.isEmpty()) {
                        Seq<CsvEvent> rowEvents = events.takeWhile(e -> !(e instanceof CsvEvent.EndRecord));
                        log.debug("Row {}, writer {}: {}", row, writerIdx, rowEvents);
                        events = events.drop(rowEvents.size());
                        if (!events.isEmpty()) {
                            // drop the endRecord() separator between two rows of a single inner write
                            events = events.drop(1);
                        }
                        while (row >= resultRows.size()) {
                            if (row == resultRows.size()) {
                                // empty columns until current writer
                                resultRows.add(Vector.fill(writerIdx, () -> CsvEvent.endValue()));
                            } else {
                                // completely empty row
                                resultRows.add(Vector.fill(writers.size(), () -> CsvEvent.endValue()));
                            }
                        }
                        resultRows.set(row, resultRows.get(row).appendAll(rowEvents));
                        row++;
                    }
                }
                // fill the rows that weren't emitted from this writer (but were from others) with empty columns
                while (row < resultRows.size()) {
                    resultRows.set(row, resultRows.get(row).append(CsvEvent.endValue()));
                    row++;
                }
            }
            return Vector.ofAll(resultRows).map(row -> row.append(CsvEvent.endRecord())).fold(Vector.empty(), (a,b) -> a.appendAll(b));
        }
    };
}