Esempio n. 1
0
  @Override
  public List<City> parse(Stream<String> lines) {
    LOG.debug("Parsing cities ...");

    List<City> countries =
        lines
            .skip(2)
            .map(
                s -> {
                  // ad,arans,Arans,04,,42.5833333,1.5166667
                  String[] strings = s.split(",");
                  City mdo = new City();
                  mdo.setName(strings[2]);
                  mdo.setCountryCode(strings[0].toUpperCase());
                  if (StringUtils.isEmpty(strings[3])) {
                    LOG.debug("Skipping city {}", strings[2]);
                    // skip this city
                    return null;
                  }
                  try {
                    mdo.setRegion(Integer.parseInt(strings[3]));
                  } catch (NumberFormatException nfe) {
                    // skip this city
                    return null;
                  }
                  return mdo;
                })
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
    LOG.debug("No. of parsed cities={}", countries.size());

    return countries;
  }
Esempio n. 2
0
 @Override
 protected BackEndDataProvider<StrBean, SerializablePredicate<StrBean>> createDataProvider() {
   return dataProvider =
       new BackEndDataProvider<>(
           query -> {
             Stream<StrBean> stream =
                 data.stream().filter(t -> query.getFilter().orElse(s -> true).test(t));
             if (!query.getSortOrders().isEmpty()) {
               Comparator<StrBean> sorting =
                   query
                       .getSortOrders()
                       .stream()
                       .map(this::getComparator)
                       .reduce((c1, c2) -> c1.thenComparing(c2))
                       .get();
               stream = stream.sorted(sorting);
             }
             List<StrBean> list =
                 stream
                     .skip(query.getOffset())
                     .limit(query.getLimit())
                     .collect(Collectors.toList());
             list.forEach(s -> System.err.println(s.toString()));
             return list.stream();
           },
           query ->
               (int)
                   data.stream().filter(t -> query.getFilter().orElse(s -> true).test(t)).count());
 }
Esempio n. 3
0
 public void filterFile() {
   try (Stream<String> lines =
       Files.lines(Paths.get(INPUT_FILE_NAME), Charset.forName("ISO-8859-1"))) {
     lines
         .skip(startLine)
         .filter(
             line -> {
               Matcher m = PATTERN.matcher(line);
               return m.matches();
             })
         .map(
             line -> {
               Matcher m = PATTERN.matcher(line);
               m.matches();
               return m.group(1);
             })
         .filter(
             system -> {
               if (system.equals(currentSystem)) {
                 return false;
               } else {
                 currentSystem = system;
                 return true;
               }
             })
         .map(system -> "jumped to " + system)
         .forEach(System.out::println);
   } catch (IOException ioe) {
     System.out.println(ioe.toString());
   }
 }
Esempio n. 4
0
 /**
  * @param path the path to number folders from.
  * @return the folder number of a directory recursively
  */
 static long countSubDirectories(Path path) {
   try (Stream<Path> stream = Files.walk(path)) {
     // skip first to avoid counting input directory
     return stream.skip(1).filter(Files::isDirectory).count();
   } catch (IOException e) {
     throw new RuntimeException(e.getMessage(), e);
   }
 }
Esempio n. 5
0
  public static void main(String[] args) {
    /** 例①: 打印数列的前10项 */
    Stream<Long> fibonacci = Stream.generate(new FibonacciSupplier());
    fibonacci.limit(10).forEach(System.out::println);

    /** 例②: 打印数列的20~30项 */
    Stream<Long> fibonacci2 = Stream.generate(new FibonacciSupplier());
    List<Long> list = fibonacci2.skip(20).limit(10).collect(Collectors.toList());
    list.forEach(System.out::println);
  }
Esempio n. 6
0
 /**
  * Update the dataset limit if it's too large for the system settings.
  *
  * @param metadata the dataset metadata to update.
  */
 private void updateLimit(DataSetMetadata metadata) {
   // auto closable block is really important to make sure the stream is closed after the limit is
   // set
   try (final Stream<DataSetRow> stream = contentStore.stream(metadata)) {
     final Optional<DataSetRow> firstAfterLimit = stream.skip(sizeLimit).findAny();
     if (firstAfterLimit.isPresent()) {
       metadata.getContent().setLimit(sizeLimit);
     }
   }
 }
Esempio n. 7
0
  public static HalRepresentationBuilder paginated(
      String name,
      String self,
      Stream<? extends HalResource> stream,
      long pageNumber,
      long pageSize)
      throws URISyntaxException {
    long effectivePageNumber = pageNumber > 0 ? pageNumber - 1 : pageNumber;
    long displayPageNumber = pageNumber <= 1 ? 1 : pageNumber;
    HalRepresentationBuilder builder = new HalRepresentationBuilder();
    builder
        .addEmbedded(
            name, stream.skip((effectivePageNumber) * pageSize).limit(pageSize).collect(toList()))
        .addLink("next", new URI(self + "?page=" + (displayPageNumber + 1)))
        .addLink(SELF, new URI(self + (displayPageNumber > 1 ? "?page=" + displayPageNumber : "")));

    return builder;
  }
Esempio n. 8
0
  public static void main(String[] args) {
    Stream<Double> piStream = Stream.generate(new PiSupplier());
    piStream.skip(1000000).limit(10).forEach(System.out::println);

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

    Stream<Double> piStream_1 = Stream.generate(new PiSupplier());
    piStream_1
        .map(new EulerTransform())
        .map(new EulerTransform())
        .map(new EulerTransform())
        .map(new EulerTransform())
        .map(new EulerTransform())
        .map(new EulerTransform())
        .skip(10)
        .limit(20)
        .forEach(System.out::println);
  }