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

📄 replacing.java

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

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

public final class Replacing
{

    private Replacing()
    {
    }

    private static int replace(ForwardIterator first, ForwardIterator last, Object oldValue, Object newValue)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        ForwardIterator firstx = (ForwardIterator)first.clone();
        int n = 0;
        for(; !firstx.equals(last); firstx.advance())
        {
            Object one = firstx.get();
            if(one != null ? one.equals(oldValue) : oldValue == null)
            {
                firstx.put(newValue);
                n++;
            }
        }

        return n;
    }

    public static int replace(List list, Object oldValue, Object newValue)
    {
        return replace(IteratorFactory.startForward(list), IteratorFactory.finishForward(list), oldValue, newValue);
    }

    private static int replaceIf(ForwardIterator first, ForwardIterator last, UnaryPredicate predicate, Object newValue)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        ForwardIterator firstx = (ForwardIterator)first.clone();
        int n = 0;
        for(; !firstx.equals(last); firstx.advance())
            if(predicate.execute(firstx.get()))
            {
                firstx.put(newValue);
                n++;
            }

        return n;
    }

    public static int replaceIf(List list, UnaryPredicate predicate, Object newValue)
    {
        return replaceIf(((ForwardIterator) (IteratorFactory.start(list))), ((ForwardIterator) (IteratorFactory.finish(list))), predicate, newValue);
    }

    private static OutputIterator replaceCopy(InputIterator first, InputIterator last, OutputIterator result, Object oldValue, Object newValue)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        InputIterator firstx = (InputIterator)first.clone();
        OutputIterator resultx = (OutputIterator)result.clone();
        for(; !firstx.equals(last); firstx.advance())
        {
            Object one = firstx.get();
            if(one != null ? one.equals(oldValue) : oldValue == null)
                resultx.put(newValue);
            else
                resultx.put(one);
            resultx.advance();
        }

        return resultx;
    }

    public static OutputIterator replaceCopy(Collection input, OutputIterator result, Object oldValue, Object newValue)
    {
        return replaceCopy(IteratorFactory.start(input), IteratorFactory.finish(input), result, oldValue, newValue);
    }

    public static void replaceCopy(Collection source, Collection destination, Object oldValue, Object newValue)
    {
        replaceCopy(IteratorFactory.start(source), IteratorFactory.finish(source), ((OutputIterator) (new InsertIterator(destination))), oldValue, newValue);
    }

    private static OutputIterator replaceCopyIf(InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate predicate, Object newValue)
    {
        if(!first.isCompatibleWith(last))
            throw new IllegalArgumentException("iterators not compatible");
        InputIterator firstx = (InputIterator)first.clone();
        OutputIterator resultx = (OutputIterator)result.clone();
        for(; !firstx.equals(last); firstx.advance())
        {
            resultx.put(predicate.execute(firstx.get()) ? newValue : firstx.get());
            resultx.advance();
        }

        return resultx;
    }

    private static OutputIterator replaceCopyIf(Collection input, OutputIterator result, UnaryPredicate predicate, Object newValue)
    {
        return replaceCopyIf(IteratorFactory.start(input), IteratorFactory.finish(input), result, predicate, newValue);
    }

    public static void replaceCopyIf(Collection source, Collection destination, UnaryPredicate predicate, Object newValue)
    {
        replaceCopyIf(IteratorFactory.start(source), IteratorFactory.finish(source), ((OutputIterator) (new InsertIterator(destination))), predicate, newValue);
    }
}

⌨️ 快捷键说明

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