org.cactoos.iterable.Mapped Java Examples

The following examples show how to use org.cactoos.iterable.Mapped. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AvgOf.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param src The iterable
 * @checkstyle ExecutableStatementCountCheck (150 lines)
 */
public AvgOf(final Iterable<Scalar<Number>> src) {
    super(
        new Ternary<>(
            new LengthOf(src).longValue(),
            len -> len > 0,
            len -> new Folded<>(
                BigDecimal.ZERO,
                (sum, value) -> sum.add(value, MathContext.DECIMAL128),
                new Mapped<>(
                    number -> BigDecimal.valueOf(
                        number.value().doubleValue()
                    ),
                    src
                )
            ).value().divide(
                BigDecimal.valueOf(len),
                MathContext.DECIMAL128
            ).doubleValue(),
            len -> 0.0
        )
    );
}
 
Example #2
Source File: Method.java    From eo with MIT License 6 votes vote down vote up
/**
 * Convert it to Java.
 *
 * @return Java code
 */
public String java() {
    return new UncheckedText(
        new FormattedText(
            "%s %s(%s)",
            this.type,
            this.name,
            new UncheckedText(
                new JoinedText(
                    ", ",
                    new Mapped<>(
                        Parameter::java,
                        this.parameters
                    )
                )
            ).asString()
        )
    ).asString();
}
 
Example #3
Source File: Ctor.java    From eo with MIT License 6 votes vote down vote up
/**
 * Ctor.
 *
 * @param parameters Constructor parameters
 * @param arguments Super constructor arguments.
 */
public Ctor(
    final List<Parameter> parameters,
    final Collection<Argument> arguments
) {
    this.template = new UncheckedText(
        new FormattedText(
            "public %%s(%s) {\n this(%s);\n}",
            new UncheckedText(
                new JoinedText(
                    ", ",
                    new Mapped<>(Parameter::java, parameters)
                )
            ).asString(),
            new UncheckedText(
                new JoinedText(
                    ", ",
                    new Mapped<>(Argument::java, arguments)
                )
            ).asString()
        )
    ).asString();
}
 
Example #4
Source File: ApacheHeaders.java    From verano-http with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param headers Headers
 */
public ApacheHeaders(final Header... headers) {
    super(
        dict -> new JoinedDict(
            dict,
            new Headers(
                new Mapped<>(
                    header -> new hr.com.vgv.verano.http.parts.Header(
                        header.getName(), header.getValue()
                    ),
                    headers
                )
            ).apply(dict)
        )
    );
}
 
Example #5
Source File: FormParams.java    From verano-http with MIT License 6 votes vote down vote up
@Override
public final String asString() {
    return new UncheckedText(
        new JoinedText(
            "&",
            new Mapped<>(
                input -> String.format(
                    "%s=%s", input.key(),
                    URLEncoder.encode(
                        input.value(),
                        "UTF-8"
                    )
                ),
                new Mapped<>(
                    in -> new KvpOf(in.key().substring(2), in.value()),
                    new Filtered<>(
                        input -> input.key().startsWith("f."),
                        this.dict
                    )
                )
            )
        )
    ).asString();
}
 
Example #6
Source File: JavaInterface.java    From eo with MIT License 6 votes vote down vote up
@Override
public Input code() {
    return new InputOf(
        new FormattedText(
            "%s\n\npublic interface %s {\n  %s\n}\n",
            "package eo;",
            this.name,
            new UncheckedText(
                new JoinedText(
                    "\n    ",
                    new Mapped<>(
                        mtd -> new UncheckedText(
                            new FormattedText("%s;", mtd.java())
                        ).asString(),
                        this.methods
                    )
                )
            ).asString().replace("\n", "\n  ")
        )
    );
}
 
Example #7
Source File: Results.java    From jpeek with MIT License 6 votes vote down vote up
/**
 * Recent artifacts..
 * @return List of them
 */
public Iterable<Iterable<Directive>> recent() {
    return new Mapped<>(
        item -> {
            final String[] parts = item.get("artifact").getS().split(":");
            return new Directives()
                .add("repo")
                .add("group").set(parts[0]).up()
                .add("artifact").set(parts[1]).up()
                .up();
        },
        this.table.frame()
            .where("good", "true")
            .through(
                new QueryValve()
                    .withScanIndexForward(false)
                    .withIndexName("recent")
                    .withConsistentRead(false)
                    // @checkstyle MagicNumber (1 line)
                    .withLimit(25)
                    .withAttributesToGet("artifact")
            )
    );
}
 
Example #8
Source File: AndInThreadsTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void iteratesEmptyList() {
    final List<String> list = new Synced<>(
        new ArrayList<>(2)
    );
    new Assertion<>(
        "Must iterate an empty list",
        new AndInThreads(
            new Mapped<String, Scalar<Boolean>>(
                new FuncOf<>(list::add, () -> true), new IterableOf<>()
            )
        ),
        new ScalarHasValue<>(
            Matchers.allOf(
                Matchers.equalTo(true),
                new MatcherOf<>(
                    value -> {
                        return list.isEmpty();
                    }
                )
            )
        )
    ).affirm();
}
 
Example #9
Source File: ChainedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void withIterable() throws Exception {
    new Assertion<>(
        "Must work with iterable",
        new LengthOf(
            new Filtered<>(
                input -> !input.startsWith("st"),
                new Mapped<>(
                    new Chained<>(
                        input -> input.concat("1"),
                        new IterableOf<Func<String, String>>(
                            input -> input.concat("2"),
                            input -> input.replaceAll("a", "b")
                        ),
                        String::trim
                    ),
                    new IterableOf<>("private", "static", "String")
                )
            )
        ).intValue(),
        new IsEqual<>(2)
    ).affirm();
}
 
Example #10
Source File: ChainedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void withoutIterable() throws Exception {
    new Assertion<>(
        "Must work without iterable",
        new LengthOf(
            new Filtered<>(
                input -> input.endsWith("12"),
                new Mapped<>(
                    new Chained<>(
                        input -> input.concat("1"),
                        input -> input.concat("2")
                    ),
                    new IterableOf<>("public", "final", "class")
                )
            )
        ).intValue(),
        new IsEqual<>(3)
    ).affirm();
}
 
Example #11
Source File: MultiplicationOf.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param src The iterable
 */
public MultiplicationOf(final Iterable<? extends Number> src) {
    super(() -> {
        if (!src.iterator().hasNext()) {
            throw new IllegalArgumentException(
                "Zero arguments - can not multiply"
            );
        }
        return new Folded<>(
            BigDecimal.ONE,
            (mtn, value) -> mtn.multiply(value, MathContext.DECIMAL128),
                new Mapped<>(
                    number -> BigDecimal.valueOf(number.doubleValue()),
                    src
                )
            ).value().doubleValue();
    });
}
 
Example #12
Source File: PrimaryConstructor.java    From eo with MIT License 5 votes vote down vote up
/**
 * Generate constructor java code.
 *
 * @return Java code for constructor.
 */
public String code() {
    return new UncheckedText(
        new FormattedText(
            "public %s(%s) {\n %s \n}",
            this.name,
            new UncheckedText(
                new JoinedText(
                    ", ",
                    new Mapped<>(
                        attr -> attr.java(
                            PrimaryConstructor.CTOR_PARAM_FORMAT
                        ),
                        this.attributes
                    )
                )
            ).asString(),
            new UncheckedText(
                new JoinedText(
                    "\n",
                    new Mapped<>(
                        attr -> attr.java(
                            PrimaryConstructor.CTOR_INIT_FORMAT
                        ),
                        this.attributes
                    )
                )
            ).asString()
        )
    ).asString();
}
 
Example #13
Source File: Tree.java    From eo with MIT License 5 votes vote down vote up
/**
 * Compile it to Java files.
 *
 * @return Java files (path, content)
 */
public Map<String, Input> java() {
    return new StickyMap<>(
        new Mapped<>(
            javaFile -> new MapEntry<>(
                javaFile.path(), javaFile.code()
            ),
            new Mapped<>(RootNode::java, this.nodes)
        )
    );
}
 
Example #14
Source File: Index.java    From jpeek with MIT License 5 votes vote down vote up
@Override
public Iterator<Directive> iterator() {
    return new Directives()
        .add("index")
        .attr("artifact", "unknown")
        .append(new Header())
        .append(
            () -> new Directives()
                .attr(
                    "xmlns:xsi",
                    "http://www.w3.org/2001/XMLSchema-instance"
                    )
                .attr(
                "xsi:noNamespaceSchemaLocation",
                "xsd/index.xsd"
                )
                .iterator()
        )
        .append(
            new Joined<>(
                new Mapped<>(
                    Index::metric,
                    new Filtered<>(
                        path -> path.getFileName().toString().matches(
                            "^[A-Z].+\\.xml$"
                        ),
                        new Directory(this.output)
                    )
                )
            )
        )
        .iterator();
}
 
Example #15
Source File: CdBase.java    From jare with MIT License 5 votes vote down vote up
@Override
@Cacheable(lifetime = 1, unit = TimeUnit.MINUTES)
public Iterable<Domain> domain(final String name) {
    return new Mapped<>(
        this.origin.domain(name),
        CdDomain::new
    );
}
 
Example #16
Source File: CdBase.java    From jare with MIT License 5 votes vote down vote up
@Override
@Cacheable(unit = TimeUnit.HOURS, lifetime = 1)
public Iterable<Domain> all() {
    return new Mapped<>(
        this.origin.all(),
        CdDomain::new
    );
}
 
Example #17
Source File: ObjectBody.java    From eo with MIT License 5 votes vote down vote up
/**
 * Java code for object body.
 *
 * @param name Java class name
 * @return Java code
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public String java(final String name) {
    return new UncheckedText(
        new JoinedText(
            "\n",
            new UncheckedText(
                new JoinedText(
                    "\n",
                    new Mapped<>(
                        attr -> attr.java(ObjectBody.FIELD_FORMAT),
                        this.attrs
                    )
                )
            ).asString(),
            new UncheckedText(
                new JoinedText(
                    "\n",
                    new Mapped<>(ctor -> ctor.java(name), this.ctors)
                )
            ).asString(),
            new PrimaryConstructor(name, this.attrs).code(),
            new UncheckedText(
                new JoinedText(
                    "\n",
                    new Mapped<>(MethodImpl::java, this.methods)
                )
            ).asString()
        )
    ).asString();
}
 
Example #18
Source File: Mistakes.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Worst metrics.
 * @return List of them
 * @throws IOException If fails
 */
public Iterable<Iterable<Directive>> worst() throws IOException {
    return new Mapped<>(
        item -> new Directives()
            .add("metric")
            .attr("id", item.get("metric").getS())
            .add("pos").set(item.get("pos").getN()).up()
            .add("neg").set(item.get("neg").getN()).up()
            .add("psum").set(new DyNum(item, "psum").doubleValue()).up()
            .add("pavg").set(new DyNum(item, "pavg").doubleValue()).up()
            .add("nsum").set(new DyNum(item, "nsum").doubleValue()).up()
            .add("navg").set(new DyNum(item, "navg").doubleValue()).up()
            .add("avg").set(new DyNum(item, "avg").doubleValue()).up()
            .add("champions").set(item.get("champions").getN()).up()
            .add("artifact").set(item.get("artifact").getS()).up()
            .add("mean").set(new DyNum(item, "mean").doubleValue()).up()
            .add("sigma").set(new DyNum(item, "sigma").doubleValue()).up()
            .up(),
        this.table.frame()
            .where("version", new Version().value())
            .through(
                new QueryValve()
                    .withScanIndexForward(false)
                    .withIndexName("mistakes")
                    .withConsistentRead(false)
                    // @checkstyle MagicNumber (1 line)
                    .withLimit(20)
                    .withSelect(Select.ALL_ATTRIBUTES)
            )
    );
}
 
Example #19
Source File: AndWithIndex.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param func Func to map
 * @param src The iterable
 * @param <X> Type of items in the iterable
 * @since 0.24
 */
public <X> AndWithIndex(final BiFunc<X, Integer, Boolean> func,
    final Iterable<X> src) {
    this(
        new Mapped<>(
            item -> (Func<Integer, Boolean>) input
                -> func.apply(item, input), src
        )
    );
}
 
Example #20
Source File: AndInThreadsTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void iteratesList() {
    final List<String> list = new Synced<>(new ListOf<>());
    new Assertion<>(
        "Must iterate a list with a procedure",
        new AndInThreads(
            new Mapped<String, Scalar<Boolean>>(
                new FuncOf<>(list::add, () -> true),
                new IterableOf<>("hello", "world")
            )
        ),
        new ScalarHasValue<>(true)
    ).affirm();
    new Assertion<>(
        "Iterable must contain elements in any order",
        list,
        new IsIterableContainingInAnyOrder<String>(
            new ListOf<Matcher<? super String>>(
                new MatcherOf<>(
                    text -> {
                        return "hello".equals(text);
                    }
                ),
                new MatcherOf<>(
                    text -> {
                        return "world".equals(text);
                    }
                )
            )
        )
    ).affirm();
}
 
Example #21
Source File: And.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param subject The subject
 * @param conditions Funcs to map
 * @param <X> Type of items in the iterable
 * @since 0.34
 */
@SafeVarargs
public <X> And(final X subject, final Func<X, Boolean>... conditions) {
    this(
        new Mapped<>(
            item -> (Scalar<Boolean>) () -> item.apply(subject),
            new IterableOf<>(conditions)
        )
    );
}
 
Example #22
Source File: And.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param func Func to map
 * @param src The iterable
 * @param <X> Type of items in the iterable
 * @since 0.24
 */
public <X> And(final Func<X, Boolean> func, final Iterable<X> src) {
    this(
        new Mapped<>(
            item -> (Scalar<Boolean>) () -> func.apply(item), src
        )
    );
}
 
Example #23
Source File: Reduced.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param reduce Reducing function
 * @param values Values to be wrapped as scalars
 */
@SafeVarargs
public Reduced(
    final BiFunc<T, T, T> reduce,
    final T... values
) {
    this(reduce, new Mapped<>(Constant::new, values));
}
 
Example #24
Source File: CpObject.java    From eo with MIT License 5 votes vote down vote up
/**
 * Java code for object copying.
 *
 * @return Java code
 */
public String java() {
    return new UncheckedText(
        new FormattedText(
            "new %s(%s)",
            this.name,
            new UncheckedText(
                new JoinedText(
                    ", ",
                    new Mapped<>(Argument::java, this.args)
                )
            ).asString()
        )
    ).asString();
}
 
Example #25
Source File: FallbackFrom.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Calculate level of support of the given exception type.
 * @param target Exception type
 * @return Level of support: greater or equals to 0 if the target
 *  is supported and {@link Integer#MIN_VALUE} otherwise
 * @see InheritanceLevel
 */
public Integer support(final Class<? extends Throwable> target) {
    return new MinOf(
        new Mapped<>(
            supported -> new InheritanceLevel(target, supported).value(),
            this.exceptions
        )
    ).intValue();
}
 
Example #26
Source File: HighestOf.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param items The comparable items
 */
@SafeVarargs
public HighestOf(final T... items) {
    this(
        new Mapped<>(
            item -> () -> item,
            items
        )
    );
}
 
Example #27
Source File: AndInThreads.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param func Func to map
 * @param src The iterable
 * @param <X> Type of items in the iterable
 */
public <X> AndInThreads(final Func<X, Boolean> func,
    final Iterable<X> src) {
    this(
        new Mapped<>(
            item -> (Scalar<Boolean>) () -> func.apply(item), src
        )
    );
}
 
Example #28
Source File: AndInThreads.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param svc Executable service to run thread in
 * @param func Func to map
 * @param src The iterable
 * @param <X> Type of items in the iterable
 */
public <X> AndInThreads(final ExecutorService svc,
    final Func<X, Boolean> func, final Iterable<X> src) {
    this(
        svc,
        new Mapped<>(
            item -> (Scalar<Boolean>) () -> func.apply(item), src
        )
    );
}
 
Example #29
Source File: FormParams.java    From verano-http with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param params Parameters
 */
public FormParams(final Iterable<FormParam> params) {
    super(
        new DictOf(
            new Mapped<>(
                input -> (DictInput) input,
                params
            )
        )
    );
}
 
Example #30
Source File: Joined.java    From cactoos with MIT License 5 votes vote down vote up
@Override
public InputStream stream() throws Exception {
    return new Reduced<InputStream>(
        (left, right) -> new SequenceInputStream(left, right),
        new Mapped<>(
            input -> () -> input.stream(),
            this.inputs
        )
    ).value();
}