0

I'm trying to wrap my head aroun the bifunction usage in Hazelcast Jets accumulate processor. First attempt is a simple min comparison, but what I came up with looks so unelegant. Is there a better way to do it?

Vertex min = dag.newVertex("min", accumulate(()
    -> new myObject(Type.SOME_ENUM,Double.MAX_VALUE,0L), 
       (cMin, x) -> (((myObject) x).getValue() <  cMin.Value()) ? (myObject) x) : cMin,
       (cMin) -> cMin));

Basically I have a class with 3 fields: Type, Value, TimeStamp, and I want to get object with the lowest value.

My supplier is a new object with value at max.double, which looks fine. My finisher just hands through the object which is fine as well.

But the accumulator looks unneccesarily complicated. Is there a way to avoid having to cast x to myObject twice? Or some even more elegant way, to just keep the double value, but still return the object at the end? WITHOUT having to iterate to the whole map to get the object for the min value again?

Anders Bernard
  • 541
  • 1
  • 6
  • 19

2 Answers2

1

You can use type parameters to static method:

Vertex min = dag.newVertex("min", Processors.<myObject, myObject>accumulate(
                () -> new myObject(Type.SOME_ENUM,Double.MAX_VALUE,0L),
                (cMin, x) -> x.getValue() <  cMin.getValue() ? x : cMin));

I also used the two-parameter constructor, which provides identity() as the third argument.

Btw, myObject should be MyObject, this is a Java convention.

Oliv
  • 10,221
  • 3
  • 55
  • 76
1

There is nothing in your expression that indicates the type of the item the accumulator function expects (the x). Theoretically it could be inferred by propagating the known type of the accumulated value into the expression which constitutes lambda body and finding out that x must be assignment-compatible with it, but Java does not do that.

So you must add more explicit typing, for example by providing explicit types in the accumulator function:

Vertex min = dag.newVertex("min", accumulate(() -> new MyObject(Double.MAX_VALUE),
        (MyObject acc, MyObject x) -> x.getValue() < acc.getValue() ? x : acc));
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436