Browse Source

Clases

master
Cristobal Bernal Mayordomo 1 year ago
parent
commit
337d85fa3a
5 changed files with 223 additions and 0 deletions
  1. +69
    -0
      src/main/java/com/cristobalbernal/loladvisor/dao/LolDaoImplMemory.java
  2. +35
    -0
      src/main/java/com/cristobalbernal/loladvisor/dao/UtilLolFileReader.java
  3. +11
    -0
      src/main/java/com/cristobalbernal/loladvisor/service/LolQueryService.java
  4. +57
    -0
      src/main/java/com/cristobalbernal/loladvisor/service/LolQueryServiceImpl.java
  5. +51
    -0
      src/main/java/com/cristobalbernal/loladvisor/service/LolService.java

+ 69
- 0
src/main/java/com/cristobalbernal/loladvisor/dao/LolDaoImplMemory.java View File

@ -0,0 +1,69 @@
package com.cristobalbernal.loladvisor.dao;
import com.cristobalbernal.loladvisor.config.AppConfig;
import com.cristobalbernal.loladvisor.model.Lol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@Repository
public class LolDaoImplMemory implements LolDao {
public List<Lol> lol = new ArrayList<>();
@Autowired
private AppConfig appConfig;
@PostConstruct
public void init() {
lol = UtilLolFileReader.readFile(appConfig.getFile(), appConfig.getSeparator(), appConfig.getListSeparator());
}
public Lol findById(long id) {
Optional<Lol> result = lol.stream().filter(vg -> vg.getId() == id).findFirst();
return result.orElse(null);
}
public Collection<Lol> findAll() {
return lol;
}
public void insert(Lol videogame) {
lol.add(videogame);
}
public void edit(Lol videogame) {
int i = getIndexOf(videogame.getId());
if(i != -1) {
lol.set(i, videogame);
}
}
public void delete(long id) {
int i = getIndexOf(id);
if(i != -1) {
lol.remove(i);
}
}
private int getIndexOf(long id) {
boolean founds = false;
int i = 0;
while(!founds && i < lol.size()) {
if(lol.get(i).getId() == id) {
founds = true;
} else {
i++;
}
}
return (founds) ? i : -1;
}
}

+ 35
- 0
src/main/java/com/cristobalbernal/loladvisor/dao/UtilLolFileReader.java View File

@ -0,0 +1,35 @@
package com.cristobalbernal.loladvisor.dao;
import com.cristobalbernal.loladvisor.model.Lol;
import org.springframework.util.ResourceUtils;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class UtilLolFileReader {
public static List<Lol> readFile(final String path, final String separator, final String listSeparator) {
List<Lol> result = new ArrayList<Lol>();
try {
// @formatter:off
result = Files
.lines(Paths.get(ResourceUtils.getFile(path).toURI()))
.skip(1)
.map(line -> {
String[] values = line.split(separator);
return new Lol(Long.parseLong(values[0]), values[1],
Arrays.asList(values[3].split(listSeparator)), values[4]);
}).collect(Collectors.toList());
// @formatter:on
} catch (Exception e) {
System.err.println("Error de lectura del fichero de datos");
System.exit(-1);
}
return result;
}
}

+ 11
- 0
src/main/java/com/cristobalbernal/loladvisor/service/LolQueryService.java View File

@ -0,0 +1,11 @@
package com.cristobalbernal.loladvisor.service;
import com.cristobalbernal.loladvisor.model.Lol;
import java.util.Collection;
public interface LolQueryService {
public Collection<Lol> exec();
public LolQueryService anyName(String... name);
public LolQueryService allName(String... name);
public LolQueryService titleContains(String title);
}

+ 57
- 0
src/main/java/com/cristobalbernal/loladvisor/service/LolQueryServiceImpl.java View File

@ -0,0 +1,57 @@
package com.cristobalbernal.loladvisor.service;
import com.cristobalbernal.loladvisor.dao.LolDao;
import com.cristobalbernal.loladvisor.model.Lol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Service
public class LolQueryServiceImpl implements LolQueryService {
@Autowired
private LolDao dao;
private Predicate<Lol> predicate;
@PostConstruct
public void init() {
predicate = null;
}
@Override
public Collection<Lol> exec() {
//@formatter:off
return dao.findAll()
.stream()
.filter(predicate)
.collect(Collectors.toList());
//@formatter:on
}
@Override
public LolQueryService anyName(String... name) {
Predicate<Lol> pAnyGenre = (vg -> Arrays.stream(name).anyMatch(vg.getNombre()::contains));
predicate = (predicate == null) ? pAnyGenre : predicate.and(pAnyGenre);
return this;
}
@Override
public LolQueryService allName(String... name) {
Predicate<Lol> pAllGenre = (vg -> Arrays.stream(name).allMatch(vg.getNombre()::contains));
predicate = (predicate == null) ? pAllGenre : predicate.and(pAllGenre);
return this;
}
@Override
public LolQueryService titleContains(String title) {
Predicate<Lol> pTitleContains = (vg -> vg.getNombre().toLowerCase().contains(title.toLowerCase()));
predicate = (predicate == null) ? pTitleContains : predicate.and(pTitleContains);
return this;
}
}

+ 51
- 0
src/main/java/com/cristobalbernal/loladvisor/service/LolService.java View File

@ -0,0 +1,51 @@
package com.cristobalbernal.loladvisor.service;
import com.cristobalbernal.loladvisor.dao.LolDao;
import com.cristobalbernal.loladvisor.model.Lol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class LolService {
@Autowired
LolDao lolDao;
@Autowired
LolQueryService queryService;
public Collection<String> findAllRol() {
List<String> result = null;
result = lolDao.findAll()
.stream()
.map(vg -> vg.getRol())
.flatMap(list -> list.stream())
.distinct()
.sorted()
.collect(Collectors.toList());
// @formatter:on
return result;
}
public Collection<Lol> findByAnyGenre(String... genres) {
return queryService.anyName(genres).exec();
}
public Collection<Lol> findByAllGenres(String... genres) {
return queryService.allName(genres).exec();
}
public Collection<Lol> findByTitleContains(String title) {
return queryService.titleContains(title).exec();
}
public Collection<Lol> findAll() {
return lolDao.findAll();
}
}

Loading…
Cancel
Save