本文整理汇总了Java中io.vavr.control.Try.of方法的典型用法代码示例。如果您正苦于以下问题:Java Try.of方法的具体用法?Java Try.of怎么用?Java Try.of使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vavr.control.Try
的用法示例。
在下文中一共展示了Try.of方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
});
}
}
示例2: getPublicFacingIP
import io.vavr.control.Try; //导入方法依赖的package包/类
public static Try<String> getPublicFacingIP() {
return Try.of(() -> {
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
final NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp() || iface.isVirtual() || iface.isPointToPoint()) {
continue;
}
final Enumeration<InetAddress> addresses = iface.getInetAddresses();
while (addresses.hasMoreElements()) {
final InetAddress addr = addresses.nextElement();
final String ip = addr.getHostAddress();
if (Inet4Address.class == addr.getClass()) {
return ip;
}
}
}
throw new Exception("Could not find public facing IP");
});
}
示例3: registerAccessToken
import io.vavr.control.Try; //导入方法依赖的package包/类
public Optional<AccessToken> registerAccessToken(final RequestToken requestToken, final String pinCode) {
log.info("Registering token {} with pincode {}", requestToken.toString(), pinCode);
final Try<AccessToken> tryAccessToken = Try.of(() -> {
// Don't refactor expression lambda into statement lambda. It's too
// long to be treated that way.
//noinspection CodeBlock2Expr
return this.twitter.getOAuthAccessToken(requestToken, pinCode);
});
if (tryAccessToken.isFailure()) {
log.info("Could not get access token! An error was thrown!");
return Optional.empty();
}
final AccessToken successAccessToken = tryAccessToken.get();
this.twitter.setOAuthAccessToken(successAccessToken);
log.info(
"Successfully got access token for user @{}! {}",
successAccessToken.getScreenName(),
successAccessToken.toString()
);
this.accessToken = successAccessToken;
this.sessionManager.addNewSession(this);
return Optional.of(successAccessToken);
}
示例4: remove
import io.vavr.control.Try; //导入方法依赖的package包/类
/**
* Attempts to remove an existing item at the specified index.
*
* @param index The index to remove the item at.
* @return Returns a Success with the new modified DataColumn, or a Failure.
*/
@Override
public Try<IDataColumn> remove(Integer index) {
return Try.of(() -> createColumn(this.data.removeAt(index)));
}
示例5: insertItem
import io.vavr.control.Try; //导入方法依赖的package包/类
/**
* Inserts the item at the specified index.
*
* @param index The index to insert the item at.
* @param value The item to insert.
* @return Returns a new DataColumn with the new item inserted.
*/
public Try<DataColumn<T>> insertItem(Integer index, T value) {
return Try.of(() -> createColumn(this.data.insert(index, value)));
}
示例6: replaceItem
import io.vavr.control.Try; //导入方法依赖的package包/类
/**
* Replaces the existing item at the specified index with the new item.
*
* @param index The index to replace the existing item.
* @param value The new item to replace the existing one.
* @return Returns a new DataColumn with the specified item replaced.
*/
public Try<DataColumn<T>> replaceItem(Integer index, T value) {
return Try.of(() -> createColumn(this.data.update(index, value)));
}
示例7: removeItem
import io.vavr.control.Try; //导入方法依赖的package包/类
/**
* Removes the item at the specified index.
*
* @param index The index to remove the item at.
* @return Returns a new DataColumn with the specified item removed.
*/
public Try<DataColumn<T>> removeItem(Integer index) {
return Try.of(() -> createColumn(this.data.removeAt(index)));
}