Example usage for java.util Optional map

List of usage examples for java.util Optional map

Introduction

In this page you can find the example usage for java.util Optional map.

Prototype

public <U> Optional<U> map(Function<? super T, ? extends U> mapper) 

Source Link

Document

If a value is present, returns an Optional describing (as if by #ofNullable ) the result of applying the given mapping function to the value, otherwise returns an empty Optional .

Usage

From source file:io.pravega.controller.store.stream.tables.TableHelper.java

/**
 * Method to compute segments created and deleted in latest scale event.
 *
 * @param historyTable history table/*w ww  .  j  a v  a 2  s .c  om*/
 * @return pair of segments sealed and segments created in last scale event.
 */
public static Pair<List<Integer>, List<Integer>> getLatestScaleData(final byte[] historyTable) {
    final Optional<HistoryRecord> current = HistoryRecord.readLatestRecord(historyTable, false);
    ImmutablePair<List<Integer>, List<Integer>> result;
    if (current.isPresent()) {
        final Optional<HistoryRecord> previous = HistoryRecord.fetchPrevious(current.get(), historyTable);
        result = previous
                .map(historyRecord -> new ImmutablePair<>(
                        diff(historyRecord.getSegments(), current.get().getSegments()),
                        diff(current.get().getSegments(), historyRecord.getSegments())))
                .orElseGet(() -> new ImmutablePair<>(Collections.emptyList(), current.get().getSegments()));
    } else {
        result = new ImmutablePair<>(Collections.emptyList(), Collections.emptyList());
    }
    return result;
}

From source file:com.ikanow.aleph2.graph.titan.services.TitanGraphService.java

/** Utility method for building the graph indices
 * @param bucket//w  w  w .j  a  v a 2  s. com
 * @return
 */
public static CompletableFuture<Collection<BasicMessageBean>> createIndices(DataBucketBean bucket,
        TitanGraph titan, boolean use_es_for_dedup_indices) {

    final TitanManagement mgmt = titan.openManagement();

    // First off, let's ensure that a2_p is indexed: (note these apply to both vertixes and edges)
    try {
        final PropertyKey bucket_index = mgmt.makePropertyKey(GraphAnnotationBean.a2_p).dataType(String.class)
                .cardinality(Cardinality.SET).make();
        try {
            mgmt.buildIndex(GLOBAL_PATH_INDEX_GV, Vertex.class)
                    .addKey(bucket_index, Mapping.STRING.asParameter()).buildMixedIndex(SEARCH_INDEX_NAME);
        } catch (IllegalArgumentException e) {
            //DEBUG
            //_logger.error(ErrorUtils.getLongForm("{0}", e));
            //e.printStackTrace();
        } // (already indexed, this is fine/expected)            
        try {
            mgmt.buildIndex(GLOBAL_PATH_INDEX_GE, Edge.class).addKey(bucket_index, Mapping.STRING.asParameter())
                    .buildMixedIndex(SEARCH_INDEX_NAME);
        } catch (IllegalArgumentException e) {
            //DEBUG
            //_logger.error(ErrorUtils.getLongForm("{0}", e));
            //e.printStackTrace();
        } // (already indexed, this is fine/expected)
    } catch (IllegalArgumentException e) {
        //DEBUG
        //e.printStackTrace();
    } // (already indexed, this is fine/expected)

    // Created/modified
    try {
        mgmt.buildIndex(GLOBAL_CREATED_GV, Vertex.class)
                .addKey(mgmt.makePropertyKey(GraphAnnotationBean.a2_tc).dataType(Long.class).make())
                .buildMixedIndex(SEARCH_INDEX_NAME);
    } catch (IllegalArgumentException e) {
        //DEBUG
        //_logger.error(ErrorUtils.getLongForm("{0}", e));
        //e.printStackTrace();

    } // (already indexed, this is fine/expected)            
    try {
        mgmt.buildIndex(GLOBAL_MODIFIED_GV, Vertex.class)
                .addKey(mgmt.makePropertyKey(GraphAnnotationBean.a2_tm).dataType(Long.class).make())
                .buildMixedIndex(SEARCH_INDEX_NAME);
    } catch (IllegalArgumentException e) {
        //DEBUG
        //_logger.error(ErrorUtils.getLongForm("{0}", e));
        //e.printStackTrace();

    } // (already indexed, this is fine/expected)

    // Then check that the global default index is set
    Optional<List<String>> maybe_dedup_fields = Optionals
            .of(() -> bucket.data_schema().graph_schema().deduplication_fields());
    final Collection<BasicMessageBean> ret_val = maybe_dedup_fields.map(dedup_fields -> {
        //TODO (ALEPH-15): manage the index pointed to by the bucket's signature               
        return Arrays.asList(ErrorUtils.buildErrorMessage(TitanGraph.class.getSimpleName(), "onPublishOrUpdate",
                ErrorUtils.NOT_YET_IMPLEMENTED, "custom:deduplication_fields"));
    }).orElseGet(() -> {
        try {
            //TODO (ALEPH-15): There's a slightly tricky decision here...
            // using ES makes dedup actions "very" no longer transactional because of the index refresh
            // (in theory using Cassandra or HBase makes things transactional, though I haven't tested that)
            // Conversely not using ES puts more of the load on the smaller Cassandra/HBase clusters                  
            // Need to make configurable, but default to using the transactional layer
            // Of course, if I move to eg unipop later on then I'll have to fix this
            // It's not really any different to deduplication _except_ it can happen across buckets (unlike dedup) so it's much harder to stop
            // A few possibilities:
            // 1) Have a job that runs on new-ish data (ofc that might not be easy to detect) that merges things (ofc very unclear how to do that)
            // 2) Centralize all insert actions
            // Ah here's some more interest - looks like Hbase and Cassandra's eventual consistency can cause duplicates:
            // http://s3.thinkaurelius.com/docs/titan/1.0.0/eventual-consistency.html ... tldr: basically need to have regular "clean up jobs" and live with transient issues
            // (or use a consistent data store - would also require a decent amount of code here because our dedup strategy is not perfect)

            final Function<String, PropertyKey> getOrCreateProperty = field -> Optional
                    .ofNullable(mgmt.getPropertyKey(field))
                    .orElseGet(() -> mgmt.makePropertyKey(field).dataType(String.class).make());

            final PropertyKey name_index = getOrCreateProperty.apply(GraphAnnotationBean.name);
            final PropertyKey type_index = getOrCreateProperty.apply(GraphAnnotationBean.type);

            if (use_es_for_dedup_indices) {
                mgmt.buildIndex(GLOBAL_DEFAULT_INDEX_GV, Vertex.class)
                        .addKey(name_index, Mapping.TEXTSTRING.asParameter())
                        .addKey(type_index, Mapping.STRING.asParameter()).buildMixedIndex(SEARCH_INDEX_NAME);
            } else { // use the storage backend which should have better consistency properties
                mgmt.buildIndex(GLOBAL_DEFAULT_INDEX_GV, Vertex.class).addKey(name_index).addKey(type_index)
                        //TODO (ALEPH-15: make this unique()? and then have multiple contexts, either via property or lots of graphs?
                        .buildCompositeIndex();

                //(in this case, also index "name" as an ES field to make it easier to search over)
                mgmt.buildIndex(GLOBAL_DEFAULT_INDEX_GV + "_TEXT", Vertex.class)
                        .addKey(name_index, Mapping.TEXT.asParameter()).buildMixedIndex(SEARCH_INDEX_NAME);
            }
        } catch (IllegalArgumentException e) {
            //DEBUG
            //_logger.error(ErrorUtils.getLongForm("{0}", e));
            //e.printStackTrace();

        } // (already indexed, this is fine/expected)
        return Collections.emptyList();
    });

    //TODO (ALEPH-15): allow other indexes (etc) via the schema technology override      

    // Complete management transaction
    mgmt.commit();

    //TODO (ALEPH-15): want to complete the future only once the indexing steps are done?
    return CompletableFuture.completedFuture(ret_val);
}

From source file:nu.yona.server.subscriptions.rest.UserController.java

private static Link getUserLink(String rel, UUID userId, Optional<UUID> requestingUserId,
        Optional<UUID> requestingDeviceId) {
    String requestingUserIdStr = requestingUserId.map(UUID::toString).orElse(null);
    String requestingDeviceIdStr = requestingDeviceId.map(UUID::toString).orElse(null);
    return linkTo(methodOn(UserController.class).getUser(Optional.empty(), null, requestingUserIdStr,
            requestingDeviceIdStr, null, userId)).withRel(rel).expand(OMITTED_PARAMS);
}

From source file:nu.yona.server.subscriptions.rest.UserController.java

static ControllerLinkBuilder getUpdateUserLinkBuilder(UUID userId, Optional<UUID> requestingDeviceId) {
    return linkTo(methodOn(UserController.class).updateUser(Optional.empty(), null, userId,
            requestingDeviceId.map(UUID::toString).orElse(null), null, null));
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchHiveUtils.java

/** Handles the prefix and suffix of the full hive schema
 *  https://www.elastic.co/guide/en/elasticsearch/hadoop/current/hive.html
 * @param table_name - if empty then "main_table"
 * @param bucket/* w  w w  .ja v  a  2  s .  c o m*/
 * @param schema
 * @param partial_hive_schema
 * @return
 */
public static Validation<String, String> generateFullHiveSchema(final Optional<String> table_name,
        final DataBucketBean bucket, final DataSchemaBean.DataWarehouseSchemaBean schema,
        Optional<Client> maybe_client, ElasticsearchIndexServiceConfigBean config) {
    // (ignore views for the moment)

    final String prefix = ErrorUtils.get("CREATE EXTERNAL TABLE {0} ", getTableName(bucket, schema));

    final DataSchemaBean.DataWarehouseSchemaBean.Table table = table_name.flatMap(t -> Optionals
            .ofNullable(schema.views()).stream().filter(v -> t.equals(v.database_name())).findFirst())
            .orElse(schema.main_table());

    final JsonNode user_schema = _mapper.convertValue(table.table_format(), JsonNode.class);

    final Validation<String, String> partial_table = generatePartialHiveSchema(prefix, user_schema, true);

    // (for the main table, just going to be the full alias - for views will need to be cleverer)
    final String index = Optionals
            .of(() -> bucket.data_schema().search_index_schema().technology_override_schema()
                    .get(SearchIndexSchemaDefaultBean.index_name_override_).toString())
            .orElseGet(() -> "r__" + BucketUtils.getUniqueSignature(bucket.full_name(), Optional.empty()));

    final Optional<ElasticsearchHiveOverrideBean> maybe_override = Optionals
            .of(() -> schema.technology_override_schema())
            .map(m -> BeanTemplateUtils.from(m, ElasticsearchHiveOverrideBean.class).get());

    // OK all this horrible code is intended to sort out the list of types to apply in the hive query
    final Optional<ElasticsearchHiveOverrideBean.TableOverride> table_override = maybe_override
            .map(cfg -> cfg.table_overrides().get(table_name.orElse(MAIN_TABLE_NAME)));
    final Optional<Set<String>> user_type_overrides = table_override.map(t -> t.types())
            .filter(l -> !l.isEmpty()).map(l -> new TreeSet<String>(l));
    final Set<String> mutable_type_set = user_type_overrides.orElseGet(() -> {
        return new TreeSet<String>(
                maybe_client.map(client -> ElasticsearchIndexUtils.getTypesForIndex(client, index).values())
                        .orElse(Collections.emptySet()));
    });

    final ElasticsearchIndexServiceConfigBean schema_config = ElasticsearchIndexConfigUtils
            .buildConfigBeanFromSchema(bucket, config, _mapper);
    final CollidePolicy collide_policy = Optionals
            .of(() -> schema_config.search_technology_override().collide_policy())
            .orElse(CollidePolicy.new_type);

    Optionals.of(() -> schema_config.search_technology_override().type_name_or_prefix()).map(Optional::of)
            .orElseGet(() -> Optional.of((collide_policy == CollidePolicy.new_type)
                    ? ElasticsearchContext.TypeContext.ReadWriteTypeContext.AutoRwTypeContext.DEFAULT_PREFIX
                    : ElasticsearchIndexServiceConfigBean.DEFAULT_FIXED_TYPE_NAME))
            .ifPresent(type_or_prefix -> {
                if (!user_type_overrides.isPresent()) { // leave alone if manually specified
                    if (collide_policy == CollidePolicy.new_type) { // add a few types
                        //TODO (ALEPH-17): need to make this get auto populated as new types are added, see the ALEPH-17 comment in ElasticsearchIndexService
                        if (mutable_type_set.size() < 10) {
                            IntStream.rangeClosed(1, 10).boxed().map(i -> type_or_prefix + i.toString())
                                    .forEach(type -> mutable_type_set.add(type));
                        }
                    } else { // OK in this case just make sure the default type is represented
                        mutable_type_set.add(type_or_prefix);
                    }
                }
            });

    final String suffix = Optional.of(" STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler' ")
            .map(s -> s + ErrorUtils.get(
                    "TBLPROPERTIES(''es.index.auto.create'' = ''false'', ''es.resource'' = ''{0}/{1}''", index,
                    mutable_type_set.stream().collect(Collectors.joining(","))))
            .map(s -> table_override.map(t -> t.name_mappings()).filter(m -> !m.isEmpty())
                    .map(m -> s + ", 'es.mapping.names' = '"
                            + m.entrySet().stream().map(kv -> kv.getKey() + ":" + kv.getValue())
                                    .collect(Collectors.joining(","))
                            + "'")
                    .orElse(s))
            .map(s -> table_override
                    .flatMap(t -> Optional.ofNullable(t.url_query()).map(ss -> "?" + ss).map(Optional::of)
                            .orElseGet(() -> Optional.ofNullable(t.json_query())
                                    .map(jq -> _mapper.convertValue(jq, JsonNode.class).toString())))
                    .map(ss -> s + ", 'es.query' = '" + ss + "'").orElse(s))
            .map(s -> s + ") ").get();

    return partial_table.map(s -> s + suffix);
}

From source file:com.cloudbees.jenkins.support.SupportPlugin.java

public static void writeBundle(OutputStream outputStream, final List<Component> components) throws IOException {
    StringBuilder manifest = new StringBuilder();
    StringWriter errors = new StringWriter();
    PrintWriter errorWriter = new PrintWriter(errors);
    appendManifestHeader(manifest);//w ww . j  av a 2s  .co  m
    List<Content> contents = appendManifestContents(manifest, errorWriter, components);
    contents.add(new StringContent("manifest.md", manifest.toString()));
    try {
        try (BulkChange change = new BulkChange(ContentMappings.get());
                ZipArchiveOutputStream binaryOut = new ZipArchiveOutputStream(
                        new BufferedOutputStream(outputStream, 16384))) {
            Optional<ContentFilter> maybeFilter = getContentFilter();
            Optional<FilteredOutputStream> maybeFilteredOut = maybeFilter
                    .map(filter -> new FilteredOutputStream(binaryOut, filter));
            OutputStream textOut = maybeFilteredOut.map(OutputStream.class::cast).orElse(binaryOut);
            OutputStreamSelector selector = new OutputStreamSelector(() -> binaryOut, () -> textOut);
            IgnoreCloseOutputStream unfilteredOut = new IgnoreCloseOutputStream(binaryOut);
            IgnoreCloseOutputStream filteredOut = new IgnoreCloseOutputStream(selector);
            for (Content content : contents) {
                if (content == null) {
                    continue;
                }
                final String name = maybeFilter.map(filter -> filter.filter(content.getName()))
                        .orElseGet(content::getName);
                final ZipArchiveEntry entry = new ZipArchiveEntry(name);
                entry.setTime(content.getTime());
                try {
                    binaryOut.putArchiveEntry(entry);
                    binaryOut.flush();
                    OutputStream out = content.shouldBeFiltered() ? filteredOut : unfilteredOut;
                    if (content instanceof PrefilteredContent && maybeFilter.isPresent()) {
                        ((PrefilteredContent) content).writeTo(out, maybeFilter.get());
                    } else {
                        content.writeTo(out);
                    }
                    out.flush();
                } catch (Throwable e) {
                    String msg = "Could not attach ''" + name + "'' to support bundle";
                    logger.log(Level.WARNING, msg, e);
                    errorWriter.println(msg);
                    errorWriter
                            .println("-----------------------------------------------------------------------");
                    errorWriter.println();
                    SupportLogFormatter.printStackTrace(e, errorWriter);
                    errorWriter.println();
                } finally {
                    maybeFilteredOut.ifPresent(FilteredOutputStream::reset);
                    selector.reset();
                    binaryOut.closeArchiveEntry();
                }
            }
            errorWriter.close();
            String errorContent = errors.toString();
            if (StringUtils.isNotBlank(errorContent)) {
                try {
                    binaryOut.putArchiveEntry(new ZipArchiveEntry("manifest/errors.txt"));
                    textOut.write(errorContent.getBytes(StandardCharsets.UTF_8));
                    textOut.flush();
                    binaryOut.closeArchiveEntry();
                } catch (IOException e) {
                    logger.log(Level.WARNING, "Could not write manifest/errors.txt to zip archive", e);
                }
            }
            binaryOut.flush();
            change.commit();
        }
    } finally {
        outputStream.flush();
    }
}

From source file:com.ikanow.aleph2.analytics.services.DeduplicationService.java

/** Logic to perform the custom deduplication with the current and new versions
 * @param maybe_custom_handler// w w w. j a  v  a  2s. c om
 * @param new_record
 * @param old_record
 * @returns list of Json objects to delete
 */
protected static Stream<JsonNode> handleCustomDeduplication(
        Optional<Tuple2<IEnrichmentBatchModule, DeduplicationEnrichmentContext>> maybe_custom_handler,
        final List<Tuple3<Long, IBatchRecord, ObjectNode>> new_records, final Collection<JsonNode> old_records,
        final JsonNode key) {
    return maybe_custom_handler.map(handler_context -> {
        handler_context._2().resetMutableState(old_records, key);

        final Consumer<IEnrichmentBatchModule> handler = new_module -> {
            final Stream<Tuple2<Long, IBatchRecord>> dedup_stream = Stream.concat(
                    new_records.stream().map(t3 -> Tuples._2T(t3._1(), t3._2())),
                    old_records.stream().map(old_record -> Tuples._2T(-1L,
                            (IBatchRecord) (new BatchRecordUtils.InjectedJsonBatchRecord(old_record)))));

            final int batch_size = new_records.size();

            new_module.onObjectBatch(dedup_stream, Optional.of(batch_size).filter(__ -> !old_records.isEmpty()), // (ie leave batch size blank if there's no dedup) 
                    Optional.of(key));

            new_module.onStageComplete(false);
        };

        handler.accept(handler_context._1());

        return handler_context._2().getObjectIdsToDelete();
    }).orElse(Stream.empty());
}

From source file:org.trustedanalytics.cloud.cc.api.CcSpace.java

@JsonIgnore
public UUID getGuid() {
    Optional<CcSpace> space = Optional.of(this);
    return space.map(CcSpace::getMetadata).map(CcMetadata::getGuid).orElse(null);
}

From source file:org.trustedanalytics.cloud.cc.api.CcSpace.java

@JsonIgnore
public String getName() {
    Optional<CcSpace> space = Optional.of(this);
    return space.map(CcSpace::getEntity).map(CcSpaceEntity::getName).orElse(null);
}

From source file:org.trustedanalytics.cloud.cc.api.CcSpace.java

@JsonIgnore
public UUID getOrgGuid() {
    Optional<CcSpace> space = Optional.of(this);
    return space.map(CcSpace::getEntity).map(CcSpaceEntity::getOrgGuid).orElse(null);
}