Java Code Examples for io.vavr.control.Try#success()

The following examples show how to use io.vavr.control.Try#success() . 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: ValueProtocol.java    From ts-reaktive with MIT License 6 votes vote down vote up
@Override
public Reader<CsvEvent, String> reader() {
    return new Reader<CsvEvent, String>() {
        private StringBuilder buffer = new StringBuilder();
        
        @Override
        public Try<String> reset() {
            Try<String> result = (buffer.length() > 0) ? Try.success(buffer.toString()) : none();
            buffer = new StringBuilder();
            return result;
        }

        @Override
        public Try<String> apply(CsvEvent event) {
            if (event instanceof CsvEvent.Text) {
                buffer.append(CsvEvent.Text.class.cast(event).getText());
            }
            return none();
        }
    };
}
 
Example 2
Source File: DataRow.java    From java-datatable with Apache License 2.0 5 votes vote down vote up
/**
 * Builds an instance of a DataRow.
 * Row Index is validated before creation, returning a Failure on error.
 *
 * @param table The DataTable the DataRow is pointing to.
 * @param rowIdx The row index.
 * @return Returns a DataRow wrapped in a Try.
 */
public static Try<DataRow> build(DataTable table, Integer rowIdx) {
    Guard.notNull(table, "table");

    // Check row index bounds, and return new DataRow if success.
    return VectorExtensions.outOfBounds(table.rowCount(), rowIdx)
            ? DataTableException.tryError("Invalid row index for DataRow.")
            : Try.success(new DataRow(table, rowIdx));
}
 
Example 3
Source File: SelectedSubTagProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
@Override
public Reader<XMLEvent, XMLEvent> reader() {
    return new Reader<XMLEvent, XMLEvent>() {
        private boolean matched = false;
        private int level = 0;
        private int matchLevel = -1;

        @Override
        public Try<XMLEvent> reset() {
            matched = false;
            level = 0;
            matchLevel = -1;
            return ReadProtocol.none();
        }

        @Override
        public Try<XMLEvent> apply(XMLEvent event) {
            if (!matched && event.isStartElement() && names.contains(event.asStartElement().getName())) {
                matchLevel = level;
                level++;
                matched = true;
                return Try.success(event);
            } else if (matched && event.isEndElement() && level == matchLevel + 1) {
                level--;
                matchLevel = -1;
                matched = false;
                return Try.success(event);
            } else {
                if (event.isStartElement()) {
                    level++;
                } else if (event.isEndElement()) {
                    level--;
                }

                return (matched) ? Try.success(event) : ReadProtocol.none();
            }
        }
    };
}
 
Example 4
Source File: DataSort.java    From java-datatable with Apache License 2.0 5 votes vote down vote up
/**
 * Validates that all the columns specified in the collection of items
 * to sort are actually contained and identifiable in the DataTable.
 *
 * @param table The table to sort.
 * @param sortItems The collection of sort items.
 * @return Returns validation as a Try.
 */
private static Try<Seq<IDataColumn>> validateSortColumnIdentity(DataTable table, Seq<SortItem> sortItems) {

    // Validate we can get the column for all sort items.
    Seq<Try<IDataColumn>> checkCols = sortItems.map(item -> item.getColumn(table));

    // Any failures, return error else return columns.
    return checkCols.find(Try::isFailure).isEmpty()
            ? Try.success(checkCols.map(Try::get))
            : DataTableException.tryError("Column for Sort Item not found.");
}
 
Example 5
Source File: DataSort.java    From java-datatable with Apache License 2.0 5 votes vote down vote up
/**
 * Validates that all the columns being sorted are comparable.
 *
 * @param columns The columns to sort.
 * @return Returns the result as a Try : Success / Failure.
 */
private static Try<Void> validateColumnsAreComparable(Seq<IDataColumn> columns) {
    Option<IDataColumn> invalidCol = columns.find(col -> !col.IsComparable());

    return invalidCol.isEmpty()
            ? Try.success(null)
            : DataTableException.tryError("Column '" + invalidCol.get().name() + "' doesn't support comparable.");
}
 
Example 6
Source File: BodyEventsProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
@Override
public Reader<XMLEvent, Characters> reader() {
    return new Reader<XMLEvent,Characters>() {
        private int level = 0;
        
        @Override
        public Try<Characters> reset() {
            level = 0;
            return none();
        }

        @Override
        public Try<Characters> apply(XMLEvent evt) {
            if (level == 0 && evt.isCharacters()) {
                return Try.success(evt.asCharacters());
            } else if (evt.isStartElement()) {
                level++;
                return none();
            } else if (evt.isEndElement()) {
                level--;
                return none();
            } else {
                return none();
            }
        }
        
    };
}
 
Example 7
Source File: AnyAttributeProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
@Override
public Reader<XMLEvent,Tuple2<QName,String>> reader() {
    return new Reader<XMLEvent,Tuple2<QName,String>>() {
        private int level = 0;

        @Override
        public Try<Tuple2<QName,String>> reset() {
            level = 0;
            return none();
        }

        @Override
        public Try<Tuple2<QName,String>> apply(XMLEvent evt) {
            if (level == 0 && evt.isAttribute()) {
                Attribute attr = Attribute.class.cast(evt);
                return Try.success(Tuple.of(attr.getName(), attr.getValue()));
            } else if (evt.isStartElement()) {
                level++;
                return none();
            } else if (evt.isEndElement()) {
                level--;
                return none();
            } else {
                return none();
            }
        }
    };
}
 
Example 8
Source File: DataColumnCollection.java    From java-datatable with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the IDataColumn by name.
 * Performs column name check, returns results in a Try.
 *
 * @param columnName The name of the IDataColumn.
 * @return Returns the IDataColumn.
 */
public Try<IDataColumn> tryGet(String columnName) {
    Integer idx = columnIdxByName(columnName);

    return idx < 0
            ? DataTableException.tryError("Invalid column name.")
            : Try.success(get(idx));
}
 
Example 9
Source File: StringMarshallable.java    From ts-reaktive with MIT License 4 votes vote down vote up
@Override
public Try<String> tryRead(String value) {
    return Try.success(value);
}
 
Example 10
Source File: DataRow.java    From java-datatable with Apache License 2.0 4 votes vote down vote up
private Try<Object> columnToValue(Try<IDataColumn> column) {
    return column.isSuccess()
            ? Try.success(column.get().valueAt(this.rowIdx))
            : Try.failure(column.getCause());
}
 
Example 11
Source File: SelectedTagProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
@Override
public Reader<XMLEvent, XMLEvent> reader() {
    return new Reader<XMLEvent, XMLEvent>() {
        private boolean matched = false;
        private int level = 0;

        @Override
        public Try<XMLEvent> reset() {
            matched = false;
            level = 0;
            return ReadProtocol.none();
        }

        @Override
        public Try<XMLEvent> apply(XMLEvent event) {
            if (level == 0) {
                if (event.isStartElement() && event.asStartElement().getName().equals(name)) {
                    level++;
                    matched = true;
                    return Try.success(event);
                } else if (event.isStartElement()) {
                    level++;
                    return ReadProtocol.none();
                } else {
                    return ReadProtocol.none();
                }
            } else if (matched && level == 1 && event.isEndElement()) {
                level--;
                matched = false;
                return Try.success(event);
            } else {
                if (event.isStartElement()) {
                    level++;
                } else if (event.isEndElement()) {
                    level--;
                }
                
                return (matched) ? Try.success(event) : ReadProtocol.none();
            }
        }
    };
}
 
Example 12
Source File: BodyProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
private BodyProtocol() {
    super(new Protocol<XMLEvent,String>(){
        Writer<XMLEvent,String> writer = Writer.of(value -> Vector.of(factory.createCharacters(value)));
        
        @Override
        public Writer<XMLEvent,String> writer() {
            return writer;
        }
        
        @Override
        public Class<? extends XMLEvent> getEventType() {
            return XMLEvent.class;
        }

        @Override
        public Reader<XMLEvent,String> reader() {
            return new Reader<XMLEvent,String>() {
                private int level = 0;
                private final List<String> buffer = new ArrayList<>();
                
                @Override
                public Try<String> reset() {
                    level = 0;
                    Try<String> result = Try.success(buffer.stream().collect(Collectors.joining()));
                    buffer.clear();
                    return result.get().isEmpty() ? none() : result;
                }

                @Override
                public Try<String> apply(XMLEvent evt) {
                    if (level == 0 && evt.isCharacters()) {
                        buffer.add(evt.asCharacters().getData());
                        return none();
                    } else if (evt.isStartElement()) {
                        level++;
                        return none();
                    } else if (evt.isEndElement()) {
                        level--;
                        return none();
                    } else {
                        return none();
                    }
                }
                
            };
        }
        
        @Override
        public String toString() {
            return "body";
        }
    }, XMLProtocol.locator);
}
 
Example 13
Source File: DataColumn.java    From java-datatable with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the generic data column as it's typed implementation.
 * If the types don't match, then it'll return Failure.
 *
 * @param type The type of the column.
 * @param <V> The type.
 * @return Returns the typed Data Column wrapped in a Try.
 */
@Override
@SuppressWarnings("unchecked")
public <V> Try<DataColumn<V>> asType(Class<V> type) {
    return this.type == type
            ? Try.success((DataColumn<V>)this)
            : DataTableException.tryError("Column type doesn't match type requested.");
}
 
Example 14
Source File: DataRowCollectionModifiable.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Validates the number of values equals the number of columns in the table.
 *
 * @param values The values.
 * @return Returns a sequence of ColumnValuePairs if valid.
 */
private Try<Seq<ColumnValuePair>> mapValuesToColumns(Seq<Object> values) {
    return values.length() != table.columns().count()
            ? DataTableException.tryError("Number of values does not match number of columns.")
            : Try.success(createIndexedColumnValuePair(values));
}
 
Example 15
Source File: DataColumn.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Adds / Appends and item to the end of the column.
 *
 * @param value The value to append.
 * @return Returns a new DataColumn with the new item appended.
 */
public Try<DataColumn<T>> addItem(T value) {
    return Try.success(createColumn(this.data.append(value)));
}
 
Example 16
Source File: DataColumn.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Builds a new DataColumn from the data at the specified row indexes.
 *
 * @param rowIndexes The rows which the new column data is to be built from.
 * @return Returns a new IDataColumn with just the rows specified.
 */
@Override
public Try<IDataColumn> buildFromRows(Seq<Integer> rowIndexes) {
    Seq<T> rowData = rowIndexes.map(this.data::get);
    return Try.success(new DataColumn<>(this.type, this.name, rowData));
}
 
Example 17
Source File: VectorExtensions.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Adds - Appends a new item to the end of the vector.
 *
 * @param vector The vector to add the item to.
 * @param item The item to add.
 * @param <T> The vector type.
 * @return Returns the new vector with the item added.
 */
public static <T> Try<Vector<T>> addItem(Vector<T> vector, T item) {
    return Try.success(vector.append(item));
}
 
Example 18
Source File: DataTable.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Validates the number of items in each column is the same.
 *
 * @param columns The columns to check.
 * @return Returns a Success or Failure.
 */
private static Try<Seq<IDataColumn>> validateColumnDataLength(Seq<IDataColumn> columns) {
    return columns.groupBy(col -> col.data().length()).length() > 1
            ? DataTableException.tryError("Columns have different lengths.")
            : Try.success(columns);
}
 
Example 19
Source File: DataTable.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Validates there are no duplicate columns names.
 *
 * @param columns The columns to check.
 * @return Returns a Success or Failure.
 */
private static Try<Seq<IDataColumn>> validateColumnNames(Seq<IDataColumn> columns) {
    return columns.groupBy(IDataColumn::name).length() != columns.length()
            ? DataTableException.tryError("Columns contain duplicate names.")
            : Try.success(columns);
}
 
Example 20
Source File: Guard.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Asserts the argument is not null. Returns in a Try.
 *
 * @param <T> The argument type.
 * @param argument The argument to check.
 * @param name The name of the argument.
 * @return Returns a Try testing the argument being null.
 */
public static <T> Try<T> tryNotNull(T argument, String name) {
    return argument == null
            ? Try.failure(new IllegalArgumentException("Invalid value [NULL] for argument " + name))
            : Try.success(argument);
}