4

I've started to grok into retrolambda and rxjava. Some expressions i'v converted by myself, but some of them i can't convert. I've added retrolambda to my project.Here is examples

public Observable<ImmutableList<Repository>> getUsersRepositories() {
        return githubApiService.getUsersRepositories(user.login)
                .map(repositoryResponses -> {
                        final ImmutableList.Builder<Repository> listBuilder = ImmutableList.builder();
                        for (RepositoryResponse repositoryResponse : repositoryResponses) {
                            Repository repository = new Repository();
                            repository.id = repositoryResponse.id;
                            repository.name = repositoryResponse.name;
                            repository.url = repositoryResponse.url;
                            listBuilder.add(repository);
                        }
                        return listBuilder.build();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

But i don't know how to convert this peace of code:

obs.subscribe(new Observer<List<Integer>>() {
        public void onCompleted() {
            System.out.println("completed");
        }

        public void onError(Throwable e) {
            System.out.println("failure");
        }

        public void onNext(List<Integer> value) {
            System.out.println("onnext=" + value);
        }
    });
Jenya Kirmiza
  • 511
  • 8
  • 21

3 Answers3

5

I think that you want to make something like this:

obs.subscribe(
         (List<Integer> value) -> System.out.println("onnext=" + value),
         (Throwable e) -> System.out.println("failure"),
         ()-> System.out.println("completed"));

You can check this blog and this example in GitHub.

Cabezas
  • 9,329
  • 7
  • 67
  • 69
2

Lambda is an anonymous function and you are creating an anonymous class, similar but different.

First one is already a Lambda: .map(repositoryResponses -> {...}, what do you want to change more? The second part is a "block code" so you can't reduce it more.

Second one you can't convert it because it's not a function.

Arturo
  • 548
  • 6
  • 15
  • The first code is ok, i've converted it by myself. I changed new Func1() { @Override public User call(UserResponse userResponse) { to repositoryResponses -> {} – Jenya Kirmiza Aug 27 '15 at 13:16
  • What i need is to use it on the second code. If it's not possible it's ok. I'm just asking – Jenya Kirmiza Aug 27 '15 at 13:17
  • Sure, it's ok, I mean, you can't change it more, Lambda is for anonymous functions only so you can use it for anonymous classes (second code). Ask is ok, you can't keep growing with no asking :) – Arturo Aug 27 '15 at 13:18
  • Sorry, typo in my previous comment, you **can't** use it for anonymous classes – Arturo Aug 27 '15 at 13:27
  • I understood, thanks for answer. I found that when it's possible to use retrolambda the compiler is telling it. So it's not suggesting to use lambda on the second code. – Jenya Kirmiza Aug 27 '15 at 13:35
1

I've found the way to solve it. Here is code before conversion

    repositoriesManager.getUsersRepositories().subscribe(new SimpleObserver<ImmutableList<Repository>>() {
        @Override
        public void onNext(ImmutableList<Repository> repositories) {
            repositoriesListActivity.showLoading(false);
            repositoriesListActivity.setRepositories(repositories);
        }

        @Override
        public void onError(Throwable e) {
            repositoriesListActivity.showLoading(false);
        }
    });

after conversion

  repositoriesManager.getUsersRepositories().subscribe(repositories -> {
        repositoriesListActivity.showLoading(false);
        repositoriesListActivity.setRepositories(repositories);
    },throwable -> {
        repositoriesListActivity.showLoading(false);
    });
Jenya Kirmiza
  • 511
  • 8
  • 21