2

I have a method from library, which look like this:

<T> T invoke(K key, EntryProcessor<K, V, T> entryProcessor, Object... arguments);

And EntryProcessor interface has a single method definition:

T process(MutableEntry<K, V> entry, Object... arguments) throws EntryProcessorException;

Unfortunately for my use case I need EntryProcessor with Serializable.

One solution is to make a new class implementing both EntryProcessor and Serializable, in that case I would have to create a different class for each different use case.

Normally, without Serializable, I can call invoke method as a lambda, which saves me from creating pack of specialized classes.

foo.invoke(1L, (entry, arguments) -> { 
    ...
    return null;
});

Is there a way to extend this lambda with Serializable interface?

Forin
  • 1,549
  • 2
  • 20
  • 44

1 Answers1

0

how about this?

Object[] arguments = new Object[0];
EntryProcessor<Long, String, Integer> it;

it = (EntryProcessor<Long, String, Integer> & Serializable) (entry, args) -> 1;

invoke(1L, it, arguments);
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • 1
    That helper method does not work. It returns a serializable object having a reference to the `original` object that is not serializable. So any attempt to serialize it will fail. – Holger Jul 05 '17 at 14:21
  • yeah. thanks sir. I'll remove it. – holi-java Jul 05 '17 at 14:27