Example usage for java.util Optional of

List of usage examples for java.util Optional of

Introduction

In this page you can find the example usage for java.util Optional of.

Prototype

public static <T> Optional<T> of(T value) 

Source Link

Document

Returns an Optional describing the given non- null value.

Usage

From source file:Main.java

/**
 * @return the last element in the list, or empty if the list is empty
 *//*from  w w  w  .  j a  v a  2s . co m*/
public static <E> Optional<E> last(List<E> list) {
    if (list.isEmpty())
        return Optional.empty();
    return Optional.of(list.get(list.size() - 1));
}

From source file:Main.java

static <E> Optional<E> max(List<E> elements, Comparator<E> comparator) {
    //TODO Implement me
    E maxValue = elements.get(0);/*from   w w  w.  j a v a  2s.  com*/
    int i = 0;
    while (i <= elements.size()) {
        maxValue = comparator.compare(maxValue, elements.get(i)) > 0 ? maxValue : elements.get(i);
        i++;
    }
    return Optional.of(maxValue);
}

From source file:Main.java

public static <E> Optional<E> min(List<E> elements, Comparator<E> comparator) {
    if (elements.size() != 0) {
        E minElement = elements.get(0);//from w  w  w .j av  a2 s  . c o m
        for (int i = 1; i < elements.size(); i++) {
            minElement = comparator.compare(minElement, elements.get(i)) < 0 ? minElement : elements.get(i);
        }
        return Optional.of(minElement);
    } else
        return Optional.empty();
}

From source file:Main.java

public static <E> Optional<E> max(List<E> elements, Comparator<E> comparator) {
    if (elements.size() != 0) {
        E maxElement = elements.get(0);//from   w  ww  .ja  v a2  s. c  o m
        for (int i = 1; i < elements.size(); i++) {
            maxElement = comparator.compare(maxElement, elements.get(i)) > 0 ? maxElement : elements.get(i);
        }
        return Optional.of(maxElement);
    } else
        return Optional.empty();
}

From source file:Main.java

public static Optional<String> getTextOfElement(List<XMLEvent> events, String elementName) {
    Optional<Characters> characters = getCharacterEventOfElement(events, elementName);

    if (characters.isPresent())
        return Optional.of(characters.get().getData());

    return Optional.empty();
}

From source file:Main.java

public static Optional<XMLEvent> getStartElement(List<XMLEvent> events, String elementName) {
    Iterator<XMLEvent> iterator = events.iterator();

    while (iterator.hasNext()) {
        XMLEvent event = iterator.next();

        if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(elementName))
            return Optional.of(event);

    }/*  ww w . j a v a 2 s  .com*/

    return Optional.empty();
}

From source file:Main.java

@SuppressWarnings("empty-statement")
public static Optional<String> nextTag(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && reader.next() != XMLStreamReader.START_ELEMENT)
        ;/*from  ww w . ja  v a2  s  . c  o  m*/
    if (reader.getEventType() == XMLStreamReader.END_DOCUMENT) {
        return Optional.empty();
    } else {
        return Optional.of(reader.getName().getLocalPart());
    }
}

From source file:Main.java

public static Optional<Characters> getCharacterEventOfElement(List<XMLEvent> events, String elementName) {
    Iterator<XMLEvent> iterator = events.iterator();

    while (iterator.hasNext()) {
        XMLEvent event = iterator.next();

        if (event.isStartElement() && event.asStartElement().getName().getLocalPart().equals(elementName)) {
            XMLEvent nextEvent = iterator.next();
            if (nextEvent.isCharacters())
                return Optional.of(nextEvent.asCharacters());
        }//from ww w  .j ava2s .  co m

    }

    return Optional.empty();
}

From source file:Main.java

public static <T> Optional<T> findAnyIn(Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return Optional.of(iterator.next());
    }//from   w ww  . ja v  a  2 s .co m
    return Optional.empty();
}

From source file:Main.java

/**
 * Returns item in HashSet that matches the specified Predicate.
 * @param set the HashSet to check against
 * @param condition the Predicate to check
 * @param <T> the type of objects in the HashSet
 * @return an Optional containing the matching object, Empty if not found
 * @throws IllegalArgumentException if HashSet or Predicate is null
 *///  w w w .  j  a va 2s .  com
public static <T> Optional<T> getItemInHashSet(final HashSet<T> set, final Predicate<T> condition) {
    if (set == null)
        throw new IllegalArgumentException("HashSet cannot be null!");
    if (condition == null)
        throw new IllegalArgumentException("Predicate cannot be null!");
    for (final T object : set)
        if (condition.test(object))
            return Optional.of(object);
    return Optional.empty();
}