实现简化 Stream API 的框架,但使用 Builder

Realize a frame for a simplified Stream API but use Builder

有一个任务需要完成class。我不太明白这里怎么使用Builder模式?(如果这些是class的字段,好吧,明白了,但是用方法它不起作用,我不明白(有经验的人可以你帮我吧) 有一些条件:

  1. 实现简化流的框架API。
  2. BestStream 仅适用于整数类型。
  3. 并且需要使用模式生成器。
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
    
public class BestStream {
    public static BestStream of(List<Integer> source) {
        throw new UnsupportedOperationException();
    }
    
    public BestStream map(Function<Integer, Integer> fun) {
        throw new UnsupportedOperationException();
    }
    
    public BestStream filter(Predicate<Integer> fun) {
        throw new UnsupportedOperationException();
    }
    
    public List<Integer> collect() {
        throw new UnsupportedOperationException();
    }
}

我认为您的案例中的字段是“函数式函数”(Function 和 Predicate),您会注意到它们作为参数传递给您的方法。

在此视图下,您可以应用构建器,您可以在其中“构建”按以下顺序运行的流:

  1. 通过映射字段​​映射数字
  2. 通过过滤字段过滤它们
  3. 构建集合作为结果

一个例子:

private static class BestStream {
    private final Stream<Integer> source;
    private Function<Integer, Integer> map;
    private Predicate<Integer> filter;

    private BestStream(Stream<Integer> source) {
        this.source = source;
    }

    public static BestStream of(List<Integer> source) {
        return new BestStream(source.stream());
    }

    public static void main(String[] args) {
        final List<Integer> collect =
                BestStream.of(Arrays.asList(1, 2, 3, 4, 2, 2, 9))
                        .filter(i -> Objects.equals(i, 2))
                        .map(i -> i * 2)
                        .collect();
    }

    public BestStream map(Function<Integer, Integer> fun) {
        this.map = fun;
        return this;
    }

    public BestStream filter(Predicate<Integer> fun) {
        this.filter = fun;
        return this;
    }

    public List<Integer> collect() {
        return source.map(map).filter(filter).collect(Collectors.toList());
    }
}

我认为解决这个问题最简单的方法是将其分解成多个 类。

首先,您API的界面用户请看:

package example;

import java.util.List;
import java.util.function.IntPredicate;
import java.util.function.IntUnaryOperator;

public interface BestStream {

    static BestStream of(List<Integer> list) {
        return new BestStreamListSource(list);
    }

    BestStream filter(IntPredicate predicate);
    BestStream map(IntUnaryOperator operator);
    List<Integer> collect();
}

其次是用于内部目的的接口:

package example;

public interface BestStreamSource extends BestStream {

    Integer next();
}

现在是内部 BestStreamSource 接口的基本实现,稍后将由实际实现使用:

package example;

import java.util.ArrayList;
import java.util.List;
import java.util.function.IntPredicate;
import java.util.function.IntUnaryOperator;

public abstract class BaseBestStreamSource implements BestStreamSource {

    @Override
    public BestStream filter(IntPredicate predicate) {
        return new BestStreamFilteredSource(this, predicate);
    }

    @Override
    public BestStream map(IntUnaryOperator operator) {
        return new BestStreamMappingSource(this, operator);
    }

    @Override
    public List<Integer> collect() {
        final List<Integer> result = new ArrayList<>();
        Integer value = null;

        while ((value = next()) != null) {
            result.add(value);
        }

        return result;
    }
}

现在您有 3 个 类 可以实际完成工作:

  • 一个来源,从列表中读取元素
  • 一个基于任何来源进行过滤的
  • 基于任何源进行映射的一个
package example;

import java.util.List;
import java.util.Objects;

public class BestStreamListSource extends BaseBestStreamSource {

    private final List<Integer> list;
    private int index;

    public BestStreamListSource(List<Integer> list) {
        this.list = Objects.requireNonNull(list);
        this.index = 0;
    }

    @Override
    public Integer next() {
        if (this.index < this.list.size()) {
            return this.list.get(this.index++);
        } else {
            return null;
        }
    }
}
package example;

import java.util.Objects;
import java.util.function.IntPredicate;

public class BestStreamFilteredSource extends BaseBestStreamSource {

    private final BestStreamSource parent;
    private final IntPredicate predicate;

    public BestStreamFilteredSource(BestStreamSource parent, IntPredicate predicate) {
        this.parent = Objects.requireNonNull(parent);
        this.predicate = Objects.requireNonNull(predicate);
    }

    @Override
    public Integer next() {
        final int[] holder = new int[1];
        Integer value = null;
        boolean foundMatch = false;

        while (!foundMatch && (value = this.parent.next()) != null) {
            foundMatch = this.predicate.test(value);
        }

        if (foundMatch) {
            return value;
        } else {
            return null;
        }
    }
}

package example;

import java.util.Objects;
import java.util.function.IntUnaryOperator;

public class BestStreamMappingSource extends BaseBestStreamSource {

    private final BestStreamSource parent;
    private final IntUnaryOperator mapping;

    public BestStreamMappingSource(BestStreamSource parent, IntUnaryOperator mapping) {
        this.parent = Objects.requireNonNull(parent);
        this.mapping = Objects.requireNonNull(mapping);
    }

    @Override
    public Integer next() {
        Integer value = this.parent.next();
        if (value != null) {
            value = this.mapping.applyAsInt(value);
        }

        return value;
    }
}

有了它,您可以链接任何您想做的过滤器和映射