本文整理汇总了Java中scala.util.Try类的典型用法代码示例。如果您正苦于以下问题:Java Try类的具体用法?Java Try怎么用?Java Try使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Try类属于scala.util包,在下文中一共展示了Try类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNow
import scala.util.Try; //导入依赖的package包/类
@Override
public T getNow(T valueIfAbsent) throws ExecutionException {
Preconditions.checkNotNull(scalaFuture);
Option<Try<T>> value = scalaFuture.value();
if (value.isDefined()) {
Try<T> tri = value.get();
if (tri instanceof Success) {
return ((Success<T>)tri).value();
} else {
throw new ExecutionException(((Failure<T>)tri).exception());
}
} else {
return valueIfAbsent;
}
}
示例2: schemaCheck
import scala.util.Try; //导入依赖的package包/类
@RequestMapping(SCHEMA_CHECK_URI)
public SchemaCheckResult schemaCheck(@RequestParam(value="schema") String schema,
@RequestParam(value="schemaFormat") String schemaFormat,
@RequestParam(value="schemaEngine") String schemaEngine,
@RequestParam(value="resultFormat") String resultFormat) {
Option<String> base = Option.apply(null);
Try<Schema> t = Schemas.fromString(schema,schemaFormat, schemaEngine, base);
if (t.isSuccess()) {
Schema s = t.get();
Try<String> tstr = s.serialize(resultFormat);
if (tstr.isSuccess()) {
return new SchemaCheckResult(schema,schemaFormat,resultFormat,tstr.get());
} else {
return new SchemaCheckResult(schema,schemaFormat,resultFormat,
"Error: " + tstr);
}
} else
return new SchemaCheckResult(schema,schemaFormat,resultFormat,
"Error: " + t);
}
示例3: getInputFields
import scala.util.Try; //导入依赖的package包/类
private List<Object> getInputFields(IndexedRecord inputRecord, String columnName) {
// Adapt non-avpath syntax to avpath.
// TODO: This should probably not be automatic, use the actual syntax.
if (!columnName.startsWith("."))
columnName = "." + columnName;
Try<scala.collection.immutable.List<Evaluator.Ctx>> result = wandou.avpath.package$.MODULE$.select(inputRecord,
columnName);
List<Object> values = new ArrayList<Object>();
if (result.isSuccess()) {
for (Evaluator.Ctx ctx : JavaConversions.asJavaCollection(result.get())) {
values.add(ctx.value());
}
} else {
// Evaluating the expression failed, and we can handle the exception.
Throwable t = result.failed().get();
throw ProcessingErrorCode.createAvpathSyntaxError(t, columnName, -1);
}
return values;
}
示例4: from
import scala.util.Try; //导入依赖的package包/类
public static <T> ComposableFuture<T> from(final FutureProvider<T> source) {
return ComposableFutures.build(consumer -> {
final Future<T> future = source.provide();
future.onComplete(new AbstractFunction1<Try<T>, Void>() {
@Override public Void apply(final Try<T> res) {
if (res.isSuccess()) {
consumer.consume(com.outbrain.ob1k.concurrent.Try.fromValue(res.get()));
} else {
consumer.consume(com.outbrain.ob1k.concurrent.Try.fromError(res.failed().get()));
}
return null;
}
}, ctx);
});
}
示例5: detectValueCastFromCall
import scala.util.Try; //导入依赖的package包/类
@Override
public Try<Option<ValueCast<Level>>> detectValueCastFromCall(StaticInvokeExpr e) {
SootMethod m = e.getMethod();
BiFunction<TypeView<Level>, TypeView<Level>, Try<Option<ValueCast<Level>>>>
makeCast =
(t1, t2) -> {
Optional<ValueCast<Level>> result = Interop.asJavaStream(Vars.getAll(e.getArg(0))).findFirst().map(value -> makeValueCast(t1, t2, Option.apply(value)));
return new Success<>(Interop.asScalaOption(result));
};
if (castEquals(castHighToDyn, m)) {
return makeCast.apply(THIGH, DYN);
} else if (castEquals(castLowToDyn, m)) {
return makeCast.apply(TLOW, DYN);
} else if (castEquals(castDynToLow, m)) {
return makeCast.apply(DYN, TLOW);
} else if (castEquals(castDynToHigh, m)) {
return makeCast.apply(DYN, THIGH);
} else {
return new Success<>(Option.empty());
}
}
示例6: save
import scala.util.Try; //导入依赖的package包/类
@Override
public void save(List<User> users) {
List<Pair<User, Try<String>>> result = writeUsersWithExceptions(users);
int errors = 0;
for (Pair<User, Try<String>> pair : result) {
logger.info("For user " + pair.getLeft() + " operation got " + pair.getRight());
if (pair.getRight().isFailure())
errors += 1;
}
if (errors != 0) {
throw new IllegalStateException("The write operation thrown at least one exception" + result);
}
}
示例7: newTransactionContextWrapper
import scala.util.Try; //导入依赖的package包/类
final TransactionContextWrapper newTransactionContextWrapper(final TransactionProxy parent,
final String shardName) {
final TransactionContextWrapper transactionContextWrapper =
new TransactionContextWrapper(parent.getIdentifier(), actorContext);
Future<PrimaryShardInfo> findPrimaryFuture = findPrimaryShard(shardName, parent.getIdentifier());
if (findPrimaryFuture.isCompleted()) {
Try<PrimaryShardInfo> maybe = findPrimaryFuture.value().get();
if (maybe.isSuccess()) {
onFindPrimaryShardSuccess(maybe.get(), parent, shardName, transactionContextWrapper);
} else {
onFindPrimaryShardFailure(maybe.failed().get(), parent, shardName, transactionContextWrapper);
}
} else {
findPrimaryFuture.onComplete(new OnComplete<PrimaryShardInfo>() {
@Override
public void onComplete(final Throwable failure, final PrimaryShardInfo primaryShardInfo) {
if (failure == null) {
onFindPrimaryShardSuccess(primaryShardInfo, parent, shardName, transactionContextWrapper);
} else {
onFindPrimaryShardFailure(failure, parent, shardName, transactionContextWrapper);
}
}
}, actorContext.getClientDispatcher());
}
return transactionContextWrapper;
}
示例8: hostLevel
import scala.util.Try; //导入依赖的package包/类
public <U> CompletionStage<U> hostLevel(Optional<String> s,
Function<Pair<Try<HttpResponse>, Integer>, CompletionStage<U>> responseHandler) {
return Source.single(Pair.create(HttpRequest.create().withUri(getUri(s)), 42))
.via(poolClientFlow)
.runWith(Sink.head(), materializer)
.thenComposeAsync(responseHandler);
}
示例9: dataCheck
import scala.util.Try; //导入依赖的package包/类
@RequestMapping(DATA_CHECK_URI)
public DataCheckResult dataCheck(@RequestParam(value="data") String data,
@RequestParam(value="dataFormat") String dataFormat,
@RequestParam(value="resultFormat") String resultFormat) {
Option<String> base = Option.apply(null);
Try<RDFAsJenaModel> t = RDFAsJenaModel.fromChars(data,dataFormat, base);
if (t.isSuccess()) {
RDFReader s = t.get();
String tstr = s.serialize(resultFormat);
return new DataCheckResult(data,dataFormat,resultFormat,tstr);
} else
return new DataCheckResult(data,dataFormat,resultFormat,"Error: " + t);
}
示例10: validate
import scala.util.Try; //导入依赖的package包/类
@RequestMapping(VALIDATE_URI)
public ValidateResult validate(
@RequestParam(value="schema") String schema,
@RequestParam(value="schemaFormat") String schemaFormat,
@RequestParam(value="schemaEngine") String schemaEngine,
@RequestParam(value="data") String data,
@RequestParam(value="dataFormat") String dataFormat,
@RequestParam(value="resultFormat") String resultFormat) {
Option<String> base = Option.apply(null);
List<String> emptyErrors = new ArrayList<String>();
Try<Schema> t = Schemas.fromString(schema,schemaFormat, schemaEngine, base);
if (t.isSuccess()) {
Schema s = t.get();
Try<String> tstr = s.serialize(resultFormat);
if (tstr.isSuccess()) {
Try<RDFAsJenaModel> trdf = RDFAsJenaModel.fromChars(data,dataFormat,base);
if (trdf.isSuccess()) {
Result r = s.validate(trdf.get());
String strResult = r.serialize(resultFormat);
// TODO: Populate errors and solutions...
new ValidateResult(schema,schemaFormat,schemaEngine,data,dataFormat,emptyErrors,r.isValid(),
strResult, resultFormat);
} else
new ValidateResult(schema,schemaFormat,schemaEngine,data,dataFormat,emptyErrors,false,
"Error: " + trdf, resultFormat);
return new ValidateResult(schema,schemaFormat,schemaEngine,data,dataFormat,emptyErrors,false,
"Error: " + t,resultFormat);
} else {
return new ValidateResult(schema,schemaFormat,schemaEngine,data,dataFormat,emptyErrors,false,
"Error: " + tstr,resultFormat);
}
} else {
return new ValidateResult(schema,schemaFormat,schemaEngine,data,dataFormat,emptyErrors,false,
"Error: " + t,resultFormat);
}
}
示例11: MyJavaSingleSessionSerializer
import scala.util.Try; //导入依赖的package包/类
public MyJavaSingleSessionSerializer(
Function1<SomeJavaComplexObject, String> toValue,
Function1<String, Try<SomeJavaComplexObject>> fromValue,
SessionSerializer<String, String> valueSerializer
) {
super(toValue, fromValue, valueSerializer);
}
示例12: main
import scala.util.Try; //导入依赖的package包/类
public static void main(String[] args) {
new MyJavaSingleSessionSerializer(
(JFunction1<SomeJavaComplexObject, String>) SomeJavaComplexObject::getValue,
(JFunction1<String, Try<SomeJavaComplexObject>>) value -> Try.apply((JFunction0<SomeJavaComplexObject>) () -> new SomeJavaComplexObject(value)),
SessionSerializers.StringToStringSessionSerializer
);
}
示例13: main
import scala.util.Try; //导入依赖的package包/类
public static void main(String[] args) {
new MyJavaMultiValueSessionSerializer(
(JFunction1<SomeJavaComplexObject, Map<String, String>>) sjco -> {
final java.util.Map<String, String> m = new HashMap<>();
m.put("value", sjco.getValue());
return MapConverters.toImmutableMap(m);
},
(JFunction1<Map<String, String>, Try<SomeJavaComplexObject>>) value ->
Try.apply((JFunction0<SomeJavaComplexObject>) () -> new SomeJavaComplexObject(value.get("value").get()))
);
}
示例14: validate
import scala.util.Try; //导入依赖的package包/类
@Override
public Try<MySQLConnection> validate(final MySQLConnection item) {
if (!item.isConnected()) {
return new Failure<>(new ConnectionNotConnectedException(item));
}
if (item.isQuerying()) {
return new Failure<>(new ConnectionStillRunningQueryException(item.count(), false));
}
return new Success<>(item);
}
示例15: scalaToVertx
import scala.util.Try; //导入依赖的package包/类
public static <T> Future<T> scalaToVertx(scala.concurrent.Future<T> future, ExecutionContext ec) {
Future<T> fut = Future.future();
future.onComplete(new AbstractFunction1<Try<T>, Void>() {
@Override
public Void apply(Try<T> v1) {
if (v1.isSuccess()) {
fut.complete(v1.get());
} else {
fut.fail(v1.failed().get());
}
return null;
}
}, ec);
return fut;
}