|
|
@ -0,0 +1,62 @@ |
|
|
|
package com.antoniofrische.perifericoadvisor.service; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.function.Predicate; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import com.antoniofrische.perifericosAdvisor.dao.PerifericoDao; |
|
|
|
import com.antoniofrische.perifericosAdvisor.model.Periferico; |
|
|
|
|
|
|
|
@Service |
|
|
|
public class PerifericoQueryServiceImpl implements PerifericoQueryService{ |
|
|
|
|
|
|
|
@Autowired |
|
|
|
PerifericoDao dao; |
|
|
|
|
|
|
|
private Predicate<Periferico> predicate; |
|
|
|
|
|
|
|
public void init() { |
|
|
|
predicate = null; |
|
|
|
} |
|
|
|
|
|
|
|
public Collection<Periferico> exec() { |
|
|
|
|
|
|
|
// @formatter:off |
|
|
|
return dao.findAll() |
|
|
|
.stream() |
|
|
|
.filter(predicate) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
// @formatter:on |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public PerifericoQueryServiceImpl anyTipo(String... tipo) { |
|
|
|
Predicate<Periferico> pAnyTipo = (periferico -> Arrays.stream(tipo).anyMatch(periferico.getTipo()::contains)); |
|
|
|
predicate = (predicate == null) ? pAnyTipo : predicate.and(pAnyTipo); |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
public PerifericoQueryServiceImpl allTipo(String... genres) { |
|
|
|
Predicate<Periferico> pAllGenres = (periferico -> Arrays.stream(genres).allMatch(periferico.getTipo()::contains)); |
|
|
|
predicate = (predicate == null) ? pAllGenres : predicate.and(pAllGenres); |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
public PerifericoQueryServiceImpl color(String color) { |
|
|
|
Predicate<Periferico> pYear = (periferico -> periferico.getColor().equalsIgnoreCase(color)); |
|
|
|
predicate = (predicate == null) ? pYear : predicate.and(pYear); |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
public PerifericoQueryServiceImpl titleContains(String nombre) { |
|
|
|
Predicate<Periferico> pTitleContains = (periferico -> periferico.getNombre().toLowerCase().contains(nombre.toLowerCase())); |
|
|
|
predicate = (predicate == null) ? pTitleContains : predicate.and(pTitleContains); |
|
|
|
|
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
} |