下面列出了com.google.common.base.Optional#fromNullable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Optional<QueryResp> queryFromES(String body) {
logger.info("Call ES to query");
try {
ESQueryResponse restResponse = esService.query("searchindex", "product", null, body);
if (restResponse == null) {
return Optional.absent();
}
logger.info("search result total count " + restResponse.getHit().getTotal());
return Optional.fromNullable(new QueryResp(restResponse));
} catch (RestClientException e) {
logger.error("search is " + e);
}
return Optional.absent();
}
/**
* Parser for a bn_module_views notification. The function uses the moduleViewNotificationPattern
* to parse the incoming {@link PGNotification} into a {@link ViewNotificationContainer}.
*
* @param notification The {@link PGNotification} to parse.
* @param provider The {@link SQLProvider} to access the database.
*
* @return A {@link ViewNotificationContainer} with the parsed information.
*/
private ViewNotificationContainer parseModuleViewNotification(
final PGNotification notification, final SQLProvider provider) {
final Matcher matcher = moduleViewNotificationPattern.matcher(notification.getParameter());
if (!matcher.find()) {
throw new IllegalStateException("IE02743: compiled pattern: "
+ moduleViewNotificationPattern.toString() + " did not match notification: "
+ notification.getParameter());
}
final Integer viewId = Integer.parseInt(matcher.group(3));
final Optional<INaviView> view =
Optional.fromNullable(ViewManager.get(provider).getView(viewId));
final Optional<Integer> moduleId = Optional.fromNullable(Integer.parseInt(matcher.group(4)));
final Optional<INaviModule> module = Optional.fromNullable(provider.findModule(moduleId.get()));
final String databaseOperation = matcher.group(2);
return new ViewNotificationContainer(viewId,
view,
moduleId,
module,
Optional.<INaviProject>absent(),
databaseOperation);
}
/**
* Finds the first {@link Projection} node within a {@link ParsedQuery}.
*
* @param query - The query that will be searched. (not null)
* @return The first projection encountered if the query has one; otherwise absent.
*/
public Optional<Projection> findProjection(final ParsedQuery query) {
checkNotNull(query);
// When a projection is encountered for the requested index, store it in atomic reference and quit searching.
final AtomicReference<Projection> projectionRef = new AtomicReference<>();
query.getTupleExpr().visit(new AbstractQueryModelVisitor<RuntimeException>() {
@Override
public void meet(Projection projection) {
projectionRef.set(projection);
}
});
return Optional.fromNullable( projectionRef.get() );
}
@Test(expected = IllegalArgumentException.class)
public void testModuleViewInform2() throws CouldntLoadDataException {
final INaviModule module = new MockModule(provider);
final ViewNotificationContainer container =
new ViewNotificationContainer(view.getConfiguration().getId(),
Optional.fromNullable(view),
Optional.of(module.getConfiguration().getId()),
Optional.of(module),
Optional.<INaviProject>absent(),
"DELETE");
final PostgreSQLViewNotificationParser parser = new PostgreSQLViewNotificationParser();
parser.inform(Lists.<ViewNotificationContainer>newArrayList(container), provider);
}
public static Optional<Class> useType(Class beanType) {
if (Proxy.class.isAssignableFrom(beanType)) {
return Optional.absent();
}
if (Class.class.getName().equals(beanType.getName())) {
return Optional.absent();
}
return Optional.fromNullable(beanType);
}
private String getRequestDetails(Method httpMethod, String url) {
Optional<String> requestDetails = Optional.absent();
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
requestDetails = Optional.fromNullable(RequestPrinter.print((FilterableRequestSpecification) requestSpecification, httpMethod.name(), url, LogDetail.ALL,
new PrintStream(os), true));
} catch (IOException e) {
logUtils.error("Unable to log 'Request Details', error occured during retrieving the information");
}
return requestDetails.or("");
}
public Optional<String> getFileFormat() {
String serdeLib = this.hivePartition.getTPartition().getSd().getSerdeInfo().getSerializationLib();
for (HiveSerDeWrapper.BuiltInHiveSerDe hiveSerDe : HiveSerDeWrapper.BuiltInHiveSerDe.values()) {
if (hiveSerDe.toString().equalsIgnoreCase(serdeLib)) {
return Optional.fromNullable(hiveSerDe.name());
}
}
return Optional.<String>absent();
}
private Optional<Artifact> getPchFile() {
if (!usePch) {
return Optional.absent();
}
Artifact pchHdr = null;
if (ruleContext.attributes().has("pch", BuildType.LABEL)) {
pchHdr = ruleContext.getPrerequisiteArtifact("pch", TransitionMode.TARGET);
}
return Optional.fromNullable(pchHdr);
}
@Override
public ItemStack getCraftingResult(InventoryCrafting inv) {
ItemStack targetStack = null;
ItemStack glassesStack = null;
for (ItemStack itemStack : InventoryUtils.asIterable(inv)) {
if (itemStack != null) {
if (itemStack.getItem() instanceof ItemGlasses) {
if (glassesStack != null) return null;
glassesStack = itemStack;
} else if (isSuitableItem(itemStack)) {
if (targetStack != null) return null;
targetStack = itemStack;
} else return null;
}
}
if (glassesStack == null || targetStack == null) return null;
final ItemGlasses glassesItem = (ItemGlasses)glassesStack.getItem();
Optional<Long> guid = Optional.fromNullable(glassesItem.getTerminalGuid(glassesStack));
final ItemStack result = targetStack.copy();
NbtGuidProviders.setTerminalGuid(result, guid);
return result;
}
public CreateTable(Table name,
List<TableElement> tableElements,
@Nullable List<CrateTableOption> crateTableOptions,
@Nullable GenericProperties genericProperties,
boolean ifNotExists)
{
this.name = name;
this.tableElements = tableElements;
this.ifNotExists = ifNotExists;
this.crateTableOptions = crateTableOptions != null ? crateTableOptions : ImmutableList.<CrateTableOption>of();
this.properties = Optional.fromNullable(genericProperties);
}
public synchronized Optional<Protos.FrameworkID> getFrameworkID() {
return Optional.fromNullable(frameworkId);
}
/**
* The cursor
* @param cursor
* @return
*/
@Override
public ReIndexRequestBuilder withCursor( final String cursor ) {
this.cursor = Optional.fromNullable( cursor );
return this;
}
@Override
public Optional<Organization> getOrganization() {
logger.info("Getting organization <- {}", organization);
return Optional.fromNullable(this.organization);
}
private void createLocalServerPort() {
Integer port = (Integer) mesosStormConf.get(CONF_MESOS_LOCAL_FILE_SERVER_PORT);
LOG.debug("LocalFileServer configured to listen on port: {}", port);
_localFileServerPort = Optional.fromNullable(port);
}
public Optional<MyriadContainerConfiguration> getContainerInfo() {
return Optional.fromNullable(containerInfo);
}
@VisibleForTesting
SalesforceSource(LineageInfo lineageInfo) {
this.lineageInfo = Optional.fromNullable(lineageInfo);
}
Optional<VideoTrack> getRemoteVideoTrack() {
return Optional.fromNullable(this.remoteVideoTrack);
}
private Optional<Connection> getCachedConnection(final String dataSourceName, final SQLType sqlType) {
String key = connectionMap.containsKey(dataSourceName) ? dataSourceName : MasterSlaveDataSource.getDataSourceName(dataSourceName, sqlType);
return Optional.fromNullable(connectionMap.get(key));
}
/**
* Loads a HttpClientConfigurator using the value of the {@link #HTTP_CLIENT_CONFIGURATOR_TYPE_FULL_KEY}
* property in the state.
*/
public HttpClientConfiguratorLoader(State state) {
this(Optional.<String>fromNullable(state.getProp(HTTP_CLIENT_CONFIGURATOR_TYPE_FULL_KEY)));
}
/**
* 获取分表分片键值.
*
* @param shardingKey 分片键
* @return 分表分片键值
*/
public static Optional<ShardingValue<?>> getTableShardingValue(final ShardingKey shardingKey) {
return isUseShardingHint() ? Optional.<ShardingValue<?>>fromNullable(HINT_MANAGER_HOLDER.get().getTableShardingValue(shardingKey)) : Optional.<ShardingValue<?>>absent();
}