finagle in java - Function, Function1 etc

955 views Asked by At

How do I create Function1 object for use in flatMap method of finagle's Future object in Java?

Tried this:

Function1<String, String> f = new Function1<String, String>() {
    @Override
    public String apply(String s) {
        return null;
    }
};

But it doesn't work:

Error:(22, 73) java: is not abstract and does not override abstract method andThen$mcVJ$sp(scala.Function1) in scala.Function1

1

There are 1 answers

0
Travis Brown On BEST ANSWER

For the sake of completeness, here's the answer from my two month-old comment above.

First for some imports:

import scala.Function1;
import scala.runtime.AbstractFunction1;

And now you only have to define the apply method:

Function1<String, String> f = new AbstractFunction1<String, String>() {
  public String apply(String s) {
    return s;
  }
};

If you're using Finagle, though, Twitter's Util library also provides a similar helper class:

import com.twitter.util.Function;
import scala.Function1;

And then:

Function1<String, String> f = new Function<String, String>() {
  public String apply(String s) {
    return s;
  }
};

This latter option is probably better—I've never really liked explicitly using stuff from scala.runtime.