Java Code Examples for io.vavr.control.Option#of()

The following examples show how to use io.vavr.control.Option#of() . 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: Issue141Test.java    From vavr-jackson with Apache License 2.0 6 votes vote down vote up
@Test
public void itShouldSerializeVavrOptionYearMonthAsStringWithoutJsonFormat() throws IOException {
    // Given an instance with io.vavr.control.Option
    MyVavrOptionalClassWithoutFormat obj = new MyVavrOptionalClassWithoutFormat();
    obj.operatingMonth = Option.of(YearMonth.of(2019, 12));

    // When serializing the instance using object mapper
    // with Java Time Module and VAVR Module
    ObjectMapper objectMapper = mapper();
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.registerModule(new VavrModule());
    String json = objectMapper.writeValueAsString(obj);

    // Then serialization is successful
    Assertions.assertEquals("{\"operatingMonth\":[2019,12]}", json);
    MyVavrOptionalClassWithoutFormat obj2 = objectMapper.readValue(json, MyVavrOptionalClassWithoutFormat.class);

    // And deserialization is successful
    Assertions.assertEquals(Option.of(YearMonth.of(2019, 12)), obj2.operatingMonth);
}
 
Example 2
Source File: Issue141Test.java    From vavr-jackson with Apache License 2.0 6 votes vote down vote up
@Test
public void itShouldSerializeVavrOptionYearMonthAsString() throws IOException {
    // Given an instance with io.vavr.control.Option
    MyVavrOptionClassWithFormat obj = new MyVavrOptionClassWithFormat();
    obj.operatingMonth = Option.of(YearMonth.of(2019, 12));

    // When serializing the instance using object mapper
    // with Java Time Module and VAVR Module
    ObjectMapper objectMapper = mapper();
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.registerModule(new VavrModule());
    String json = objectMapper.writeValueAsString(obj);

    // Then serialization is failed
    Assertions.assertEquals("{\"operatingMonth\":\"12-2019\"}", json);
    MyVavrOptionClassWithFormat obj2 = objectMapper.readValue(json, MyVavrOptionClassWithFormat.class);

    // And deserialization is failed
    Assertions.assertEquals(Option.of(YearMonth.of(2019, 12)), obj2.operatingMonth);
}
 
Example 3
Source File: TypeDeclarationUtils.java    From panda with Apache License 2.0 5 votes vote down vote up
public static Option<Snippet> readType(Snippet source) {
    Snippet type = new PandaSnippet();
    TokenInfo candidate = Objects.requireNonNull(source.get(0));

    if (candidate.getType() != TokenTypes.UNKNOWN) {
        return Option.none();
    }

    type.addToken(candidate);

    if (!source.hasElement(1)) {
        return Option.of(type);
    }

    candidate = Objects.requireNonNull(source.get(1));

    // read dimensions
    if (isArraySeparator(candidate)) {
        int firstIndex = 1;

        do {
            type.addToken(candidate);
            candidate = source.hasElement(++firstIndex) ? source.get(firstIndex) : null;
        } while (isArraySeparator(candidate));
    }

    return Option.of(type);
}
 
Example 4
Source File: BindingClassTest.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
@Test
void testOptionClass() throws Exception {
    OptionClass src = new OptionClass(Option.of(new ImplementedClass()));
    String json = MAPPER.writeValueAsString(src);
    OptionClass restored = MAPPER.readValue(json, OptionClass.class);
    Assertions.assertEquals(restored.value.get().getClass(), ImplementedClass.class);
}
 
Example 5
Source File: PandaModules.java    From panda with Apache License 2.0 5 votes vote down vote up
@Override
public Option<Module> get(String moduleQualifier) {
    if (!moduleQualifier.contains(":")) {
        return Option.of(modules.get(moduleQualifier));
    }

    return fetch(moduleQualifier, false);
}
 
Example 6
Source File: Regex.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Returns an Option containing the value of the capture groups, or Option.none() if the 
 * regular expression did not match.
 */
public Option<T> match(String source) {
    Matcher m = regex.matcher(source);
    if (m.matches()) {
        return Option.of(extract(m));
    } else {
        return Option.none();
    }        
}
 
Example 7
Source File: DefaultInjectorResources.java    From panda with Apache License 2.0 5 votes vote down vote up
DefaultInjectorResources(
    @Nullable InjectorResources parent,
    Map<Class<?>, InjectorResourceBind<Annotation>> resources,
    Map<HandlerRecord, InjectorResourceHandler<Annotation, Object, ?>> handlers,
    Map<Executable, Annotation[][]> cachedAnnotations
) {
    this.parent = Option.of(parent);
    this.binds = new HashMap<>(resources);
    this.handlers = new HashMap<>(handlers);
    this.cachedAnnotations = new HashMap<>(cachedAnnotations);
}
 
Example 8
Source File: OptionTest.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
@Test
void test1() throws IOException {
    Option<?> src = Option.of(1);
    String json = mapper(optSettings).writer().writeValueAsString(src);
    Assertions.assertEquals("[\"defined\",1]", json);
    Option<?> restored = mapper(optSettings).readValue(json, Option.class);
    Assertions.assertEquals(src, restored);
}
 
Example 9
Source File: PandaType.java    From panda with Apache License 2.0 5 votes vote down vote up
@Override
public Option<Type> getSuperclass() {
    for (Type base : getBases()) {
        if (TypeModels.isClass(base)) {
            return Option.of(base);
        }
    }

    return Option.none();
}
 
Example 10
Source File: PipelineComponent.java    From panda with Apache License 2.0 5 votes vote down vote up
/**
 * Get component with the given name
 *
 * @param name the name to search for
 * @return a found component
 */
@SuppressWarnings("unchecked")
public static Option<PipelineComponent<Parser>> get(String name) {
    for (Map.Entry<String, PipelineComponent<? extends Parser>> entry : COMPONENTS.entrySet()) {
        if (entry.getKey().equals(name)) {
            return Option.of((PipelineComponent<Parser>) entry.getValue());
        }
    }

    return Option.none();
}
 
Example 11
Source File: ParameterizedPojoTest.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
@Test
void testOptionOfTuple() throws Exception {
    String src00 = "A";
    String src01 = "B";
    Tuple2<String, String> src0 = Tuple.of(src00, src01);
    Option<Tuple2<String, String>> src = Option.of(src0);
    String json = MAPPER.writeValueAsString(new ParameterizedOptionPojo<>(src));
    Assertions.assertEquals(json, "{\"value\":[\"A\",\"B\"]}");
    ParameterizedOptionPojo<io.vavr.Tuple2<java.lang.String, java.lang.String>> restored =
            MAPPER.readValue(json, new TypeReference<ParameterizedOptionPojo<io.vavr.Tuple2<java.lang.String, java.lang.String>>>(){});
    Assertions.assertEquals(src, restored.getValue());
}
 
Example 12
Source File: SimplePojoTest.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
@Test
void testOptionOfString() throws Exception {
    String src0 = "A";
    Option<String> src = Option.of(src0);
    String json = MAPPER.writeValueAsString(new OptionOfString().setValue(src));
    Assertions.assertEquals(json, "{\"value\":\"A\"}");
    OptionOfString restored = MAPPER.readValue(json, OptionOfString.class);
    Assertions.assertEquals(src, restored.getValue());
}
 
Example 13
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads a tag and child elements (tag or attribute) using [p*], using [f] to create the result on reading.
 */
public static <F1,F2,F3,T> TagReadProtocol<T> tag(QName qname, ReadProtocol<XMLEvent,F1> p1, ReadProtocol<XMLEvent,F2> p2, ReadProtocol<XMLEvent,F3> p3, Function3<F1,F2,F3,T> f) {
    return new TagReadProtocol<>(Option.of(qname), Vector.of(p1, p2, p3), args -> f.apply((F1) args.get(0), (F2) args.get(1), (F3) args.get(2)));
}
 
Example 14
Source File: InstrumentationDescription.java    From kanela with Apache License 2.0 4 votes vote down vote up
public Builder withClassLoaderRefiner(Supplier<ClassLoaderRefiner> clazz) {
    classLoaderRefiner = Option.of(clazz.get());
    return this;
}
 
Example 15
Source File: PandaTypesMap.java    From panda with Apache License 2.0 4 votes vote down vote up
@Override
public Option<Type> forName(CharSequence typeName) {
    return Option.of(get(typeName.toString()));
}
 
Example 16
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag and child elements (tag or attribute) using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F1,F2,F3,T> TagProtocol<T> tag(QName qname, Protocol<XMLEvent,F1> p1, Protocol<XMLEvent,F2> p2, Protocol<XMLEvent,F3> p3, Function3<F1,F2,F3,T> f, Function1<T,F1> g1, Function1<T,F2> g2, Function1<T,F3> g3) {
    return new TagProtocol<>(Option.of(qname), Vector.of(p1, p2, p3), args -> f.apply((F1) args.get(0), (F2) args.get(1), (F3) args.get(2)), Vector.of(g1, g2, g3));
}
 
Example 17
Source File: PandaResources.java    From panda with Apache License 2.0 4 votes vote down vote up
@Override
public Option<MessengerInitializer> getMessengerInitializer() {
    return Option.of(builder.messengerInitializer);
}
 
Example 18
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads a tag and one child element (tag or attribute) using [p1], creating its result using [f].
 */
public static <F1,T> TagReadProtocol<T> tag(QName name, ReadProtocol<XMLEvent,F1> p1, Function1<F1,T> f) {
    return new TagReadProtocol<>(Option.of(name), Vector.of(p1), args -> f.apply((F1) args.get(0)));
}
 
Example 19
Source File: VavrUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenNonNull_whenCreatesOption_thenCorrect() {
    String name = "baeldung";
    Option<String> nameOption = Option.of(name);
    assertEquals("baeldung", nameOption.getOrElse("notbaeldung"));
}
 
Example 20
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag and child elements (tag or attribute) using [p*], getting values using [g*] for writing.
 */
public static <F1,F2,F3,F4,F5,F6,T> TagWriteProtocol<T> tag(QName qname, Function1<T,F1> g1, WriteProtocol<XMLEvent,F1> p1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2, Function1<T,F3> g3, WriteProtocol<XMLEvent,F3> p3, Function1<T,F4> g4, WriteProtocol<XMLEvent,F4> p4,   Function1<T,F5> g5, WriteProtocol<XMLEvent,F5> p5, Function1<T,F6> g6, WriteProtocol<XMLEvent,F6> p6) {
    return new TagWriteProtocol<>(Option.of(qname), Vector.of(p1, p2, p3, p4, p5, p6), Vector.of(g1, g2, g3, g4, g5, g6));
}