List of usage examples for java.util Spliterators spliteratorUnknownSize
public static Spliterator.OfDouble spliteratorUnknownSize(PrimitiveIterator.OfDouble iterator, int characteristics)
From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchIndexUtils.java
/** Get a set of field mappings from the "properties" section of a mapping * @param index//ww w .j a va 2 s . c o m * @return */ protected static LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> getProperties( final JsonNode index) { return Optional.ofNullable(index.get("properties")).filter(p -> !p.isNull()).map(p -> { if (!p.isObject()) throw new RuntimeException("properties must be object"); return p; }).map(p -> { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(p.fields(), Spliterator.ORDERED), false) .map(kv -> { if (!kv.getValue().has("type") && !kv.getValue().has("properties")) throw new RuntimeException(SearchIndexErrorUtils .get("field {0} must have a 'type' or 'properties' sub-field", kv.getKey())); return kv; }).collect( Collectors.<Map.Entry<String, JsonNode>, Either<String, Tuple2<String, String>>, JsonNode, LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode>>toMap( kv -> Either.<String, Tuple2<String, String>>left(kv.getKey()), kv -> kv.getValue(), (v1, v2) -> v1, // (should never happen) () -> new LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode>())); }).orElse(new LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode>()); }
From source file:ubicrypt.core.Utils.java
public static <T> Stream<T> toStream(final Iterator<T> iterator) { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); }
From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchIndexUtils.java
/** Get a set of field mappings from the "dynamic_templates" section of a mapping * @param index//from w ww .j ava 2s .c om * @return */ protected static LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> getTemplates( final JsonNode index, final JsonNode default_string_mapping, final Set<Either<String, Tuple2<String, String>>> already_processed) { return Optional.ofNullable(index.get("dynamic_templates")).filter(p -> !p.isNull()).map(p -> { if (!p.isArray()) throw new RuntimeException("dynamic_templates must be object"); return p; }).map(p -> { return StreamSupport .stream(Spliterators.spliteratorUnknownSize(p.elements(), Spliterator.ORDERED), false) .map(pf -> { if (!pf.isObject()) throw new RuntimeException("dynamic_templates[*] must be object"); return pf; }) .flatMap(pp -> StreamSupport .stream(Spliterators.spliteratorUnknownSize(pp.fields(), Spliterator.ORDERED), false)) .filter(kv -> !kv.getKey().equals(STRING_OVERRIDE_NAME) || !already_processed.contains(Either.right(Tuples._2T("*", "string")))) // (don't override a specified string) .map(kv -> !kv.getKey().equals(STRING_OVERRIDE_NAME) ? kv : Maps.immutableEntry(kv.getKey(), default_string_mapping)) //(special case - overwrite with system default) .collect( Collectors.<Map.Entry<String, JsonNode>, Either<String, Tuple2<String, String>>, JsonNode, LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode>>toMap( kv -> Either.right(buildMatchPair(kv.getValue())), kv -> kv.getValue(), (v1, v2) -> v1, // (should never happen) () -> new LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode>())); }).orElse(new LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode>()); }
From source file:enumj.Enumerator.java
/** * Returns a sequential {@code Spliterator} iterating over the current * enumerator.//w w w .java2s . c o m * * @return the new {@link Spliterator}. * @see #asStream() */ public default Spliterator<E> asSpliterator() { Checks.ensureNonEnumerating(this); return Spliterators.spliteratorUnknownSize(this, Spliterator.ORDERED); }
From source file:org.opennms.netmgt.config.DiscoveryConfigFactory.java
/** * <p>getExcludingInterator</p> * * @param it a {@link java.util.Iterator} object. * @return a {@link java.util.Iterator} object. */// w w w .j a v a2 s . co m @Override public Iterator<IPPollAddress> getExcludingInterator(final Iterator<IPPollAddress> it) { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, ORDERED | IMMUTABLE), false) // Filter out excluded addresses .filter(item -> !isExcluded(item.getAddress())).iterator(); }
From source file:com.intuit.wasabi.repository.cassandra.impl.CassandraAssignmentsRepository.java
@Override @Timed//from w w w . ja v a 2s. com public Assignment getAssignment(User.ID userID, Application.Name appName, Experiment.ID experimentID, Context context) { ListenableFuture<Result<ExperimentUserByUserIdContextAppNameExperimentId>> resultFuture = experimentUserIndexAccessor .asyncSelectBy(userID.toString(), appName.toString(), experimentID.getRawID(), context.toString()); Result<ExperimentUserByUserIdContextAppNameExperimentId> assignmentResult = UninterruptibleUtil .getUninterruptibly(resultFuture); Stream<ExperimentUserByUserIdContextAppNameExperimentId> assignmentResultStream = StreamSupport.stream( Spliterators.spliteratorUnknownSize(assignmentResult.iterator(), Spliterator.ORDERED), false); final Stream<Assignment.Builder> assignmentBuilderStream = assignmentResultStream.map(t -> { Assignment.Builder builder = Assignment.newInstance(Experiment.ID.valueOf(t.getExperimentId())) .withUserID(User.ID.valueOf(t.getUserId())).withContext(Context.valueOf(t.getContext())); if (nonNull(t.getBucket()) && !t.getBucket().trim().isEmpty()) { builder.withBucketLabel(Bucket.Label.valueOf(t.getBucket())); } return builder; }); Optional<Assignment> assignmentOptional = getAssignmentFromStream(experimentID, userID, context, assignmentBuilderStream); return assignmentOptional.isPresent() ? assignmentOptional.get() : null; }
From source file:controllers.nwbib.Application.java
/** * @param q Query to search in all fields * @param person Query for a person associated with the resource * @param name Query for the resource name (title) * @param subject Query for the resource subject * @param id Query for the resource id//www . j av a 2s . c o m * @param publisher Query for the resource publisher * @param issued Query for the resource issued year * @param medium Query for the resource medium * @param nwbibspatial Query for the resource nwbibspatial classification * @param nwbibsubject Query for the resource nwbibsubject classification * @param from The page start (offset of page of resource to return) * @param size The page size (size of page of resource to return) * @param owner Owner filter for resource queries * @param t Type filter for resource queries * @param field The facet field (the field to facet over) * @param sort Sorting order for results ("newest", "oldest", "" -> relevance) * @param set The set, overrides the default NWBib set if not empty * @param location A polygon describing the subject area of the resources * @param word A word, a concept from the hbz union catalog * @param corporation A corporation associated with the resource * @param raw A query string that's directly (unprocessed) passed to ES * @return The search results */ public static Promise<Result> facets(String q, String person, String name, String subject, String id, String publisher, String issued, String medium, String nwbibspatial, String nwbibsubject, int from, int size, String owner, String t, String field, String sort, String set, String location, String word, String corporation, String raw) { String key = String.format("facets.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", field, q, person, name, id, publisher, set, location, word, corporation, raw, subject, issued, medium, nwbibspatial, nwbibsubject, owner, t); Result cachedResult = (Result) Cache.get(key); if (cachedResult != null) { return Promise.promise(() -> cachedResult); } String labelTemplate = "<span class='%s'/> %s (%s)"; Function<JsonNode, Pair<JsonNode, String>> toLabel = json -> { String term = json.get("term").asText(); int count = json.get("count").asInt(); String icon = Lobid.facetIcon(Arrays.asList(term), field); String label = Lobid.facetLabel(Arrays.asList(term), field, ""); String fullLabel = String.format(labelTemplate, icon, label, count); return Pair.of(json, fullLabel); }; Predicate<Pair<JsonNode, String>> labelled = pair -> { JsonNode json = pair.getLeft(); String label = pair.getRight(); int count = json.get("count").asInt(); return (!label.contains("http") || label.contains("nwbib")) && label.length() > String.format(labelTemplate, "", "", count).length(); }; Collator collator = Collator.getInstance(Locale.GERMAN); Comparator<Pair<JsonNode, String>> sorter = (p1, p2) -> { String t1 = p1.getLeft().get("term").asText(); String t2 = p2.getLeft().get("term").asText(); boolean t1Current = current(subject, medium, nwbibspatial, nwbibsubject, owner, t, field, t1, raw); boolean t2Current = current(subject, medium, nwbibspatial, nwbibsubject, owner, t, field, t2, raw); if (t1Current == t2Current) { if (!field.equals(ISSUED_FIELD)) { Integer c1 = p1.getLeft().get("count").asInt(); Integer c2 = p2.getLeft().get("count").asInt(); return c2.compareTo(c1); } String l1 = p1.getRight().substring(p1.getRight().lastIndexOf('>') + 1); String l2 = p2.getRight().substring(p2.getRight().lastIndexOf('>') + 1); return collator.compare(l1, l2); } return t1Current ? -1 : t2Current ? 1 : 0; }; Function<Pair<JsonNode, String>, String> toHtml = pair -> { JsonNode json = pair.getLeft(); String fullLabel = pair.getRight(); String term = json.get("term").asText(); if (field.equals(SUBJECT_LOCATION_FIELD)) { GeoPoint point = new GeoPoint(term); term = String.format("%s,%s", point.getLat(), point.getLon()); } String mediumQuery = !field.equals(MEDIUM_FIELD) // ? medium : queryParam(medium, term); String typeQuery = !field.equals(TYPE_FIELD) // ? t : queryParam(t, term); String ownerQuery = !field.equals(ITEM_FIELD) // ? owner : withoutAndOperator(queryParam(owner, term)); String nwbibsubjectQuery = !field.equals(NWBIB_SUBJECT_FIELD) // ? nwbibsubject : queryParam(nwbibsubject, term); String nwbibspatialQuery = !field.equals(NWBIB_SPATIAL_FIELD) // ? nwbibspatial : queryParam(nwbibspatial, term); String rawQuery = !field.equals(COVERAGE_FIELD) // ? raw : rawQueryParam(raw, term); String locationQuery = !field.equals(SUBJECT_LOCATION_FIELD) // ? location : term; String subjectQuery = !field.equals(SUBJECT_FIELD) // ? subject : queryParam(subject, term); String issuedQuery = !field.equals(ISSUED_FIELD) // ? issued : queryParam(issued, term); boolean current = current(subject, medium, nwbibspatial, nwbibsubject, owner, t, field, term, raw); String routeUrl = routes.Application.search(q, person, name, subjectQuery, id, publisher, issuedQuery, mediumQuery, nwbibspatialQuery, nwbibsubjectQuery, from, size, ownerQuery, typeQuery, sort(sort, nwbibspatialQuery, nwbibsubjectQuery, subjectQuery), false, set, locationQuery, word, corporation, rawQuery).url(); String result = String.format( "<li " + (current ? "class=\"active\"" : "") + "><a class=\"%s-facet-link\" href='%s'>" + "<input onclick=\"location.href='%s'\" class=\"facet-checkbox\" " + "type=\"checkbox\" %s> %s</input>" + "</a></li>", Math.abs(field.hashCode()), routeUrl, routeUrl, current ? "checked" : "", fullLabel); return result; }; Promise<Result> promise = Lobid.getFacets(q, person, name, subject, id, publisher, issued, medium, nwbibspatial, nwbibsubject, owner, field, t, set, location, word, corporation, raw).map(json -> { Stream<JsonNode> stream = StreamSupport.stream( Spliterators.spliteratorUnknownSize(json.findValue("entries").elements(), 0), false); if (field.equals(ITEM_FIELD)) { stream = preprocess(stream); } String labelKey = String.format( "facets-labels.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", field, raw, q, person, name, id, publisher, set, word, corporation, subject, issued, medium, nwbibspatial, nwbibsubject, raw, field.equals(ITEM_FIELD) ? "" : owner, t, location); @SuppressWarnings("unchecked") List<Pair<JsonNode, String>> labelledFacets = (List<Pair<JsonNode, String>>) Cache .get(labelKey); if (labelledFacets == null) { labelledFacets = stream.map(toLabel).filter(labelled).collect(Collectors.toList()); Cache.set(labelKey, labelledFacets, ONE_DAY); } return labelledFacets.stream().sorted(sorter).map(toHtml).collect(Collectors.toList()); }).map(lis -> ok(String.join("\n", lis))); promise.onRedeem(r -> Cache.set(key, r, ONE_DAY)); return promise; }
From source file:com.ikanow.aleph2.management_db.mongodb.services.IkanowV1SyncService_Buckets.java
/** Builds a V2 bucket out of a V1 source * @param src_json// ww w.j a v a 2s.com * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException * @throws ParseException */ protected static DataBucketBean getBucketFromV1Source(final JsonNode src_json) throws JsonParseException, JsonMappingException, IOException, ParseException { // (think we'll use key instead of _id): //final String _id = safeJsonGet(JsonUtils._ID, src_json).asText(); final String key = safeJsonGet("key", src_json).asText(); final String created = safeJsonGet("created", src_json).asText(); final String modified = safeJsonGet("modified", src_json).asText(); final String title = safeJsonGet("title", src_json).asText(); final String description = safeJsonGet("description", src_json).asText(); final String owner_id = safeJsonGet("ownerId", src_json).asText(); final JsonNode tags = safeJsonGet("tags", src_json); // collection of strings //final JsonNode comm_ids = safeJsonGet("communityIds", src_json); // collection of strings final JsonNode px_pipeline = safeJsonGet("processingPipeline", src_json); // collection of JSON objects, first one should have data_bucket final JsonNode px_pipeline_first_el = ((ObjectNode) px_pipeline.get(0)) .without(Arrays.asList("test_params")); final JsonNode data_bucket_tmp = safeJsonGet("data_bucket", px_pipeline_first_el);// (WARNING: mutable, see below) final JsonNode scripting = safeJsonGet("scripting", data_bucket_tmp); // HANDLE SUBSTITUTION final String sub_prefix = Optional.ofNullable(scripting.get("sub_prefix")).map(x -> x.asText()) .orElse("$$SCRIPT_"); final String sub_suffix = Optional.ofNullable(scripting.get("sub_suffix")).map(x -> x.asText()) .orElse("$$"); final List<UnaryOperator<String>> search_replace = StreamSupport .stream(Spliterators.spliteratorUnknownSize(scripting.fieldNames(), Spliterator.ORDERED), false) .filter(f -> !f.equals("sub_prefix") && !f.equals("sub_suffix")) // (remove non language fields) .map(lang -> Tuples._2T(scripting.get(lang), lang)) // Get (separator regex, entire script, sub prefix) .map(scriptobj_lang -> Tuples._3T(safeJsonGet("separator_regex", scriptobj_lang._1()).asText(), safeJsonGet("script", scriptobj_lang._1()).asText(), sub_prefix + scriptobj_lang._2())) // Split each "entire script" up into blocks of format (bloc, lang) .<Stream<Tuple2<String, String>>>map(regex_script_lang -> Stream.concat( Stream.of(Tuples._2T(regex_script_lang._2(), regex_script_lang._3())), regex_script_lang._1().isEmpty() ? Stream.of(Tuples._2T(regex_script_lang._2(), regex_script_lang._3())) : Arrays.stream(regex_script_lang._2().split(regex_script_lang._1())) .<Tuple2<String, String>>map(s -> Tuples._2T(s, regex_script_lang._3())))) // Associate a per-lang index with each script block -> (replacement, string_sub) .<Tuple2<String, String>>flatMap(stream -> StreamUtils.zip(stream, Stream.iterate(0, i -> i + 1), (script_lang, i) -> Tuples._2T( script_lang._1().replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "\\r"), i == 0 ? script_lang._2() + sub_suffix // (entire thing) : script_lang._2() + "_" + i + sub_suffix))) //(broken down components) .<UnaryOperator<String>>map(t2 -> (String s) -> s.replace(t2._2(), t2._1())) //(need to escape "s and newlines) .collect(Collectors.toList()); // Apply the list of transforms to the string ((ObjectNode) data_bucket_tmp).remove("scripting"); // (WARNING: mutable) final String data_bucket_str = search_replace.stream().reduce(data_bucket_tmp.toString(), (acc, s) -> s.apply(acc), (acc1, acc2) -> acc1); // Convert back to the bucket JSON final JsonNode data_bucket = ((ObjectNode) _mapper.readTree(data_bucket_str)) .without(Arrays.asList("test_params")); final DataBucketBean bucket = BeanTemplateUtils.build(data_bucket, DataBucketBean.class) .with(DataBucketBean::_id, getBucketIdFromV1SourceKey(key)) .with(DataBucketBean::created, parseJavaDate(created)) .with(DataBucketBean::modified, parseJavaDate(modified)).with(DataBucketBean::display_name, title) .with(DataBucketBean::description, description).with(DataBucketBean::owner_id, owner_id) .with(DataBucketBean::tags, StreamSupport.stream(tags.spliterator(), false).map(jt -> jt.asText()) .collect(Collectors.toSet())) .done().get(); return bucket; }
From source file:com.joyent.manta.client.MantaClient.java
/** * Return a stream of the contents of a directory in Manta. * * @param path The fully qualified path of the directory. * @return A {@link Stream} of {@link MantaObjectResponse} listing the contents of the directory. * @throws IOException thrown when there is a problem getting the listing over the network *///w ww . ja v a 2s. co m public Stream<MantaObject> listObjects(final String path) throws IOException { final MantaDirectoryListingIterator itr = streamingIterator(path); /* We preemptively check the iterator for a next value because that will * trigger an error if the path doesn't exist or is otherwise inaccessible. * This error typically takes the form of an UncheckedIOException, so we * unwind that exception if the cause is a MantaClientHttpResponseException * and rethrow another MantaClientHttpResponseException, so that the * stacktrace will point to this running method. */ try { if (!itr.hasNext()) { itr.close(); return Stream.empty(); } } catch (UncheckedIOException e) { if (e.getCause() instanceof MantaClientHttpResponseException) { throw e.getCause(); } else { throw e; } } final int additionalCharacteristics = Spliterator.CONCURRENT | Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.DISTINCT; Stream<Map<String, Object>> backingStream = StreamSupport .stream(Spliterators.spliteratorUnknownSize(itr, additionalCharacteristics), false); Stream<MantaObject> stream = backingStream.map(MantaObjectConversionFunction.INSTANCE).onClose(itr::close); danglingStreams.add(stream); return stream; }