org.cactoos.map.MapEntry Java Examples

The following examples show how to use org.cactoos.map.MapEntry. 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: PropertiesOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void convertsMapToProperties() {
    new Assertion<>(
        "Must convert map to properties",
        new PropertiesOf(
            new Sticky<>(
                new MapOf<Integer, String>(
                    new MapEntry<>(0, "hello, world"),
                    new MapEntry<>(1, "how are you?")
                )
            )
        ),
        new ScalarHasValue<>(
            new MatcherOf<Properties>(
                props -> {
                    return props.getProperty("0").endsWith(", world");
                }
            )
        )
    ).affirm();
}
 
Example #2
Source File: PropertiesOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void sensesChangesInMap() throws Exception {
    final AtomicInteger size = new AtomicInteger(2);
    final PropertiesOf props = new PropertiesOf(
        new MapOf<>(
            () -> new Repeated<>(
                size.incrementAndGet(), () -> new MapEntry<>(
                    new SecureRandom().nextInt(),
                    1
                )
            )
        )
    );
    new Assertion<>(
        "Must sense the changes in the underlying map",
        props.value().size(),
        new IsNot<>(new IsEqual<>(props.value().size()))
    ).affirm();
}
 
Example #3
Source File: Skeleton.java    From jpeek with MIT License 6 votes vote down vote up
/**
 * Calculate Xembly for a single .class file.
 * @param ctc The class
 * @return Metrics
 */
private static Map.Entry<String, Directives> xembly(final CtClass ctc) {
    ctc.defrost();
    String pkg = ctc.getPackageName();
    if (pkg == null) {
        pkg = "";
    }
    return new MapEntry<>(
        pkg,
        new Directives()
            .add("class")
            .comment(
                Xembler.escape(
                    String.format(
                        "Package: %s; name: %s; file: %s",
                        ctc.getPackageName(),
                        ctc.getName(),
                        ctc.getClassFile().getName()
                    )
                )
            )
            .attr("id", ctc.getSimpleName())
            .append(new XmlClass(ctc))
            .up()
    );
}
 
Example #4
Source File: App.java    From jpeek with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param source Source directory
 * @param target Target dir
 */
public App(final Path source, final Path target) {
    this(
        source, target,
        new MapOf<String, Object>(
            new MapEntry<>("LCOM", true),
            new MapEntry<>("LCOM2", true),
            new MapEntry<>("LCOM3", true),
            new MapEntry<>("LCOM4", true),
            new MapEntry<>("LCOM5", true),
            new MapEntry<>("SCOM", true),
            new MapEntry<>("NHD", true),
            new MapEntry<>("MMAC", true),
            new MapEntry<>("OCC", true),
            new MapEntry<>("PCC", true),
            new MapEntry<>("TCC", true),
            new MapEntry<>("LCC", true),
            new MapEntry<>("CCM", true),
            new MapEntry<>("MWE", true)
        )
    );
}
 
Example #5
Source File: HashDict.java    From verano-http with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param kvps Key-value pairs
 */
public HashDict(final Iterable<Kvp> kvps) {
    this(
        kvps,
        new MapOf<>(
            new Mapped<>(
                input -> new MapEntry<>(input.key(), input.value()),
                kvps
            )
        )
    );
}
 
Example #6
Source File: HtCookies.java    From cactoos-http with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param rsp Response
 */
public HtCookies(final Input rsp) {
    super(() -> new Grouped<>(
        new Mapped<>(
            (Text entry) -> {
                final Iterable<Text> parts = new Split(entry, "=");
                if (new LengthOf(parts).intValue() != 2) {
                    throw new IllegalArgumentException(
                        "Incorrect HTTP Response cookie"
                    );
                }
                final Iterator<Text> iter = parts.iterator();
                return new MapEntry<>(
                    iter.next().asString(),
                    iter.next().asString()
                );
            },
            new Joined<>(
                new Mapped<>(
                    e -> new Split(e, ";\\s+"),
                    new HtHeaders(rsp).get("set-cookie")
                )
            )
        ),
        MapEntry::getKey,
        MapEntry::getValue
    ));
}
 
Example #7
Source File: StickyBiFunc.java    From cactoos with MIT License 5 votes vote down vote up
@Override
public Z apply(final X first, final Y second) throws Exception {
    final Map.Entry<X, Y> key = new MapEntry<>(first, second);
    while (this.cache.size() > this.size) {
        this.cache.remove(this.cache.keySet().iterator().next());
    }
    if (!this.cache.containsKey(key)) {
        this.cache.put(key, this.func.apply(first, second));
    }
    return this.cache.get(key);
}
 
Example #8
Source File: PropertiesOf.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param entries The map with properties
 * @since 0.23
 */
public PropertiesOf(final Iterable<Map.Entry<?, ?>> entries) {
    this(
        new MapOf<>(
            new Mapped<Map.Entry<?, ?>, Map.Entry<String, String>>(
                input -> new MapEntry<>(
                    input.getKey().toString(), input.getValue().toString()
                ),
                entries
            )
        )
    );
}
 
Example #9
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 #10
Source File: PhonebookTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void addContact() throws Exception {
    try (
        Servers servers = new Servers(
            new ServerH2(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-h2.sql"
                        )
                    )
                )
            ),
            new ServerPgsql(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-pgsql.sql"
                        )
                    )
                )
            )
        )
    ) {
        for (final Session session : servers.sessions()) {
            final Phonebook phonebook = new PhonebookSql(session);
            final Contact contact = phonebook.contact(
                new MapOf<String, String>(
                    new MapEntry<>("name", "Donald Knuth")
                )
            );
            contact.phones().add(
                new MapOf<String, String>(
                    new MapEntry<>("number", "99991234"),
                    new MapEntry<>("carrier", "TIM")
                )
            );
            contact.phones().add(
                new MapOf<String, String>(
                    new MapEntry<>("number", "98812564"),
                    new MapEntry<>("carrier", "Oi")
                )
            );
            MatcherAssert.assertThat(
                XhtmlMatchers.xhtml(
                    contact.about()
                ),
                XhtmlMatchers.hasXPaths(
                    "/contact/name[text()='Donald Knuth']",
                    "/contact/phones/phone/number[text()='99991234']",
                    "/contact/phones/phone/carrier[text()='TIM']",
                    "/contact/phones/phone/number[text()='98812564']",
                    "/contact/phones/phone/carrier[text()='Oi']"
                )
            );
        }
    }
}
 
Example #11
Source File: PhonebookTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void renameContact() throws Exception {
    try (
        Servers servers = new Servers(
            new ServerH2(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-h2.sql"
                        )
                    )
                )
            ),
            new ServerPgsql(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-pgsql.sql"
                        )
                    )
                )
            )
        )
    ) {
        for (final Session session : servers.sessions()) {
            final Phonebook phonebook = new PhonebookSql(session);
            final Contact contact = phonebook.filter("maria").iterator()
                .next();
            contact.update(
                new MapOf<String, String>(
                    new MapEntry<>("name", "Maria Lima")
                )
            );
            MatcherAssert.assertThat(
                XhtmlMatchers.xhtml(
                    new PhonebookSql(session)
                        .filter("maria")
                        .iterator()
                        .next()
                        .about()
                ),
                XhtmlMatchers.hasXPaths(
                    "/contact/name[text()='Maria Lima']"
                )
            );
        }
    }
}
 
Example #12
Source File: StatementTransactionTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void commit() throws Exception {
    final Transacted transacted = new Transacted(
        new SessionNoAuth(
            new SourceH2("safedb")
        )
    );
    new ScriptSql(
        new ResourceOf(
            "com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql"
        )
    ).run(transacted);
    MatcherAssert.assertThat(
        "Can't perform a transaction commit",
        XhtmlMatchers.xhtml(
            new ResultAsValue<>(
                new StatementTransaction<>(
                    transacted,
                    () -> {
                        final Phonebook phonebook = new PhonebookSql(
                            transacted
                        );
                        final Contact contact = phonebook.contact(
                            new MapOf<String, String>(
                                new MapEntry<>("name", "Albert Einstein")
                            )
                        );
                        contact.phones().add(
                            new MapOf<String, String>(
                                new MapEntry<>("number", "99991234"),
                                new MapEntry<>("carrier", "TIM")
                            )
                        );
                        contact.phones().add(
                            new MapOf<String, String>(
                                new MapEntry<>("number", "98812564"),
                                new MapEntry<>("carrier", "Oi")
                            )
                        );
                        return contact.about();
                    }
                )
            ).value()
        ),
        XhtmlMatchers.hasXPaths(
            "/contact/name[text()='Albert Einstein']",
            "/contact/phones/phone/number[text()='99991234']",
            "/contact/phones/phone/carrier[text()='TIM']",
            "/contact/phones/phone/number[text()='98812564']",
            "/contact/phones/phone/carrier[text()='Oi']"
        )
    );
}
 
Example #13
Source File: StatementTransactionTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void rollback() throws Exception {
    final Transacted transacted = new Transacted(
        new SessionNoAuth(
            new SourceH2("unsafedb")
        )
    );
    new ScriptSql(
        new ResourceOf(
            "com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql"
        )
    ).run(transacted);
    final Phonebook phonebook = new PhonebookSql(transacted);
    final String name = "Frank Miller";
    try {
        new StatementTransaction<>(
            transacted,
            () -> {
                final Contact contact = phonebook.contact(
                    new MapOf<String, String>(
                        new MapEntry<>("name", name)
                    )
                );
                contact.phones().add(
                    new MapOf<String, String>(
                        new MapEntry<>("number", "99991234"),
                        new MapEntry<>("carrier", "TIM")
                    )
                );
                throw new IllegalStateException("Rollback");
            }
        ).result();
    } catch (final IllegalStateException ex) {
    }
    MatcherAssert.assertThat(
        "Can't perform a transaction rollback",
        StreamSupport.stream(
            phonebook.filter(name).spliterator(),
            false
        ).count(),
        Matchers.equalTo(0L)
    );
}
 
Example #14
Source File: ReportDataTest.java    From jpeek with MIT License 4 votes vote down vote up
private static Map<String, Object> args() {
    return new MapOf<String, Object>(
        new MapEntry<>("a", 1), new MapEntry<>("b", 2)
    );
}
 
Example #15
Source File: KvpOf.java    From verano-http with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param key Key
 * @param value Value
 */
public KvpOf(final String key, final String value) {
    this(new MapEntry<>(key, value));
}