⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 finding.java

📁 java 的源代码
💻 JAVA
字号:
package com.reddragon2046.base.utilities.data.algorithms;

import com.reddragon2046.base.utilities.data.*;
import com.reddragon2046.base.utilities.data.util.IteratorFactory;
import java.util.Collection;
import java.util.Iterator;

// Referenced classes of package com.reddragon2046.base.utilities.data.algorithms:
//            Predicates

public final class Finding
{

    private Finding()
    {
    }

    public static InputIterator find(InputIterator first, InputIterator last, Object object)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        InputIterator firstx;
        for(firstx = (InputIterator)first.clone(); !firstx.equals(last); firstx.advance())
        {
            Object one = firstx.get();
            if(one != null ? one.equals(object) : object == null)
                break;
        }

        return firstx;
    }

    public static InputIterator find(Collection collection, Object object)
    {
        return find(IteratorFactory.start(collection), IteratorFactory.finish(collection), object);
    }

    public static InputIterator findIf(InputIterator first, InputIterator last, UnaryPredicate predicate)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        InputIterator firstx;
        for(firstx = (InputIterator)first.clone(); !firstx.equals(last) && !predicate.execute(firstx.get()); firstx.advance());
        return firstx;
    }

    public static InputIterator findIf(Collection collection, UnaryPredicate predicate)
    {
        return findIf(IteratorFactory.start(collection), IteratorFactory.finish(collection), predicate);
    }

    public static InputIterator adjacentFind(InputIterator first, InputIterator last)
    {
        return adjacentFind(first, last, ((BinaryPredicate) (new Predicates.EqualTo())));
    }

    public static InputIterator adjacentFind(Collection collection)
    {
        return adjacentFind(IteratorFactory.start(collection), IteratorFactory.finish(collection));
    }

    public static InputIterator adjacentFind(Series series, BinaryPredicate predicate)
    {
        return adjacentFind(series.startInput(), series.finishInput(), predicate);
    }

    static InputIterator adjacentFind(InputIterator first, InputIterator last, BinaryPredicate predicate)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        InputIterator firstx = (InputIterator)first.clone();
        if(firstx.equals(last))
            return last;
        InputIterator next = (InputIterator)first.clone();
        next.advance();
        for(; !next.equals(last); next.advance())
        {
            if(predicate.execute(firstx.get(), next.get()))
                return firstx;
            firstx.advance();
        }

        return next;
    }

    public static InputIterator adjacentFind(Collection collection, BinaryPredicate predicate)
    {
        return adjacentFind(IteratorFactory.start(collection), IteratorFactory.finish(collection), predicate);
    }

    public static Object detect(InputIterator first, InputIterator last, UnaryPredicate predicate)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        for(InputIterator firstx = (InputIterator)first.clone(); !firstx.equals(last);)
        {
            Object object = firstx.next();
            if(predicate.execute(object))
                return object;
        }

        return null;
    }

    public static Object detect(Collection collection, UnaryPredicate predicate)
    {
        return detect(IteratorFactory.start(collection), IteratorFactory.finish(collection), predicate);
    }

    public static boolean some(InputIterator first, InputIterator last, UnaryPredicate predicate)
    {
        return detect(first, last, predicate) != null;
    }

    public static boolean some(Collection collection, UnaryPredicate predicate)
    {
        return some(IteratorFactory.start(collection), IteratorFactory.finish(collection), predicate);
    }

    public static boolean every(InputIterator first, InputIterator last, UnaryPredicate predicate)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        for(InputIterator firstx = (InputIterator)first.clone(); !firstx.equals(last);)
            if(!predicate.execute(firstx.next()))
                return false;

        return true;
    }

    public static boolean every(Collection collection, UnaryPredicate predicate)
    {
        return every(IteratorFactory.start(collection), IteratorFactory.finish(collection), predicate);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -