List of usage examples for com.google.common.base Optional orNull
@Nullable public abstract T orNull();
From source file:org.locationtech.geogig.web.api.commands.FeatureDiffWeb.java
/** * Runs the command and builds the appropriate response * //from w w w.ja va 2s .c o m * @param context - the context to use for this command * * @throws CommandSpecException */ @Override public void run(CommandContext context) { if (path == null || path.trim().isEmpty()) { throw new CommandSpecException("No path for feature name specifed"); } final Context geogig = this.getCommandLocator(context); ObjectId newId = geogig.command(ResolveTreeish.class).setTreeish(newTreeish).call().get(); ObjectId oldId = geogig.command(ResolveTreeish.class).setTreeish(oldTreeish).call().get(); RevFeature newFeature = null; RevFeatureType newFeatureType = null; RevFeature oldFeature = null; RevFeatureType oldFeatureType = null; final Map<PropertyDescriptor, AttributeDiff> diffs; Optional<NodeRef> ref = parseID(newId, geogig); Optional<RevObject> object; // need these to determine if the feature was added or removed so I can build the diffs // myself until the FeatureDiff supports null values boolean removed = false; boolean added = false; if (ref.isPresent()) { object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call(); if (object.isPresent() && object.get() instanceof RevFeatureType) { newFeatureType = (RevFeatureType) object.get(); } else { throw new CommandSpecException("Couldn't resolve newCommit's featureType"); } object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call(); if (object.isPresent() && object.get() instanceof RevFeature) { newFeature = (RevFeature) object.get(); } else { throw new CommandSpecException("Couldn't resolve newCommit's feature"); } } else { removed = true; } if (!oldId.equals(ObjectId.NULL)) { ref = parseID(oldId, geogig); if (ref.isPresent()) { object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call(); if (object.isPresent() && object.get() instanceof RevFeatureType) { oldFeatureType = (RevFeatureType) object.get(); } else { throw new CommandSpecException("Couldn't resolve oldCommit's featureType"); } object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call(); if (object.isPresent() && object.get() instanceof RevFeature) { oldFeature = (RevFeature) object.get(); } else { throw new CommandSpecException("Couldn't resolve oldCommit's feature"); } } else { added = true; } } else { added = true; } if (removed) { Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>(); ImmutableList<PropertyDescriptor> attributes = oldFeatureType.sortedDescriptors(); ImmutableList<Optional<Object>> values = oldFeature.getValues(); for (int index = 0; index < attributes.size(); index++) { Optional<Object> value = values.get(index); if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) { Optional<Geometry> temp = Optional.absent(); if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(Optional.fromNullable((Geometry) value.orNull()), temp)); } } else { if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(value, Optional.absent())); } } } diffs = tempDiffs; } else if (added) { Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>(); ImmutableList<PropertyDescriptor> attributes = newFeatureType.sortedDescriptors(); ImmutableList<Optional<Object>> values = newFeature.getValues(); for (int index = 0; index < attributes.size(); index++) { Optional<Object> value = values.get(index); if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) { Optional<Geometry> temp = Optional.absent(); if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(temp, Optional.fromNullable((Geometry) value.orNull()))); } } else { if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(Optional.absent(), value)); } } } diffs = tempDiffs; } else { FeatureDiff diff = new FeatureDiff(path, newFeature, oldFeature, newFeatureType, oldFeatureType, all); diffs = diff.getDiffs(); } context.setResponseContent(new CommandResponse() { @Override public void write(ResponseWriter out) throws Exception { out.start(); out.writeFeatureDiffResponse(diffs); out.finish(); } }); }
From source file:org.geogig.osm.cli.commands.OSMHistoryImport.java
/** * @param primitive/*w ww . j a va 2s . co m*/ * @param thisChangePointCache * @return */ private Geometry parseGeometry(Context context, Primitive primitive, Map<Long, Coordinate> thisChangePointCache) { if (primitive instanceof Relation) { return null; } if (primitive instanceof Node) { Optional<Point> location = ((Node) primitive).getLocation(); return location.orNull(); } final Way way = (Way) primitive; final ImmutableList<Long> nodes = way.getNodes(); List<Coordinate> coordinates = Lists.newArrayList(nodes.size()); FindTreeChild findTreeChild = context.command(FindTreeChild.class); Optional<ObjectId> nodesTreeId = context.command(ResolveTreeish.class) .setTreeish(Ref.STAGE_HEAD + ":" + NODE_TYPE_NAME).call(); if (nodesTreeId.isPresent()) { RevTree headTree = context.objectDatabase().getTree(nodesTreeId.get()); findTreeChild.setParent(headTree); } int findTreeChildCalls = 0; Stopwatch findTreeChildSW = Stopwatch.createUnstarted(); ObjectStore objectDatabase = context.objectDatabase(); for (Long nodeId : nodes) { Coordinate coord = thisChangePointCache.get(nodeId); if (coord == null) { findTreeChildCalls++; String fid = String.valueOf(nodeId); findTreeChildSW.start(); Optional<NodeRef> nodeRef = findTreeChild.setChildPath(fid).call(); findTreeChildSW.stop(); Optional<org.locationtech.geogig.model.Node> ref = Optional.absent(); if (nodeRef.isPresent()) { ref = Optional.of(nodeRef.get().getNode()); } if (ref.isPresent()) { final int locationAttIndex = 6; ObjectId objectId = ref.get().getObjectId(); RevFeature revFeature = objectDatabase.getFeature(objectId); Point p = (Point) revFeature.get(locationAttIndex, GEOMF).orNull(); if (p != null) { coord = p.getCoordinate(); thisChangePointCache.put(Long.valueOf(nodeId), coord); } } } if (coord != null) { coordinates.add(coord); } } if (findTreeChildCalls > 0) { // System.err.printf("%,d findTreeChild calls (%s)\n", findTreeChildCalls, // findTreeChildSW); } if (coordinates.size() < 2) { return null; } return GEOMF.createLineString(coordinates.toArray(new Coordinate[coordinates.size()])); }
From source file:org.geogit.api.plumbing.diff.FeatureDiff.java
/** * //from w w w .ja v a2 s. c o m * @param path the full path to the feature, including its name * @param newRevFeature the new version of the feature * @param oldRevFeature the old version of the feature * @param newRevFeatureType the new version of the feature type * @param oldRevFeatureType the old version of the feature type * @param all - true if all attributes should be added regardless of change */ public FeatureDiff(String path, RevFeature newRevFeature, RevFeature oldRevFeature, RevFeatureType newRevFeatureType, RevFeatureType oldRevFeatureType, boolean all) { this.path = path; this.newFeatureType = newRevFeatureType; this.oldFeatureType = oldRevFeatureType; diffs = new HashMap<PropertyDescriptor, AttributeDiff>(); ImmutableList<PropertyDescriptor> oldAttributes = oldRevFeatureType.sortedDescriptors(); ImmutableList<PropertyDescriptor> newAttributes = newRevFeatureType.sortedDescriptors(); ImmutableList<Optional<Object>> oldValues = oldRevFeature.getValues(); ImmutableList<Optional<Object>> newValues = newRevFeature.getValues(); BitSet updatedAttributes = new BitSet(newValues.size()); for (int i = 0; i < oldAttributes.size(); i++) { Optional<Object> oldValue = oldValues.get(i); int idx = newAttributes.indexOf(oldAttributes.get(i)); if (idx != -1) { Optional<Object> newValue = newValues.get(idx); if (!oldValue.equals(newValue) || all) { if (Geometry.class.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) { diffs.put(oldAttributes.get(i), new GeometryAttributeDiff(Optional.fromNullable((Geometry) oldValue.orNull()), Optional.fromNullable((Geometry) newValue.orNull()))); } else { diffs.put(oldAttributes.get(i), new GenericAttributeDiffImpl(oldValue, newValue)); } } updatedAttributes.set(idx); } else { if (Geometry.class.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) { diffs.put(oldAttributes.get(i), new GeometryAttributeDiff(Optional.fromNullable((Geometry) oldValue.orNull()), Optional.fromNullable((Geometry) null))); } else { diffs.put(oldAttributes.get(i), new GenericAttributeDiffImpl(oldValue, null)); } } } updatedAttributes.flip(0, newValues.size()); for (int i = updatedAttributes.nextSetBit(0); i >= 0; i = updatedAttributes.nextSetBit(i + 1)) { if (Geometry.class.isAssignableFrom(newAttributes.get(i).getType().getBinding())) { diffs.put(oldAttributes.get(i), new GeometryAttributeDiff(Optional.fromNullable((Geometry) null), Optional.fromNullable((Geometry) newValues.get(i).orNull()))); } else { diffs.put(newAttributes.get(i), new GenericAttributeDiffImpl(null, newValues.get(i))); } } }
From source file:org.locationtech.geogig.api.plumbing.diff.FeatureDiff.java
/** * /*w w w .j ava 2s. com*/ * @param path the full path to the feature, including its name * @param newRevFeature the new version of the feature * @param oldRevFeature the old version of the feature * @param newRevFeatureType the new version of the feature type * @param oldRevFeatureType the old version of the feature type * @param all - true if all attributes should be added regardless of change */ public FeatureDiff(String path, RevFeature newRevFeature, RevFeature oldRevFeature, RevFeatureType newRevFeatureType, RevFeatureType oldRevFeatureType, boolean all) { this.path = path; this.newFeatureType = newRevFeatureType; this.oldFeatureType = oldRevFeatureType; diffs = new HashMap<PropertyDescriptor, AttributeDiff>(); ImmutableList<PropertyDescriptor> oldAttributes = oldRevFeatureType.sortedDescriptors(); ImmutableList<PropertyDescriptor> newAttributes = newRevFeatureType.sortedDescriptors(); ImmutableList<Optional<Object>> oldValues = oldRevFeature.getValues(); ImmutableList<Optional<Object>> newValues = newRevFeature.getValues(); BitSet updatedAttributes = new BitSet(newValues.size()); for (int i = 0; i < oldAttributes.size(); i++) { Optional<Object> oldValue = oldValues.get(i); int idx = newAttributes.indexOf(oldAttributes.get(i)); if (idx != -1) { Optional<Object> newValue = newValues.get(idx); if (!oldValue.equals(newValue) || all) { if (Geometry.class.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) { diffs.put(oldAttributes.get(i), new GeometryAttributeDiff(Optional.fromNullable((Geometry) oldValue.orNull()), Optional.fromNullable((Geometry) newValue.orNull()))); } else { diffs.put(oldAttributes.get(i), new GenericAttributeDiffImpl(oldValue, newValue)); } } updatedAttributes.set(idx); } else { if (Geometry.class.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) { diffs.put(oldAttributes.get(i), new GeometryAttributeDiff(Optional.fromNullable((Geometry) oldValue.orNull()), Optional.fromNullable((Geometry) null))); } else { diffs.put(oldAttributes.get(i), new GenericAttributeDiffImpl(oldValue, null)); } } } updatedAttributes.flip(0, newValues.size()); for (int i = updatedAttributes.nextSetBit(0); i >= 0; i = updatedAttributes.nextSetBit(i + 1)) { if (Geometry.class.isAssignableFrom(newAttributes.get(i).getType().getBinding())) { diffs.put(newAttributes.get(i), new GeometryAttributeDiff(Optional.fromNullable((Geometry) null), Optional.fromNullable((Geometry) newValues.get(i).orNull()))); } else { diffs.put(newAttributes.get(i), new GenericAttributeDiffImpl(null, newValues.get(i))); } } }
From source file:org.opencms.xml.containerpage.CmsFormatterConfiguration.java
/** * Selects the best matching schema formatter for the provided type and width from this configuration.<p> * * @param containerTypes the container types (comma separated) * @param containerWidth the container width * * @return the matching formatter, or <code>null</code> if none was found *///w ww . java2s .co m public I_CmsFormatterBean getDefaultSchemaFormatter(final String containerTypes, final int containerWidth) { Optional<I_CmsFormatterBean> result = Iterables.tryFind(m_allFormatters, Predicates .and(new IsSchemaFormatter(), new MatchesTypeOrWidth(containerTypes, containerWidth, false))); return result.orNull(); }
From source file:io.crate.analyze.CreateAnalyzerStatementAnalyzer.java
@Override public CreateAnalyzerAnalyzedStatement visitCharFilters(CharFilters charFilters, Context context) { for (NamedProperties charFilterNode : charFilters.charFilters()) { String name = charFilterNode.ident(); Optional<GenericProperties> properties = charFilterNode.properties(); // use a builtin char-filter without parameters if (!properties.isPresent()) { // validate if (!context.statement.analyzerService().hasBuiltInCharFilter(name)) { throw new IllegalArgumentException( String.format(Locale.ENGLISH, "Non-existing built-in char-filter '%s'", name)); }/* w w w .ja v a 2 s . com*/ validateCharFilterProperties(name, properties.orNull()); // build context.statement.addCharFilter(name, Settings.EMPTY); } else { String type = extractType(properties.get(), context.analysis.parameterContext()); if (!context.statement.analyzerService().hasBuiltInCharFilter(type)) { // only builtin char-filters can be extended, for now throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Non-existing built-in char-filter" + " type '%s'", type)); } validateCharFilterProperties(type, properties.get()); // build // transform name as char-filter is not publicly available name = String.format(Locale.ENGLISH, "%s_%s", context.statement.ident(), name); Settings.Builder builder = Settings.builder(); for (Map.Entry<String, Expression> charFilterProperty : properties.get().properties().entrySet()) { GenericPropertiesConverter.genericPropertyToSetting(builder, getSettingsKey("index.analysis.char_filter.%s.%s", name, charFilterProperty.getKey()), charFilterProperty.getValue(), context.analysis.parameterContext()); } context.statement.addCharFilter(name, builder.build()); } } return null; }
From source file:org.geogit.web.api.commands.FeatureDiffWeb.java
/** * Runs the command and builds the appropriate response * //www . ja v a2s . c o m * @param context - the context to use for this command * * @throws CommandSpecException */ @Override public void run(CommandContext context) { if (path == null || path.trim().isEmpty()) { throw new CommandSpecException("No path for feature name specifed"); } ObjectId newId = null; final CommandLocator geogit = this.getCommandLocator(context); if (newCommitId.equals(ObjectId.NULL.toString()) || newCommitId.trim().isEmpty()) { Optional<ObjectId> oid = geogit.command(ResolveTreeish.class).setTreeish(Ref.HEAD).call(); if (oid.isPresent()) { newId = oid.get(); } else { throw new CommandSpecException("Something went wrong, couldn't resolve HEAD"); } } else { newId = ObjectId.valueOf(newCommitId); } ObjectId oldId = ObjectId.valueOf(oldCommitId); RevFeature newFeature = null; RevFeatureType newFeatureType = null; RevFeature oldFeature = null; RevFeatureType oldFeatureType = null; final Map<PropertyDescriptor, AttributeDiff> diffs; Optional<NodeRef> ref = parseID(newId, geogit); Optional<RevObject> object; // need these to determine if the feature was added or removed so I can build the diffs // myself until the FeatureDiff supports null values boolean removed = false; boolean added = false; if (ref.isPresent()) { object = geogit.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call(); if (object.isPresent() && object.get() instanceof RevFeatureType) { newFeatureType = (RevFeatureType) object.get(); } else { throw new CommandSpecException("Couldn't resolve newCommit's featureType"); } object = geogit.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call(); if (object.isPresent() && object.get() instanceof RevFeature) { newFeature = (RevFeature) object.get(); } else { throw new CommandSpecException("Couldn't resolve newCommit's feature"); } } else { removed = true; } if (!oldId.equals(ObjectId.NULL)) { ref = parseID(oldId, geogit); if (ref.isPresent()) { object = geogit.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call(); if (object.isPresent() && object.get() instanceof RevFeatureType) { oldFeatureType = (RevFeatureType) object.get(); } else { throw new CommandSpecException("Couldn't resolve oldCommit's featureType"); } object = geogit.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call(); if (object.isPresent() && object.get() instanceof RevFeature) { oldFeature = (RevFeature) object.get(); } else { throw new CommandSpecException("Couldn't resolve oldCommit's feature"); } } else { added = true; } } else { added = true; } if (removed) { Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>(); ImmutableList<PropertyDescriptor> attributes = oldFeatureType.sortedDescriptors(); ImmutableList<Optional<Object>> values = oldFeature.getValues(); for (int index = 0; index < attributes.size(); index++) { Optional<Object> value = values.get(index); if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) { Optional<Geometry> temp = Optional.absent(); if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(Optional.fromNullable((Geometry) value.orNull()), temp)); } } else { if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(value, Optional.absent())); } } } diffs = tempDiffs; } else if (added) { Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>(); ImmutableList<PropertyDescriptor> attributes = newFeatureType.sortedDescriptors(); ImmutableList<Optional<Object>> values = newFeature.getValues(); for (int index = 0; index < attributes.size(); index++) { Optional<Object> value = values.get(index); if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) { Optional<Geometry> temp = Optional.absent(); if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(temp, Optional.fromNullable((Geometry) value.orNull()))); } } else { if (value.isPresent() || all) { tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(Optional.absent(), value)); } } } diffs = tempDiffs; } else { FeatureDiff diff = new FeatureDiff(path, newFeature, oldFeature, newFeatureType, oldFeatureType, all); diffs = diff.getDiffs(); } context.setResponseContent(new CommandResponse() { @Override public void write(ResponseWriter out) throws Exception { out.start(); out.writeFeatureDiffResponse(diffs); out.finish(); } }); }
From source file:com.google.devtools.build.android.AndroidResourceClassWriter.java
public void writePublicValue(ResourceType type, String name, Optional<Integer> value) { SortedMap<String, Optional<Integer>> publicMappings = publicIds.get(type); if (publicMappings == null) { publicMappings = new TreeMap<>(); publicIds.put(type, publicMappings); }//from www. j a v a2 s. com Optional<Integer> oldValue = publicMappings.put(name, value); // AAPT should issue an error, but do a bit of sanity checking here just in case. if (oldValue != null && !oldValue.equals(value)) { // Enforce a consistent ordering on the warning message. Integer lower = oldValue.orNull(); Integer higher = value.orNull(); if (Ordering.natural().compare(oldValue.orNull(), value.orNull()) > 0) { lower = higher; higher = oldValue.orNull(); } logger.warning(String.format("resource %s/%s has conflicting public identifiers (0x%x vs 0x%x)", type, name, lower, higher)); } }
From source file:com.eucalyptus.vm.MetadataRequest.java
public MetadataRequest(String requestIp, String requestUrl, Optional<String> vmId) { try {// w w w .j a va 2 s . c om this.requestIp = requestIp; requestUrl = requestUrl.replaceAll("[/]+", "/"); String[] path = requestUrl.split("/", 2); if (path.length > 0) { this.metadataName = path[0]; if (path.length > 1) { this.localPath = path[1].replaceFirst("^[/]*", ""); } else { this.localPath = ""; } } else { this.metadataName = ""; this.localPath = ""; } this.vmId = vmId.orNull(); } finally { LOG.debug((this.vmId != null ? "Instance" : "External") + " Metadata: requestIp=" + this.requestIp + " metadataName=" + this.metadataName + " metadataPath=" + this.localPath + " requestUrl=" + requestUrl); } }
From source file:org.geogit.cli.porcelain.Blame.java
@Override public void runInternal(GeogitCLI cli) throws IOException { checkParameter(paths.size() < 2, "Only one path allowed"); checkParameter(!paths.isEmpty(), "A path must be specified"); ConsoleReader console = cli.getConsole(); GeoGIT geogit = cli.getGeogit();/*from w w w . j av a 2 s . com*/ String path = paths.get(0); BlameReport report = geogit.command(BlameOp.class).setPath(path).call(); Map<String, ValueAndCommit> changes = report.getChanges(); Iterator<String> iter = changes.keySet().iterator(); while (iter.hasNext()) { String attrib = iter.next(); ValueAndCommit valueAndCommit = changes.get(attrib); RevCommit commit = valueAndCommit.commit; Optional<?> value = valueAndCommit.value; if (porcelain) { StringBuilder sb = new StringBuilder(); sb.append(attrib).append(' '); sb.append(commit.getId().toString()).append(' '); sb.append(commit.getAuthor().getName().or("")).append(' '); sb.append(commit.getAuthor().getEmail().or("")).append(' '); sb.append(Long.toString(commit.getAuthor().getTimestamp())).append(' '); sb.append(Integer.toString(commit.getAuthor().getTimeZoneOffset())); if (!noValues) { sb.append(" ").append(TextValueSerializer.asString(Optional.of((Object) value.orNull()))); } console.println(sb.toString()); } else { Ansi ansi = newAnsi(console.getTerminal()); ansi.fg(GREEN).a(attrib + ": ").reset(); if (!noValues) { String s = value.isPresent() ? value.get().toString() : "NULL"; ansi.fg(YELLOW).a(s).a(" ").reset(); } ansi.a(commit.getId().toString().substring(0, 7)).a(" "); ansi.a(commit.getAuthor().getName().or("")).a(" "); ansi.a(commit.getAuthor().getEmail().or("")).a(" "); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String date = formatter.format( new Date(commit.getAuthor().getTimestamp() + commit.getAuthor().getTimeZoneOffset())); ansi.a(date); console.println(ansi.toString()); } } }