本文整理汇总了Java中io.vavr.control.Try类的典型用法代码示例。如果您正苦于以下问题:Java Try类的具体用法?Java Try怎么用?Java Try使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Try类属于io.vavr.control包,在下文中一共展示了Try类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testReplaceColumn
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void testReplaceColumn() {
DataTable oldTable = createDataTable();
IDataColumn newCol = createDoubleColumn();
Try<DataTable> newTable = oldTable.columns().replace("IntegerCol", newCol);
assertTrue(newTable.isSuccess());
assertTrue(oldTable.columns().count() == 3);
assertTrue(newTable.get().columns().count() == 3);
// Check the correct column was replaced.
assertTrue(newTable.get().column(0).name().equals("StringCol"));
assertTrue(newTable.get().column(1).name().equals("DoubleCol"));
assertTrue(newTable.get().column(2).name().equals("BooleanCol"));
}
示例2: listFiles_existing_folder
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void listFiles_existing_folder() throws IOException {
final Try<Path> pathUtilsTestFolder = Paths.getPathForResource(PATH_UTIL_TESTS_FOLDER);
assertThat(pathUtilsTestFolder.isSuccess()).isTrue();
final List<Path> files = Paths.listFiles(pathUtilsTestFolder.get());
assertThat(files.size()).isEqualTo(2);
final List<String> fileNames = files.stream()
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toList());
assertThat(fileNames).containsExactlyInAnyOrder(
EXISTING_FILE_NAME,
EXISTING_ANOTHER_FILE_NAME
);
}
示例3: testInsertColumn
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void testInsertColumn() {
DataTable oldTable = createDataTable();
IDataColumn newCol = createDoubleColumn();
Try<DataTable> newTable = oldTable.columns().insert(0, newCol);
assertTrue(newTable.isSuccess());
assertTrue(oldTable.columns().count() == 3);
assertTrue(newTable.get().columns().count() == 4);
// Check the column was inserted in the correct place.
assertTrue(newTable.get().column(0).name().equals("DoubleCol"));
assertTrue(newTable.get().column(1).name().equals("StringCol"));
assertTrue(newTable.get().column(2).name().equals("IntegerCol"));
}
示例4: value
import io.vavr.control.Try; //导入依赖的package包/类
@Override
public final Try<T> value() {
if(results.isEmpty()) {
throw new IllegalStateException("Attempt to combine an empty set");
}
return results.map(Result::value)
.reduce((t1, t2) -> {
if(t1.isFailure() || t2.isFailure()) {
return Try.failure(
new RuntimeException(
String.join("\r\n", issues())
)
);
}
return Try.success(
combineOperator.apply(t1.get(), t2.get())
);
});
}
示例5: getAvailablePort
import io.vavr.control.Try; //导入依赖的package包/类
public static Try<Integer> getAvailablePort() {
synchronized (ASSIGNED_PORTS_MONITOR) {
return Try.of(() -> {
int hostPort;
do {
final ServerSocket serverSocket = new ServerSocket(0);
hostPort = serverSocket.getLocalPort();
serverSocket.close();
} while (assignedPorts.contains(hostPort));
assignedPorts = assignedPorts.add(hostPort);
return hostPort;
});
}
}
示例6: testSimpleQuickSort
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void testSimpleQuickSort() {
DataTable table = DataTableBuilder
.create("NewTable")
.withColumn(Integer.class, "IntCol", List.of(3, 20, 4, 18, 0, -30, 100))
.build().get();
Try<DataView> view = table.quickSort("IntCol", SortOrder.Descending);
assertTrue(view.isSuccess());
// Check IntCol values are in descending order.
assertTrue(view.get().row(0).getAs(Integer.class, "IntCol") == 100);
assertTrue(view.get().row(1).getAs(Integer.class, "IntCol") == 20);
assertTrue(view.get().row(2).getAs(Integer.class, "IntCol") == 18);
assertTrue(view.get().row(3).getAs(Integer.class, "IntCol") == 4);
assertTrue(view.get().row(4).getAs(Integer.class, "IntCol") == 3);
assertTrue(view.get().row(5).getAs(Integer.class, "IntCol") == 0);
assertTrue(view.get().row(6).getAs(Integer.class, "IntCol") == -30);
}
示例7: actionByColumnName
import io.vavr.control.Try; //导入依赖的package包/类
private Try<DataTable> actionByColumnName(String columnName, Function<Integer, Try<DataTable>> action) {
Integer idx = columnIdxByName(columnName);
return idx < 0
? error("Column not found with name " + columnName)
: action.apply(idx);
}
示例8: loadStylesheet
import io.vavr.control.Try; //导入依赖的package包/类
@Override
protected String loadStylesheet() {
return Try.of(() -> stylesheetFilePath)
.mapTry(Files::readAllBytes)
.map(String::new)
.getOrElseThrow((Function<? super Throwable, RuntimeException>) RuntimeException::new);
}
示例9: testDataReplaceRowValues
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void testDataReplaceRowValues() {
DataTable table = createDataTable();
Try<DataTable> result = table.rows().replaceValues(2, "ZZ", 100, true);
assertTrue(result.isSuccess());
testDataTableOnReplace(result.get());
}
示例10: testTryNotNullValidArgument
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void testTryNotNullValidArgument() {
Try<Integer> result = Guard.tryNotNull(12345, "MyArgName");
assertTrue(result.isSuccess());
assertTrue(result.get() == 12345);
}
示例11: testDataInsertRowWithInvalidIndex
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void testDataInsertRowWithInvalidIndex() {
DataTable table = createDataTable();
// Insert data at an invalid index.
Object[] rowValues = { "ZZ", 100, true };
Try<DataTable> result = table.rows().insert(200, rowValues);
assertTrue(result.isFailure());
assertTrue(result.getCause() instanceof IndexOutOfBoundsException);
}
示例12: tryGetAs
import io.vavr.control.Try; //导入依赖的package包/类
/**
* Returns the value of a particular row column as a specific type.
* This method performs bounds checks and type checks. Any errors
* will return a Try.failure.
*
* @param type The data type.
* @param colName The name of the column.
* @param <T> The value type.
* @return Returns the value at the specified index.
*/
public <T> Try<T> tryGetAs(Class<T> type, String colName) {
// Get the column as it's typed version.
Try<DataColumn<T>> col = this.table.columns()
.tryGet(colName)
.flatMap(c -> c.asType(type));
return col.isFailure()
? Try.failure(col.getCause())
: Try.success(col.get().valueAt(this.rowIdx));
}
示例13: loadNode
import io.vavr.control.Try; //导入依赖的package包/类
@Override
public <T extends Node> Try<T> loadNode(final FxmlNode nodeInfo, final Class<T> nodeClass) {
return this.loadNodeImpl(
this.getSingleStageFxmlLoader(nodeInfo),
nodeInfo,
nodeClass
);
}
示例14: loadNodeImpl
import io.vavr.control.Try; //导入依赖的package包/类
/**
* This method acts just like {@link #loadNode(FxmlNode)} but with no
* autoconfiguration of controller binding and stylesheet application.
*/
private <T extends Node> Try<T> loadNodeImpl(final FxmlLoader fxmlLoader, final FxmlNode fxmlNode, final Class<T> clazz) {
final String filePath = this.filePath(fxmlNode);
fxmlLoader.setLocation(getUrlForResource(filePath));
final Try<T> loadResult = Try.of(fxmlLoader::load).map(clazz::cast);
loadResult.onSuccess(fxmlLoader::onSuccess).onFailure(fxmlLoader::onFailure);
return this.applyStylesheetIfNeeded(
fxmlNode,
loadResult
);
}
示例15: testValidToTypedColumn
import io.vavr.control.Try; //导入依赖的package包/类
@Test
public void testValidToTypedColumn() {
IDataColumn column = createIntegerColumn();
Try<DataColumn<Integer>> typedCol = column.asType(Integer.class);
assertTrue(typedCol.isSuccess());
assertTrue(typedCol.get().valueAt(1) == 7);
}