微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决实现简化的Stream API的框架,但使用Builder

有一项任务需要完成课程。我不太了解如何在此处使用Builder模式?(如果这些是类的字段,请理解,但是如果使用的方法不起作用,我就不会理解(有经验的人可以帮助我吗? ) 有一些条件:

  1. 为简化的Stream API实现框架。
  2. BestStream仅适用于Integer类型。
  3. 并且需要使用模式生成器。
  • 方法 -初始数据。
  • 方法 map -将一个数字转换为另一个数字(元素* 2)。
  • 方法 filter -过滤元素流(元素== 2)。
  • 方法 collect -根据指定的地图和过滤条件从源中收集所有元素。
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();
    }
}

解决方法

我认为您所使用的字段是“函数功能”(函数和谓词),因为您会注意到它们作为参数传递给您的方法。

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

  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,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;
    }
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。