All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mindmaps.graql.internal.util.CommonUtil Maven / Gradle / Ivy

/*
 * MindmapsDB - A Distributed Semantic Database
 * Copyright (C) 2016  Mindmaps Research Ltd
 *
 * MindmapsDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MindmapsDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MindmapsDB. If not, see .
 */

package io.mindmaps.graql.internal.util;

import com.google.common.collect.ImmutableSet;

import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Stream;

public class CommonUtil {

    private CommonUtil() {}

    /**
     * @param optional the optional to change into a stream
     * @param  the type in the optional
     * @return a stream of one item if the optional has an element, else an empty stream
     */
    public static  Stream optionalToStream(Optional optional) {
        return optional.map(Stream::of).orElseGet(Stream::empty);
    }

    public static  Optional tryNext(Iterator iterator) {
        if (iterator.hasNext()) {
            return Optional.of(iterator.next());
        } else {
            return Optional.empty();
        }
    }

    public static  Optional tryAny(Iterable iterable) {
        return tryNext(iterable.iterator());
    }

    public static  Optional optionalOr(Optional first, Optional second) {
        if (first.isPresent()) {
            return first;
        } else {
            return second;
        }
    }

    public static  Collector, ImmutableSet> toImmutableSet() {
        return new Collector, ImmutableSet>() {
            @Override
            public Supplier> supplier() {
                return ImmutableSet::builder;
            }

            @Override
            public BiConsumer, T> accumulator() {
                return ImmutableSet.Builder::add;
            }

            @Override
            public BinaryOperator> combiner() {
                return (b1, b2) -> b1.addAll(b2.build());
            }

            @Override
            public Function, ImmutableSet> finisher() {
                return ImmutableSet.Builder::build;
            }

            @Override
            public Set characteristics() {
                return ImmutableSet.of();
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy