最近我一直在尝试将我的数据解析器重新实现为 java 中的流,但我不知道如何做一件特定的事情:
考虑带有时间戳的对象 A。考虑由各种 A 对象组成的对象 B 考虑一些指标,这些指标告诉我们对象 B 的时间范围。
我现在拥有的是一些带有状态的方法,它通过对象 A 的列表,如果它适合最后一个对象 B,它会去那里,否则它会创建新的 B 实例并开始将对象 A 放在那里。
我想以流的方式做到这一点
获取对象 A 的整个列表并将其作为流。现在我需要找出将创建“块”并将它们累积到对象 B 中的函数。我该怎么做?
谢谢
编辑:
A 和 B 很复杂,但我会尝试在这里发布一些简化版本。
class A {
private final long time;
private A(long time) {
this.time = time;
}
long getTime() {
return time;
}
}
class B {
// not important, build from "full" temporaryB class
// result of accumulation
}
class TemporaryB {
private final long startingTime;
private int counter;
public TemporaryB(A a) {
this.startingTime = a.getTime();
}
boolean fits(A a) {
return a.getTime() - startingTime < THRESHOLD;
}
void add(A a) {
counter++;
}
}
class Accumulator {
private List<B> accumulatedB;
private TemporaryBParameters temporaryBParameters
public void addA(A a) {
if(temporaryBParameters.fits(a)) {
temporaryBParameters.add(a)
} else {
accumulateB.add(new B(temporaryBParameters)
temporaryBParameters = new TemporaryBParameters(a)
}
}
}
好的,所以这是非常简化的方式,我现在该怎么做。我不喜欢它。它很丑。