wywoływanie wielu funkcji na raz

0

Siemka.
Możecie mi wytłumaczyć jak wywołać coś takiego.

     collectFrom(src).when(sel).mapEvery(map);

co ma spowodować utworzenie listy wynikowej, której elementy stanowią wybrane przez selektor elementy listy, przekształacone za pomocą podanego mappera.

import java.util.*;

public class Main {
  public Main() {
    List<Integer> src = Arrays.asList(1,2,4,5);
    System.out.println(test(src));
  }
  public List<Integer> test(List<Integer> src) {
    Selector<Integer> sel = new Selector<Integer>(){
		@Override
//Imlpementacja zgodnie z założeniami zadania
		public boolean select(Integer t) {
			if(t<10)
				return true;
			else
				return false;
		}
    };
    Mapper<Integer> map = new Mapper<Integer>(){
//Imlpementacja zgodnie z założeniami zadania
		@Override
		public int map(Integer t) {
			return t+10;
		}
    };
    return  collectFrom(src).when(sel).mapEvery(map);//Tu jest problem
  } 

Mapper i Selector to dwa interface z funkcjami select i map.
collectForm jest statyczna metoda innej klasy ListCreator ktora musze stworzyc.
Jak ma przebiegac implementacja funkcji collectForm, when i mapEvery aby manipulowaly zwracana kolekcja.

 import java.util.List;

public class ListCreator<T>{
	static <T> List collectFrom(List<T> list){
		
	}
	
	when(Selector<T> sel){
		
	}
	
	mapEvery(Mapper<T> map){
		
	}

}   
0

Będę złym człowiekiem i dam Ci gotowe rozwiązanie.

 public List<Integer> test(final List<Integer> src) {
		final Selector<Integer> sel = t -> t < 10;
		final Mapper<Integer> map = t -> t + 10;
		return collectFrom(src).when(sel).mapEvery(map);
	}
 public class ListCreator<T> {
	
	private List<T> list;
	
	private ListCreator(final List<T> list) {
		this.list = list;
	}
	
	public static <T> ListCreator<T> collectFrom(final List<T> list){
		return new ListCreator<>(list);
	}
	
	public ListCreator<T> when(final Selector<T> sel){
		final List<T> resultList = new ArrayList<>();
		for (final T element : this.list) {
		    if(sel.select(element)){
				resultList.add(element);
			}
		}
		this.list = resultList;
		return this;
	}
	
	public List<T> mapEvery(final Mapper<T> map){
		final List<T> resultList = new ArrayList<>();
		for (final T element : this.list) {
			resultList.add(map.map(element));
		}
		this.list = resultList;
		return this.list;
	}
}
0

Oh kurczę, przyjąłem że Mapper ma taki interfejs

 T map(T t);

a nie tak jak u Ciebie

int map(T t);

liczę że z taką drobnostką dasz sobie radę ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1