public static void main(String[] args) {

    // stream.collect(collector): values
    // Collectors.groupingBy(classifier)
    Stream<Locale> locales = Stream.of(Locale.getAvailableLocales());
    Map<String, List<Locale>> languageToLocales =
        locales.collect(Collectors.groupingBy(Locale::getDisplayLanguage));
    System.out.println(languageToLocales);

    System.out.println("--------------");

    // stream.collect(collector): values
    // Collectors.groupingBy(classifier, downstream)
    locales = Stream.of(Locale.getAvailableLocales());
    Map<String, Long> languageToLocalesCounting =
        locales.collect(Collectors.groupingBy(Locale::getDisplayLanguage, Collectors.counting()));
    System.out.println(languageToLocalesCounting);

    System.out.println("--------------");

    // stream.collect(collector): values
    // Collectors.partitioningBy(predicate)
    locales = Stream.of(Locale.getAvailableLocales());
    Map<Boolean, List<Locale>> englishAndOtherLocales =
        locales.collect(Collectors.partitioningBy(l -> l.getLanguage().equals("en")));
    List<Locale> englishLocales = englishAndOtherLocales.get(true);
    System.out.println(englishLocales);

    System.out.println("--------------");
  }
Ejemplo n.º 2
0
  public void testFind() throws IOException {
    PathBiPredicate pred = new PathBiPredicate((path, attrs) -> true);

    try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
      Set<Path> result = s.collect(Collectors.toCollection(TreeSet::new));
      assertEquals(pred.visited(), all);
      assertEquals(result.toArray(new Path[0]), pred.visited());
    }

    pred = new PathBiPredicate((path, attrs) -> attrs.isSymbolicLink());
    try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
      s.forEach(path -> assertTrue(Files.isSymbolicLink(path)));
      assertEquals(pred.visited(), all);
    }

    pred = new PathBiPredicate((path, attrs) -> path.getFileName().toString().startsWith("e"));
    try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
      s.forEach(path -> assertEquals(path.getFileName().toString(), "empty"));
      assertEquals(pred.visited(), all);
    }

    pred =
        new PathBiPredicate(
            (path, attrs) ->
                path.getFileName().toString().startsWith("l") && attrs.isRegularFile());
    try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
      s.forEach(path -> fail("Expect empty stream"));
      assertEquals(pred.visited(), all);
    }
  }
Ejemplo n.º 3
0
 public List<GameObject> getGameObjectsInRange(
     Vector2f pos, int width, int height, Collection<GameObject> list) {
   Rectangle range = new Rectangle(pos.x, pos.y, width, height);
   Stream<GameObject> filter =
       list.stream().filter(e -> range.contains(e.getPos().x, e.getPos().y));
   return filter.collect(Collectors.toList());
 }
 @Test
 public void findAllAsStream() {
   Entity entity0 = mock(Entity.class);
   Query query = mock(Query.class);
   when(decoratedRepository.findAll(query)).thenReturn(Stream.of(entity0));
   Stream<Entity> entities = entityListenerRepositoryDecorator.findAll(query);
   assertEquals(entities.collect(Collectors.toList()), Arrays.asList(entity0));
 }
Ejemplo n.º 5
0
 public ClusterBlockException indexBlockedException(ClusterBlockLevel level, String index) {
   if (!indexBlocked(level, index)) {
     return null;
   }
   Stream<ClusterBlock> blocks =
       concat(global(level).stream(), blocksForIndex(level, index).stream());
   return new ClusterBlockException(unmodifiableSet(blocks.collect(toSet())));
 }
 /**
  * Returns a list of lines read from the specified file. If the file is empty or unreadable,
  * returns an empty list.
  */
 private static List<String> readLinesFromFile(String fileName) {
   try (Stream<String> lines =
       Files.lines(new File(fileName).toPath(), Charset.defaultCharset())) {
     return lines.collect(Collectors.toList());
   } catch (IOException e) {
     System.err.printf("Failed to load file: %s", fileName);
     return new ArrayList<>();
   }
 }
Ejemplo n.º 7
0
 @Test
 public void _11_그룹핑() {
   final Stream<Locale> stream = Stream.of(Locale.getAvailableLocales());
   Map<String, List<Locale>> countryToLocales =
       stream.collect(
           Collectors.groupingBy(Locale::getCountry) // 묶을 기준값
           );
   System.out.println(countryToLocales.get("CH"));
 }
 @Test
 public void findAllAsStream() {
   Entity entity0 = Mockito.mock(Entity.class);
   @SuppressWarnings("unchecked")
   Query<Entity> query = Mockito.mock(Query.class);
   Mockito.when(decoratedRepository.findAll(query)).thenReturn(Stream.of(entity0));
   Stream<Entity> entities = entityListenerRepositoryDecorator.findAll(query);
   Assert.assertEquals(entities.collect(Collectors.toList()), Arrays.asList(entity0));
 }
Ejemplo n.º 9
0
 public List<TMap> getList(String key) {
   List<Map> list = (List<Map>) get(key);
   if (list == null) {
     return Collections.emptyList();
   } else {
     Stream<TMap> stream = list.stream().map(TMap::new);
     return stream.collect(Collectors.toList());
   }
 }
Ejemplo n.º 10
0
 public static <T, U, V extends Map<T, U>, W> V toMap(
     Function<W, T> keyMapper,
     Function<W, U> valueMapper,
     Supplier<V> mapSupplier,
     Stream<W> stream) {
   return stream.collect(
       Collectors.toMap(
           keyMapper::apply, valueMapper::apply, (value1, value2) -> value2, mapSupplier));
 }
Ejemplo n.º 11
0
 private void checkMalformedInputException(Stream<String> s) {
   try {
     List<String> lines = s.collect(Collectors.toList());
     fail("UncheckedIOException expected");
   } catch (UncheckedIOException ex) {
     IOException cause = ex.getCause();
     assertTrue(cause instanceof MalformedInputException, "MalformedInputException expected");
   }
 }
 public void accept(Stream<T> classes) {
   classes
       .collect(groupingBy(identity(), counting()))
       . //
       forEach(
           (clazz, count) -> {
             types
                 .computeIfAbsent(clazz, c -> new IntSummaryStatistics())
                 .accept(count.intValue());
           });
 }
 @Test
 public void findAllStream() {
   Object id0 = "id0";
   Object id1 = "id1";
   Entity entity0 = mock(Entity.class);
   Entity entity1 = mock(Entity.class);
   Stream<Object> entityIds = Stream.of(id0, id1);
   when(decoratedRepository.findAll(entityIds)).thenReturn(Stream.of(entity0, entity1));
   Stream<Entity> expectedEntities = entityListenerRepositoryDecorator.findAll(entityIds);
   assertEquals(expectedEntities.collect(Collectors.toList()), Arrays.asList(entity0, entity1));
 }
Ejemplo n.º 14
0
 protected void updateEntitiesList() {
   this.entities = getAll();
   if (this.searchCriteria != null && this.searchCriteria.size() > 0) {
     Stream<T> stream = this.entities.stream();
     for (FieldSearchCriterion<T> fieldSearchCriterion : this.searchCriteria) {
       stream = stream.filter(fieldSearchCriterion.buildPredicate());
     }
     this.entities = stream.collect(Collectors.toList());
   }
   this.notifyObservers(null);
 }
Ejemplo n.º 15
0
  /**
   * Get the Stream as a list of String(s).
   *
   * @param filePath
   * @return
   */
  public static List<String> getFileInputAsList(String filePath) {
    try (Stream<String> stream = readInputFromFile(filePath)) {

      return stream.collect(Collectors.toList());

    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
Ejemplo n.º 16
0
  @Test
  public void stringStreamWithNull() {

    Stream<String> res =
        For.stream("hello world".chars().boxed().map(i -> Character.toChars(i)[0]))
            .iterable(i -> (Iterable<String>) null)
            .yield(v1 -> v2 -> "" + v1 + v2)
            .unwrap();
    List<String> expected = Arrays.asList();

    assertThat(expected, equalTo(res.collect(Collectors.toList())));
  }
Ejemplo n.º 17
0
 @Test
 public void _10_toMap() {
   Stream<Locale> localeStream = Stream.of(Locale.getAvailableLocales());
   Map<String, String> languageNames =
       localeStream.collect(
           Collectors.toMap(
               l -> l.getDisplayLanguage(), // key 맵핑
               l -> l.getDisplayLanguage(l), // value 맵핑
               (existingValue, newValue) -> existingValue)); // key가 충돌하는 경우 머지 함수
   assertThat(languageNames.get("우크라이나어"), is("українська"));
   assertThat(languageNames.get("태국어"), is("ไทย"));
 }
 @Test
 public void findAllStreamFetch() {
   Fetch fetch = new Fetch();
   Object id0 = "id0";
   Object id1 = "id1";
   Entity entity0 = Mockito.mock(Entity.class);
   Entity entity1 = Mockito.mock(Entity.class);
   Stream<Object> entityIds = Stream.of(id0, id1);
   Mockito.when(decoratedRepository.findAll(entityIds, fetch))
       .thenReturn(Stream.of(entity0, entity1));
   Stream<Entity> expectedEntities = entityListenerRepositoryDecorator.findAll(entityIds, fetch);
   Assert.assertEquals(
       expectedEntities.collect(Collectors.toList()), Arrays.asList(entity0, entity1));
 }
Ejemplo n.º 19
0
  @Test
  public void _05_상태유지변환_StatefulTransformation() {
    // distinct는 이전 값을 기억하고 있어야 한다.
    Stream<String> uniqueWords =
        Stream.of("merrily", "merrily", "merrily", "gently", "merrily").distinct();

    final List<String> actual = uniqueWords.collect(Collectors.toList());
    assertThat(actual.get(0), is("merrily"));
    assertThat(actual.get(1), is("gently"));

    // sort도 이전 값을 기억하고 있어야한다. (전체 값을 다 알고 있어야..)
    Stream<String> longestFirst =
        words.stream().sorted(Comparator.comparing(String::length).reversed());
    final Optional<String> longestFirstFirst = longestFirst.findFirst();
    assertThat(longestFirstFirst.get(), is("disappointment"));
  }
Ejemplo n.º 20
0
 public ClusterBlockException indicesBlockedException(ClusterBlockLevel level, String[] indices) {
   boolean indexIsBlocked = false;
   for (String index : indices) {
     if (indexBlocked(level, index)) {
       indexIsBlocked = true;
     }
   }
   if (!indexIsBlocked) {
     return null;
   }
   Function<String, Stream<ClusterBlock>> blocksForIndexAtLevel =
       index -> blocksForIndex(level, index).stream();
   Stream<ClusterBlock> blocks =
       concat(global(level).stream(), Stream.of(indices).flatMap(blocksForIndexAtLevel));
   return new ClusterBlockException(unmodifiableSet(blocks.collect(toSet())));
 }
  @Override
  protected EntityLivingBase findNewTarget() {
    double maxDist = getTargetDistance();

    List<T> entities =
        EntitySelector.type(
            taskOwner.worldObj,
            targetClass,
            taskOwner.boundingBox.expand(maxDist, maxDist * 0.5D, maxDist));

    Stream<T> stream =
        entities
            .stream()
            .filter(entity -> entity.getDistanceSqToEntity(taskOwner) <= maxDist * maxDist);
    if (predicate != null) stream = stream.filter(predicate);

    entities = stream.collect(Collectors.toList());
    if (entities.isEmpty()) return null;

    return entities.get(taskOwner.worldObj.rand.nextInt(entities.size()));
  }
Ejemplo n.º 22
0
  public static JsonObject invoke(JsonObject json, String area) {
    JsonObject responseData = json.getObject("ResponseData");

    if (responseData == null) {
      return new JsonObject();
    }

    if (Server.l != null) {
      Server.l.info(getLatestUpdate(json) + " - " + getExecutionTime(json));
    }

    Predicate<String> isJsonArray = name -> responseData.getField(name) instanceof JsonArray;

    Function<String, List<JsonObject>> jsonArrayToList =
        name -> {
          Stream<Object> stream =
              stream(responseData.<JsonArray>getField(name).spliterator(), false);
          Stream<JsonObject> objectStream = stream.filter(getFilterFor(area));
          return objectStream.collect(toList());
        };

    Predicate<Map.Entry<String, List<JsonObject>>> nonEmpty = entry -> !entry.getValue().isEmpty();

    Stream<Map.Entry<String, List<JsonObject>>> entryStream =
        responseData
            .getFieldNames()
            .stream()
            .filter(isJsonArray)
            .collect(toMap(String::toLowerCase, jsonArrayToList))
            .entrySet()
            .stream()
            .filter(nonEmpty);

    JsonObject result = toJsonObject(entryStream);

    setUniqueKeysOn(result.getArray("trains"));
    writeHeaderFields(result);

    return result;
  }
Ejemplo n.º 23
0
 private void checkLines(Stream<String> s, List<String> expected) {
   List<String> lines = s.collect(Collectors.toList());
   assertTrue(lines.size() == expected.size(), "Unexpected number of lines");
   assertTrue(lines.equals(expected), "Unexpected content");
 }
Ejemplo n.º 24
0
 public <K, V> Map<K, List<T>> by(Function<T, K> transformation) {
   return stream.collect(Collectors.groupingBy(transformation));
 }
Ejemplo n.º 25
0
  /**
   * calls the event handlers and returns a result with information on whether it needs to be ack'd
   * and whether it needs to be indexed
   *
   * @param messages
   * @return
   */
  private List<IndexEventResult> callEventHandlers(final List<QueueMessage> messages) {

    if (logger.isDebugEnabled()) {
      logger.debug("callEventHandlers with {} message(s)", messages.size());
    }

    Stream<IndexEventResult> indexEventResults =
        messages
            .stream()
            .map(
                message -> {
                  if (logger.isDebugEnabled()) {
                    logger.debug(
                        "Queue message with ID {} has been received {} time(s)",
                        message.getMessageId(),
                        message.getReceiveCount());
                  }

                  AsyncEvent event = null;
                  try {
                    event = (AsyncEvent) message.getBody();

                  } catch (ClassCastException cce) {
                    logger.error("Failed to deserialize message body", cce);
                    return new IndexEventResult(
                        Optional.absent(), Optional.absent(), System.currentTimeMillis());
                  }

                  if (event == null) {
                    logger.error("AsyncEvent type or event is null!");
                    return new IndexEventResult(
                        Optional.absent(), Optional.absent(), System.currentTimeMillis());
                  }

                  final AsyncEvent thisEvent = event;

                  if (logger.isDebugEnabled()) {
                    logger.debug("Processing event with type {}", event.getClass().getSimpleName());
                  }

                  try {

                    IndexOperationMessage single = new IndexOperationMessage();

                    // normal indexing event for an entity
                    if (event instanceof EntityIndexEvent) {

                      single = handleEntityIndexUpdate(message);

                    }
                    // normal indexing event for an edge
                    else if (event instanceof EdgeIndexEvent) {

                      single = handleEdgeIndex(message);

                    }
                    // deletes are 2-part, actual IO to delete data, then queue up a de-index
                    else if (event instanceof EdgeDeleteEvent) {

                      single = handleEdgeDelete(message);
                    }
                    // deletes are 2-part, actual IO to delete data, then queue up a de-index
                    else if (event instanceof EntityDeleteEvent) {

                      single = handleEntityDelete(message);
                    }
                    // initialization has special logic, therefore a special event type and no index
                    // operation message
                    else if (event instanceof InitializeApplicationIndexEvent) {

                      handleInitializeApplicationIndex(event, message);
                    }
                    // this is the main event that pulls the index doc from map persistence and
                    // hands to the index producer
                    else if (event instanceof ElasticsearchIndexEvent) {

                      handleIndexOperation((ElasticsearchIndexEvent) event);

                    } else if (event instanceof DeIndexOldVersionsEvent) {

                      single = handleDeIndexOldVersionEvent((DeIndexOldVersionsEvent) event);

                    } else {

                      throw new Exception(
                          "Unknown EventType for message: " + message.getStringBody().trim());
                    }

                    if (!(event instanceof ElasticsearchIndexEvent)
                        && !(event instanceof InitializeApplicationIndexEvent)
                        && single.isEmpty()) {
                      logger.warn(
                          "No index operation messages came back from event processing for msg: {} ",
                          message.getStringBody().trim());
                    }

                    // if no exception happens and the QueueMessage is returned in these results, it
                    // will get ack'd
                    return new IndexEventResult(
                        Optional.of(single), Optional.of(message), thisEvent.getCreationTime());

                  } catch (IndexDocNotFoundException e) {

                    // this exception is throw when we wait before trying quorum read on map
                    // persistence.
                    // return empty event result so the event's message doesn't get ack'd
                    if (logger.isDebugEnabled()) {
                      logger.debug(e.getMessage());
                    }
                    return new IndexEventResult(
                        Optional.absent(), Optional.absent(), thisEvent.getCreationTime());

                  } catch (Exception e) {

                    // NPEs don't have a detail message, so add something for our log statement to
                    // identify better
                    final String errorMessage;
                    if (e instanceof NullPointerException) {
                      errorMessage = "NullPointerException";
                    } else {
                      errorMessage = e.getMessage();
                    }

                    // if the event fails to process, log and return empty message result so it
                    // doesn't get ack'd
                    logger.error(
                        "{}. Failed to process message: {}",
                        errorMessage,
                        message.getStringBody().trim());
                    return new IndexEventResult(
                        Optional.absent(), Optional.absent(), thisEvent.getCreationTime());
                  }
                });

    return indexEventResults.collect(Collectors.toList());
  }
Ejemplo n.º 26
0
    public void requestExamples() {
      // #ws-holder
      WSRequest request = ws.url("http://example.com");
      // #ws-holder

      // #ws-complex-holder
      WSRequest complexRequest =
          request
              .setHeader("headerKey", "headerValue")
              .setRequestTimeout(1000)
              .setQueryParameter("paramKey", "paramValue");
      // #ws-complex-holder

      // #ws-get
      CompletionStage<WSResponse> responsePromise = complexRequest.get();
      // #ws-get

      String url = "http://example.com";
      // #ws-auth
      ws.url(url).setAuth("user", "password", WSAuthScheme.BASIC).get();
      // #ws-auth

      // #ws-follow-redirects
      ws.url(url).setFollowRedirects(true).get();
      // #ws-follow-redirects

      // #ws-query-parameter
      ws.url(url).setQueryParameter("paramKey", "paramValue");
      // #ws-query-parameter

      // #ws-header
      ws.url(url).setHeader("headerKey", "headerValue").get();
      // #ws-header

      String jsonString = "{\"key1\":\"value1\"}";
      // #ws-header-content-type
      ws.url(url).setHeader("Content-Type", "application/json").post(jsonString);
      // OR
      ws.url(url).setContentType("application/json").post(jsonString);
      // #ws-header-content-type

      // #ws-timeout
      ws.url(url).setRequestTimeout(1000).get();
      // #ws-timeout

      // #ws-post-form-data
      ws.url(url)
          .setContentType("application/x-www-form-urlencoded")
          .post("key1=value1&key2=value2");
      // #ws-post-form-data

      // #ws-post-json
      JsonNode json = Json.newObject().put("key1", "value1").put("key2", "value2");

      ws.url(url).post(json);
      // #ws-post-json

      String value =
          IntStream.range(0, 100).boxed().map(i -> "abcdefghij").reduce("", (a, b) -> a + b);
      ByteString seedValue = ByteString.fromString(value);
      Stream<ByteString> largeSource = IntStream.range(0, 10).boxed().map(i -> seedValue);
      Source<ByteString, ?> largeImage = Source.from(largeSource.collect(Collectors.toList()));
      // #ws-stream-request
      CompletionStage<WSResponse> wsResponse = ws.url(url).setBody(largeImage).execute("PUT");
      // #ws-stream-request
    }
Ejemplo n.º 27
0
  /**
   * 聚集计算
   *
   * @param dataList 待计算的数据
   * @param query 原始查询请求
   * @return LinkedList<ResultRecord> 计算后的数据
   */
  public static List<SearchIndexResultRecord> aggregate(
      List<SearchIndexResultRecord> dataList, int dimSize, List<QueryMeasure> queryMeasures) {

    if (CollectionUtils.isEmpty(queryMeasures) || CollectionUtils.isEmpty(dataList)) {
      LOGGER.info("no need to group.");
      return dataList;
    }

    Set<Integer> countIndex = Sets.newHashSet();
    for (int i = 0; i < queryMeasures.size(); i++) {
      if (queryMeasures.get(i).getAggregator().equals(Aggregator.DISTINCT_COUNT)) {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.info(queryMeasures.get(i) + " ============= begin print values ===== ====");
          final int tmp = i;
          dataList.forEach(rs -> LOGGER.info(rs.getField(tmp) + ""));
          LOGGER.info(" ============= end print measure values ==============");
        }
        countIndex.add(i);
      }
    }

    int arraySize = dataList.get(0).getFieldArraySize();

    long current = System.currentTimeMillis();
    Stream<SearchIndexResultRecord> stream =
        dataList.size() > 300000 ? dataList.parallelStream() : dataList.stream();

    int defaultSize = (int) (dataList.size() > 100 ? dataList.size() * 0.01 : dataList.size());

    final BinaryOperator<SearchIndexResultRecord> reduceOperation =
        (x, y) -> {
          if (!y.getGroupBy().equals(x.getGroupBy())) {
            x = SearchIndexResultRecord.of(arraySize);
            x.setGroupBy(y.getGroupBy());
            for (int i = 0; i < dimSize; i++) {
              x.setField(i, y.getField(i));
            }
          }
          try {
            int index = dimSize;
            for (int i = 0; i < queryMeasures.size(); i++) {
              QueryMeasure measure = queryMeasures.get(i);
              index = i + dimSize;
              if (measure.getAggregator().equals(Aggregator.DISTINCT_COUNT)) {
                if (!x.getDistinctMeasures().containsKey(i)) {
                  x.getDistinctMeasures().put(i, new HashSet<>(defaultSize));
                }

                if (y.getDistinctMeasures().containsKey(i)) {
                  x.getDistinctMeasures().get(i).addAll(y.getDistinctMeasures().get(i));
                } else if (y.getField(index) != null) {
                  x.getDistinctMeasures().get(i).add(y.getField(index));
                }

              } else {
                x.setField(
                    index, measure.getAggregator().aggregate(x.getField(index), y.getField(index)));
              }
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
          return x;
        };
    final Collector<SearchIndexResultRecord, ?, SearchIndexResultRecord> reducing =
        Collectors.reducing(SearchIndexResultRecord.of(arraySize), reduceOperation);
    Map<String, SearchIndexResultRecord> groupResult =
        stream.collect(
            Collectors.groupingByConcurrent(SearchIndexResultRecord::getGroupBy, reducing));

    if (CollectionUtils.isNotEmpty(countIndex)) {
      groupResult
          .values()
          .forEach(
              record -> {
                for (int index : countIndex) {
                  if (record.getDistinctMeasures() != null
                      && record.getDistinctMeasures().containsKey(index)) {
                    record.setField(
                        dimSize + index, record.getDistinctMeasures().get(index).size());
                  }
                }
              });
    }
    LOGGER.info(
        "group agg(sum) cost: {}ms, size:{}!",
        (System.currentTimeMillis() - current),
        groupResult.size());
    return new ArrayList<>(groupResult.values());
  }
Ejemplo n.º 28
0
 public static <T> Map<T, List<Tuple>> groupBy(Stream<Tuple> tupleStream, String column) {
   return tupleStream.collect(groupingBy((Tuple tuple) -> (T) tuple.val(column).get()));
 }
Ejemplo n.º 29
0
 public static Annotations of(final Stream<Annotation> annotations) {
   return new Annotations(annotations.collect(Collectors.toList()));
 }
Ejemplo n.º 30
0
 /**
  * Of corpus.
  *
  * @param documentStream the document stream
  * @return the corpus
  */
 static Corpus of(@NonNull Stream<Document> documentStream) {
   return new InMemoryCorpus(documentStream.collect(Collectors.toList()));
 }