Example usage for java.util.stream Stream map

List of usage examples for java.util.stream Stream map

Introduction

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

Prototype

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

Source Link

Document

Returns a stream consisting of the results of applying the given function to the elements of this stream.

Usage

From source file:com.nextdoor.bender.operation.conditional.ConditionalOperationTest.java

@Test
public void testSingleConditionMatch() {
    /*// w w w.jav a2 s  . com
     * Setup the pipeline of operation processors
     */
    List<Pair<FilterOperation, List<OperationProcessor>>> conditions = new ArrayList<Pair<FilterOperation, List<OperationProcessor>>>();
    List<OperationProcessor> case1Ops = new ArrayList<OperationProcessor>();

    DummyAppendOperationFactory pos = new DummyAppendOperationFactory();
    DummyAppendOperationConfig posConf = new DummyAppendOperationConfig();
    posConf.setAppendStr("+");
    pos.setConf(posConf);
    case1Ops.add(new OperationProcessor(pos));
    FilterOperation filter = new BasicFilterOperation(true);
    conditions.add(new ImmutablePair<FilterOperation, List<OperationProcessor>>(filter, case1Ops));

    ConditionalOperation op = new ConditionalOperation(conditions, false);

    /*
     * Create thread that supplies input events
     */
    Queue<InternalEvent> inputQueue = new Queue<InternalEvent>();
    supply(2, inputQueue);

    /*
     * Process
     */
    Stream<InternalEvent> input = inputQueue.stream();
    Stream<InternalEvent> output = op.getOutputStream(input);

    List<String> actual = output.map(m -> {
        return m.getEventObj().getPayload().toString();
    }).collect(Collectors.toList());
    List<String> expected = Arrays.asList("0+", "1+");

    assertEquals(2, actual.size());
    assertTrue(expected.containsAll(actual));
}

From source file:com.nextdoor.bender.operation.conditional.ConditionalOperationTest.java

@Test
public void testSingleConditionNoMatch() {
    /*/*ww w.  j  av a  2  s . co m*/
     * Setup the pipeline of operation processors
     */
    List<Pair<FilterOperation, List<OperationProcessor>>> conditions = new ArrayList<Pair<FilterOperation, List<OperationProcessor>>>();
    List<OperationProcessor> case1Ops = new ArrayList<OperationProcessor>();

    DummyAppendOperationFactory pos = new DummyAppendOperationFactory();
    DummyAppendOperationConfig posConf = new DummyAppendOperationConfig();
    posConf.setAppendStr("+");
    pos.setConf(posConf);
    case1Ops.add(new OperationProcessor(pos));
    FilterOperation filter = new BasicFilterOperation(false);
    conditions.add(new ImmutablePair<FilterOperation, List<OperationProcessor>>(filter, case1Ops));

    ConditionalOperation op = new ConditionalOperation(conditions, false);

    /*
     * Create thread that supplies input events
     */
    Queue<InternalEvent> inputQueue = new Queue<InternalEvent>();
    supply(2, inputQueue);

    /*
     * Process
     */
    Stream<InternalEvent> input = inputQueue.stream();
    Stream<InternalEvent> output = op.getOutputStream(input);

    List<String> actual = output.map(m -> {
        return m.getEventObj().getPayload().toString();
    }).collect(Collectors.toList());
    List<String> expected = Arrays.asList("0", "1");

    assertEquals(2, actual.size());
    assertTrue(expected.containsAll(actual));
}

From source file:com.nextdoor.bender.operation.conditional.ConditionalOperationTest.java

@Test
public void testTwoConditions() {
    List<Pair<FilterOperation, List<OperationProcessor>>> conditions = new ArrayList<Pair<FilterOperation, List<OperationProcessor>>>();
    /*//from  w  ww  . j  ava 2s .  c  o m
     * Case 1
     */
    List<OperationProcessor> case1Ops = new ArrayList<OperationProcessor>();

    DummyAppendOperationFactory pos = new DummyAppendOperationFactory();
    DummyAppendOperationConfig posConf = new DummyAppendOperationConfig();
    posConf.setAppendStr("+");
    pos.setConf(posConf);
    case1Ops.add(new OperationProcessor(pos));
    FilterOperation case1Filter = new BasicFilterOperation(false);
    conditions.add(new ImmutablePair<FilterOperation, List<OperationProcessor>>(case1Filter, case1Ops));

    /*
     * Case 2
     */
    List<OperationProcessor> case2Ops = new ArrayList<OperationProcessor>();

    DummyAppendOperationFactory neg = new DummyAppendOperationFactory();
    DummyAppendOperationConfig negConf = new DummyAppendOperationConfig();
    negConf.setAppendStr("-");
    neg.setConf(negConf);
    case2Ops.add(new OperationProcessor(neg));
    FilterOperation case2Filter = new BasicFilterOperation(true);
    conditions.add(new ImmutablePair<FilterOperation, List<OperationProcessor>>(case2Filter, case2Ops));

    ConditionalOperation op = new ConditionalOperation(conditions, false);

    /*
     * Create thread that supplies input events
     */
    Queue<InternalEvent> inputQueue = new Queue<InternalEvent>();
    supply(2, inputQueue);

    /*
     * Process
     */
    Stream<InternalEvent> input = inputQueue.stream();
    Stream<InternalEvent> output = op.getOutputStream(input);

    List<String> actual = output.map(m -> {
        return m.getEventObj().getPayload().toString();
    }).collect(Collectors.toList());
    List<String> expected = Arrays.asList("0-", "1-");

    assertEquals(2, actual.size());
    assertTrue(expected.containsAll(actual));
}

From source file:com.nextdoor.bender.operation.conditional.ConditionalOperationTest.java

@Test
public void testTwoConditionsNoMatch() {
    List<Pair<FilterOperation, List<OperationProcessor>>> conditions = new ArrayList<Pair<FilterOperation, List<OperationProcessor>>>();
    /*/*from   w  w w  .  j a  v  a2  s  .  co  m*/
     * Case 1
     */
    List<OperationProcessor> case1Ops = new ArrayList<OperationProcessor>();

    DummyAppendOperationFactory pos = new DummyAppendOperationFactory();
    DummyAppendOperationConfig posConf = new DummyAppendOperationConfig();
    posConf.setAppendStr("+");
    pos.setConf(posConf);
    case1Ops.add(new OperationProcessor(pos));
    FilterOperation case1Filter = new BasicFilterOperation(false);
    conditions.add(new ImmutablePair<FilterOperation, List<OperationProcessor>>(case1Filter, case1Ops));

    /*
     * Case 2
     */
    List<OperationProcessor> case2Ops = new ArrayList<OperationProcessor>();

    DummyAppendOperationFactory neg = new DummyAppendOperationFactory();
    DummyAppendOperationConfig negConf = new DummyAppendOperationConfig();
    negConf.setAppendStr("-");
    neg.setConf(negConf);
    case2Ops.add(new OperationProcessor(neg));
    FilterOperation case2Filter = new BasicFilterOperation(false);
    conditions.add(new ImmutablePair<FilterOperation, List<OperationProcessor>>(case2Filter, case2Ops));

    ConditionalOperation op = new ConditionalOperation(conditions, false);

    /*
     * Create thread that supplies input events
     */
    Queue<InternalEvent> inputQueue = new Queue<InternalEvent>();
    supply(2, inputQueue);

    /*
     * Process
     */
    Stream<InternalEvent> input = inputQueue.stream();
    Stream<InternalEvent> output = op.getOutputStream(input);

    List<String> actual = output.map(m -> {
        return m.getEventObj().getPayload().toString();
    }).collect(Collectors.toList());
    List<String> expected = Arrays.asList("0", "1");

    assertEquals(2, actual.size());
    assertTrue(expected.containsAll(actual));
}

From source file:org.openlmis.fulfillment.repository.OrderRepositoryIntegrationTest.java

private Set<UUID> getIds(Stream<Order> stream) {
    return stream.map(BaseEntity::getId).collect(Collectors.toSet());
}

From source file:it.polimi.diceH2020.SPACE4Cloud.shared.inputData.old.InstanceData_old.java

private <R> List<R> getFromMapperTypeVM(List<TypeVMJobClassKey> lstTypeJobClass,
        Function<Optional<TypeVM>, ? extends R> mapper) {
    Stream<Optional<TypeVM>> strm = lstTypeJobClass.stream().map(k -> mapTypeVMs.get().get(k.getJob()).stream()
            .filter(tVM -> tVM.getId() == k.getTypeVM()).findAny()).filter(Optional::isPresent);
    return strm.map(mapper).collect(toList());
}

From source file:com.netflix.subtitles.ttml.TtmlParagraphResolver.java

private Stream<Slice<PEltype>> split(Stream<PEltype> pStream) {
    SliceBuilder<PEltype> builder = new SliceBuilder<PEltype>()
            .setBeginGetter(p -> ConversionHelper.smpteTimecodeToMilliSeconds(p.getBegin(), frameRate))
            .setEndGetter(p -> ConversionHelper.smpteTimecodeToMilliSeconds(p.getEnd(), frameRate));

    return SplitUtils.split(pStream.map(builder::build).collect(Collectors.toList())).stream();
}

From source file:com.ikanow.aleph2.enrichment.utils.services.JsScriptEngineService.java

@Override
public void onObjectBatch(Stream<Tuple2<Long, IBatchRecord>> batch, Optional<Integer> batch_size,
        Optional<JsonNode> grouping_key) {
    try {/*  ww w  .jav  a  2 s . c o  m*/
        final Supplier<Object> get_batch = () -> batch;
        final Supplier<Object> get_batch_record = () -> batch.map(x -> x._2().getJson());

        ((Invocable) _engine.get()).invokeFunction("aleph2_global_handle_batch", get_batch, get_batch_record,
                batch_size.orElse(null), grouping_key.orElse(null));
    } catch (Throwable e) {
        _logger.error(ErrorUtils.getLongForm("onObjectBatch: {0}", e));

        final Level level = Lambdas.get(() -> {
            if (_mutable_first_error.isSet()) {
                return Level.DEBUG;
            } else {
                _mutable_first_error.set(false);
                return Level.ERROR;
            }
        });
        _bucket_logger.optional()
                .ifPresent(l -> l.log(level, ErrorUtils.lazyBuildMessage(false,
                        () -> this.getClass().getSimpleName() + "."
                                + Optional.ofNullable(_control.get().name()).orElse("no_name"),
                        () -> Optional.ofNullable(_control.get().name()).orElse("no_name") + ".onObjectBatch",
                        () -> null,
                        () -> ErrorUtils.get("Error on batch in job {0}: {1} (first_seen={2})",
                                Optional.ofNullable(_control.get().name()).orElse("(no name)"), e.getMessage(),
                                (level == Level.ERROR)),
                        () -> ImmutableMap.<String, Object>of("full_error",
                                ErrorUtils.getLongForm("{0}", e)))));

        if (_config.get().exit_on_error()) {
            throw new RuntimeException(e);
        }
    }
}

From source file:fi.helsinki.opintoni.service.EventService.java

private List<EventDto> getEvents(List<OodiEvent> oodiEvents, Stream<String> courseIds, Locale locale) {

    List<String> filteredCourseIds = filterOutCancelledCourses(courseIds);

    Map<String, CoursePageCourseImplementation> coursePages = getCoursePages(oodiEvents, filteredCourseIds);

    Stream<CoursePageEvent> coursePageEvents = getCoursePageEvents(filteredCourseIds);

    Stream<EventDto> oodiEventDtos = oodiEvents.stream().filter(oodiEvent -> !oodiEvent.isCancelled)
            .map(oodiEvent -> eventConverter.toDto(oodiEvent,
                    getCoursePage(coursePages, getRealisationId(oodiEvent)), locale));

    Stream<EventDto> coursePageEventDtos = coursePageEvents.map(coursePageEvent -> eventConverter
            .toDto(coursePageEvent, getCoursePage(coursePages, getRealisationId(coursePageEvent))));

    return Stream.concat(oodiEventDtos, coursePageEventDtos).sorted().collect(Collectors.toList());
}