List of usage examples for com.google.common.base Optional or
@Beta public abstract T or(Supplier<? extends T> supplier);
From source file:io.crate.analyze.relations.RelationNormalizer.java
@Nullable private static List<Symbol> pushGroupBy(Optional<List<Symbol>> childGroupBy, Optional<List<Symbol>> parentGroupBy) { assert !(childGroupBy.isPresent() && parentGroupBy .isPresent()) : "Cannot merge 'group by' if exists in both parent and child relations"; return childGroupBy.or(parentGroupBy).orNull(); }
From source file:gobblin.util.ConfigUtils.java
/** * Finds a list of properties whose keys are complete prefix of other keys. This function is * meant to be used during conversion from Properties to typesafe Config as the latter does not * support this scenario./* www. j a va2 s .c o m*/ * @param properties the Properties collection to inspect * @param keyPrefix an optional key prefix which limits which properties are inspected. * */ public static Set<String> findFullPrefixKeys(Properties properties, Optional<String> keyPrefix) { TreeSet<String> propNames = new TreeSet<>(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { String entryKey = entry.getKey().toString(); if (StringUtils.startsWith(entryKey, keyPrefix.or(StringUtils.EMPTY))) { propNames.add(entryKey); } } Set<String> result = new HashSet<>(); String lastKey = null; Iterator<String> sortedKeysIter = propNames.iterator(); while (sortedKeysIter.hasNext()) { String propName = sortedKeysIter.next(); if (null != lastKey && propName.startsWith(lastKey + ".")) { result.add(lastKey); } lastKey = propName; } return result; }
From source file:norbert.mynemo.dataimport.FileImporter.java
/** * Returns a rating file that can parse the ratings contained in the given file. * * @throws UnsupportedOperationException if the file cannot be parsed *//* w w w . j a v a 2 s . co m*/ private static RatingImporter getFile(String ratingFilepath, Collection<String> mappingFilepaths, Optional<String> user) throws IOException { for (String mappingFilepath : mappingFilepaths) { if (CkRatingImporter.canImport(ratingFilepath, mappingFilepath)) { return new CkRatingImporter(ratingFilepath, mappingFilepath); } if (TenMillionRatingImporter.canImport(ratingFilepath, mappingFilepath)) { return new TenMillionRatingImporter(ratingFilepath, mappingFilepath); } } if (MovieLensRatingImporter.canImport(ratingFilepath)) { return new MovieLensRatingImporter(ratingFilepath, user.or(DEFAULT_USER_ID)); } // the Mynemo rating file must stay last because it does not use headers, so the parser can // parse several file formats. if (MynemoRatingImporter.canImport(ratingFilepath)) { return new MynemoRatingImporter(ratingFilepath); } throw new UnsupportedOperationException("Unable to convert the file \"" + ratingFilepath + "\"."); }
From source file:gobblin.util.ConfigUtils.java
/** * Convert all the keys that start with a <code>prefix</code> in {@link Properties} to a * {@link Config} instance. The method also tries to guess the types of properties. * * <p>/*from ww w . j ava 2 s. c o m*/ * This method will throw an exception if (1) the {@link Object#toString()} method of any two keys in the * {@link Properties} objects returns the same {@link String}, or (2) if any two keys are prefixes of one another, * see the Java Docs of {@link ConfigFactory#parseMap(Map)} for more details. * </p> * * @param properties the given {@link Properties} instance * @param prefix of keys to be converted * @return a {@link Config} instance */ public static Config propertiesToTypedConfig(Properties properties, Optional<String> prefix) { Map<String, Object> typedProps = guessPropertiesTypes(properties); ImmutableMap.Builder<String, Object> immutableMapBuilder = ImmutableMap.builder(); for (Map.Entry<String, Object> entry : typedProps.entrySet()) { if (StringUtils.startsWith(entry.getKey(), prefix.or(StringUtils.EMPTY))) { immutableMapBuilder.put(entry.getKey(), entry.getValue()); } } return ConfigFactory.parseMap(immutableMapBuilder.build()); }
From source file:org.apache.rya.indexing.external.PcjIntegrationTestingUtil.java
/** * Creates a new PCJ Table in Accumulo and populates it by scanning an * instance of Rya for historic matches.// w w w .j a va 2 s.com * <p> * If any portion of this operation fails along the way, the partially * create PCJ table will be left in Accumulo. * * @param ryaConn - Connects to the Rya that will be scanned. (not null) * @param accumuloConn - Connects to the accumulo that hosts the PCJ results. (not null) * @param pcjTableName - The name of the PCJ table that will be created. (not null) * @param sparql - The SPARQL query whose results will be loaded into the table. (not null) * @param resultVariables - The variables that are included in the query's resulting binding sets. (not null) * @param pcjVarOrderFactory - An optional factory that indicates the various variable orders * the results will be stored in. If one is not provided, then {@link ShiftVarOrderFactory} * is used by default. (not null) * @throws PcjException The PCJ table could not be create or the values from * Rya were not able to be loaded into it. */ public static void createAndPopulatePcj(final RepositoryConnection ryaConn, final Connector accumuloConn, final String pcjTableName, final String sparql, final String[] resultVariables, final Optional<PcjVarOrderFactory> pcjVarOrderFactory) throws PcjException { checkNotNull(ryaConn); checkNotNull(accumuloConn); checkNotNull(pcjTableName); checkNotNull(sparql); checkNotNull(resultVariables); checkNotNull(pcjVarOrderFactory); final PcjTables pcj = new PcjTables(); // Create the PCJ's variable orders. final PcjVarOrderFactory varOrderFactory = pcjVarOrderFactory.or(new ShiftVarOrderFactory()); final Set<VariableOrder> varOrders = varOrderFactory.makeVarOrders(new VariableOrder(resultVariables)); // Create the PCJ table in Accumulo. pcj.createPcjTable(accumuloConn, pcjTableName, varOrders, sparql); // Load historic matches from Rya into the PCJ table. populatePcj(accumuloConn, pcjTableName, ryaConn); }
From source file:com.complexible.common.openrdf.model.Graphs.java
/** * Return the contents of the given list by following the rdf:first/rdf:rest structure of the list * @param theGraph the graph/*from w w w . j av a2 s . c o m*/ * @param theRes the resource which is the head of the list * * @return the contents of the list. */ public static List<Value> asList(final Graph theGraph, final Resource theRes) { List<Value> aList = Lists.newArrayList(); Resource aListRes = theRes; while (aListRes != null) { Optional<Resource> aFirst = getResource(theGraph, aListRes, RDF.FIRST); Optional<Resource> aRest = getResource(theGraph, aListRes, RDF.REST); if (aFirst.isPresent()) { aList.add(aFirst.get()); } if (aRest.or(RDF.NIL).equals(RDF.NIL)) { aListRes = null; } else { aListRes = aRest.get(); } } return aList; }
From source file:com.eucalyptus.vm.VmInstanceMetadata.java
public static String getByKey(final VmInstance vm, final String pathArg) { final String path = Objects.firstNonNull(pathArg, ""); final String pathNoSlash; LOG.debug("Servicing metadata request:" + path); if (path.endsWith("/")) { pathNoSlash = path.substring(0, path.length() - 1); } else {//from w ww . j a va 2 s .co m pathNoSlash = path; } Optional<MetadataGroup> groupOption = Optional.absent(); for (final MetadataGroup metadataGroup : MetadataGroup.values()) { if (metadataGroup.providesPath(pathNoSlash) || metadataGroup.providesPath(path)) { groupOption = Optional.of(metadataGroup); } } final MetadataGroup group = groupOption.or(MetadataGroup.Core); final Map<String, String> metadataMap = Optional.fromNullable(group.apply(vm)) .or(Collections.<String, String>emptyMap()); final String value = metadataMap.get(path); return value == null ? metadataMap.get(pathNoSlash) : value; }
From source file:com.cloudera.director.aws.rds.RDSInstanceTemplate.java
/** * Returns the database engine, based on the specified optional engine and database type. * * @param databaseType the database type * @param optionalEngine the optional engine specified by the user * @return the specified engine, if present and nonempty, or the default engine for the * specified database type, otherwise// w w w . j ava 2 s . c o m */ private static String getEngine(Optional<String> optionalEngine, DatabaseType databaseType) { String engine = optionalEngine.or(""); if (engine.isEmpty()) { engine = RDSEngine.getDefaultEngine(databaseType).getEngineName(); } return engine; }
From source file:org.openqa.selenium.testing.drivers.ExternalDriverSupplier.java
private static Optional<Supplier<WebDriver>> createForExternalServer(Capabilities desiredCapabilities, Capabilities requiredCapabilities, Optional<Supplier<WebDriver>> delegate) { String externalUrl = System.getProperty(EXTERNAL_SERVER_URL_PROPERTY); if (externalUrl != null) { logger.info("Using external WebDriver server: " + externalUrl); URL url;/* www . j ava 2 s . c om*/ try { url = new URL(externalUrl); } catch (MalformedURLException e) { throw new RuntimeException("Invalid server URL: " + externalUrl, e); } Supplier<WebDriver> defaultSupplier = new DefaultRemoteSupplier(url, desiredCapabilities, requiredCapabilities); Supplier<WebDriver> supplier = new ExternalServerDriverSupplier(url, delegate.or(defaultSupplier)); return Optional.of(supplier); } return delegate; }
From source file:org.gradle.plugins.ide.internal.IdePlugin.java
/** * Returns the path to the correct Gradle distribution to use. The wrapper of the generating project will be used only if the execution context of the currently running Gradle is in the Gradle home (typical of a wrapper execution context). If this isn't the case, we try to use the current Gradle home, if available, as the distribution. Finally, if nothing matches, we default to the system-wide Gradle distribution. * * @param project the Gradle project generating the IDE files * @return path to Gradle distribution to use within the generated IDE files *//*from www . j a v a 2 s . c o m*/ public static String toGradleCommand(Project project) { Gradle gradle = project.getGradle(); Optional<String> gradleWrapperPath = Optional.absent(); Project rootProject = project.getRootProject(); String gradlewExtension = OperatingSystem.current().isWindows() ? ".bat" : ""; File gradlewFile = rootProject.file("gradlew" + gradlewExtension); if (gradlewFile.exists()) { gradleWrapperPath = Optional.of(gradlewFile.getAbsolutePath()); } if (gradle.getGradleHomeDir() != null) { if (gradleWrapperPath.isPresent() && gradle.getGradleHomeDir().getAbsolutePath() .startsWith(gradle.getGradleUserHomeDir().getAbsolutePath())) { return gradleWrapperPath.get(); } return gradle.getGradleHomeDir().getAbsolutePath() + "/bin/gradle"; } return gradleWrapperPath.or("gradle"); }