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

The following examples show how to use io.vavr.control.Option#none() . 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: BeanAnnotationTest.java    From vavr-jackson with Apache License 2.0 6 votes vote down vote up
public BeanObjectOptional(boolean empty) {
    if (empty) {
        charSeq = CharSeq.empty();
        either = Either.left(EITHER_VALUE);
        option = Option.none();
        map = HashMap.empty();
        multimap = HashMultimap.withSeq().empty();
        seq = List.empty();
        set = HashSet.empty();
    } else {
        charSeq = CharSeq.of(CHARSEQ_VALUE);
        either = Either.right(EITHER_VALUE);
        option = Option.of(OPTION_VALUE);
        map = HashMap.of("key", MAP_VALUE);
        multimap = HashMultimap.withSeq().of("key", MULTIMAP_VALUE);
        seq = List.of(SEQ_VALUE);
        set = HashSet.of(SET_VALUE);
    }
}
 
Example 2
Source File: OptionTest.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
@Test
void test2() throws IOException {
    Option<?> src = Option.none();
    String json = mapper(optSettings).writer().writeValueAsString(src);
    Assertions.assertEquals("[\"undefined\"]", json);
    Option<?> restored = mapper(optSettings).readValue(json, Option.class);
    Assertions.assertEquals(src, restored);
    Option<?> plain = mapper(optSettings).readValue("null", Option.class);
    Assertions.assertEquals(src, plain);
}
 
Example 3
Source File: CatalogueDatabase.java    From library with MIT License 5 votes vote down vote up
Option<Book> findBy(ISBN isbn) {
    try {
        return Option.of(
                jdbcTemplate.queryForObject(
                        "SELECT b.* FROM catalogue_book b WHERE b.isbn = ?",
                        new BeanPropertyRowMapper<>(BookDatabaseRow.class),
                        isbn.getIsbn())
                        .toBook());
    } catch (EmptyResultDataAccessException e) {
        return Option.none();

    }
}
 
Example 4
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 5
Source File: JavaStreams.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Returns Option.none() if the stream is empty, or an Option.of(stream) if there are elements in the stream.
 */
public static <T> Option<Stream<T>> toOption(Stream<T> stream) {
    Iterator<T> iterator = stream.iterator();
    if (iterator.hasNext()) {
        return Option.of(StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false));
    } else {
        return Option.none();
    }
}
 
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: PandaModules.java    From panda with Apache License 2.0 5 votes vote down vote up
protected Option<Module> fetch(String moduleQualifier, boolean compute) {
    String[] names = moduleQualifier.split(":");
    Modules modules = this;
    Module module = null;

    for (String name : names) {
        if (StringUtils.isEmpty(name)) {
            throw new PandaFrameworkException("Illegal name " + moduleQualifier);
        }

        Module nextModule = modules.get(name).getOrNull();

        if (nextModule == null && compute) {
            nextModule = new PandaModule(module, name);
            modules.include(nextModule);
        }

        if (nextModule == null) {
            return Option.none();
        }

        module = nextModule;
        modules = module;
    }

    return Option.of(module);
}
 
Example 8
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 9
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag and with any name, getting values using [g1] for writing.
 */
public static <T> TagWriteProtocol<T> writeTagName(Function1<T,QName> g1) {
    return new TagWriteProtocol<>(Option.none(), Vector.empty(), Vector.of(g1));
}
 
Example 10
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag with any name and inner protocols using [p*], getting values using [g*] for writing.
 */
public static <F2,F3,T> TagWriteProtocol<T> tagName(Function1<T,QName> g1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2, Function1<T,F3> g3, WriteProtocol<XMLEvent,F3> p3) {
    return new TagWriteProtocol<>(Option.none(), Vector.of(p2, p3), Vector.of(g1, g2, g3));
}
 
Example 11
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag with any name and inner protocols using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F2,F3,T> TagProtocol<T> tagName(Protocol<XMLEvent,F2> p2, Protocol<XMLEvent,F3> p3, Function3<QName,F2,F3,T> f, Function1<T,QName> g1, Function1<T,F2> g2, Function1<T,F3> g3) {
    return new TagProtocol<>(Option.none(), Vector.of(p2, p3), args -> f.apply((QName) args.get(0), (F2) args.get(1), (F3) args.get(2)), Vector.of(g1, g2, g3));
}
 
Example 12
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag with any name and inner protocols using [p*], getting values using [g*] for writing.
 */
public static <F2,T> TagWriteProtocol<T> tagName(Function1<T,QName> g1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2) {
    return new TagWriteProtocol<>(Option.none(), Vector.of(p2), Vector.of(g1, g2));
}
 
Example 13
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads a tag with any name and inner protocols using [p*], using [f] to create the result on reading.
 */
public static <F2,T> TagReadProtocol<T> tagName(ReadProtocol<XMLEvent,F2> p2, Function2<QName,F2,T> f) {
    return new TagReadProtocol<>(Option.none(), Vector.of(p2), args -> f.apply((QName) args.get(0), (F2) args.get(1)));
}
 
Example 14
Source File: PlaceOnHoldCommand.java    From library with MIT License 4 votes vote down vote up
static PlaceOnHoldCommand openEnded(PatronId patronId, LibraryBranchId libraryBranchId, BookId bookId) {
    return new PlaceOnHoldCommand(Instant.now(), patronId, libraryBranchId, bookId, Option.none());
}
 
Example 15
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads a tag with any name, creating its result using [f].
 */
public static <T> TagReadProtocol<T> readTagName(Function1<QName,T> f) {
    return new TagReadProtocol<>(Option.none(), Vector.empty(), args -> f.apply((QName) args.get(0)));
}
 
Example 16
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag with any name, using [f] to create the result on reading, getting values using [g1] for writing.
 */
public static <T> TagProtocol<T> tagName(Function1<QName,T> f, Function1<T,QName> g1) {
    return new TagProtocol<>(Option.none(), Vector.empty(), args -> f.apply((QName) args.get(0)), Vector.of(g1));
}
 
Example 17
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Accepts any single tag when reading, routing its body through to the given inner protocol.
 */
public static <T> ReadProtocol<XMLEvent, T> anyTag(ReadProtocol<XMLEvent, T> inner) {
    return new TagReadProtocol<>(Option.none(), inner);
}
 
Example 18
Source File: Jackson.java    From ts-reaktive with MIT License 4 votes vote down vote up
public <T> Stream<T> parse(JsonParser input, Reader<JSONEvent, T> reader) {
    reader.reset();
    
    Iterator<T> iterator = new Iterator<T>() {
        private Option<T> next = parse();
        
        private Option<T> parse() {
            for (Option<JSONEvent> evt = nextEvent(); evt.isDefined(); evt = nextEvent()) {
                Try<T> read = reader.apply(evt.get());
                if (read.isSuccess()) {
                    return read.toOption();
                } else if (read.isFailure() && !ReadProtocol.isNone(read)) {
                    throw (RuntimeException) read.failed().get();
                }
            }
            return Option.none();
        }
        
        private Option<JSONEvent> nextEvent() {
            try {
                if (input.nextToken() != null) {
                    return some(getEvent(input));
                } else {
                    return none(); // end of stream
                }
            } catch (IOException e) {
                throw new IllegalArgumentException(e);
            }
        }
        
        @Override
        public boolean hasNext() {
            return next.isDefined();
        }

        @Override
        public T next() {
            T elmt = next.get();
            next = parse();
            return elmt;
        }
    };
    
    return StreamSupport.stream(
        Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED),
        false);
}
 
Example 19
Source File: OptionDeserializer.java    From vavr-jackson with Apache License 2.0 4 votes vote down vote up
@Override
public Option<?> getNullValue(DeserializationContext ctxt) throws JsonMappingException {
    return Option.none();
}
 
Example 20
Source File: PatronEvent.java    From library with MIT License 4 votes vote down vote up
public static BookPlacedOnHoldEvents events(BookPlacedOnHold bookPlacedOnHold) {
    return new BookPlacedOnHoldEvents(bookPlacedOnHold.getPatronId(), bookPlacedOnHold, Option.none());
}