Created
March 1, 2016 11:46
-
-
Save hacksoldier/dffe84f429ae349566c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2016 Marco Velluto | |
* @since Java 1.8 | |
* @dependency: <groupId>joda-time</groupId> | |
*/ | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
import org.joda.time.DateTime; | |
import org.junit.Test; | |
public class FilterMapStreamTest | |
{ | |
@Test | |
public final void test() throws ParseException | |
{ | |
List<DeviceStorico> storico = new ArrayList<DeviceStorico>(); | |
storico = popolaLista(); | |
this.popolaDeviceDTO("5202", storico); | |
} | |
private List<DeviceStorico> popolaLista() throws ParseException | |
{ | |
List<DeviceStorico> storico = new ArrayList<DeviceStorico>(); | |
DeviceStorico ds = new DeviceStorico(); | |
ds = new DeviceStorico(); | |
ds.setUrbisCode("5202"); | |
ds.setGruppoDistribuzioneId(2107L); | |
ds.setDataCambioGruppo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-02-03 14:51:55")); | |
storico.add(ds); | |
ds = new DeviceStorico(); | |
ds.setUrbisCode("5202"); | |
ds.setGruppoDistribuzioneId(2107L); | |
ds.setDataCambioGruppo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-02-01 14:51:26")); | |
storico.add(ds); | |
ds = new DeviceStorico(); | |
ds.setUrbisCode("5202"); | |
ds.setGruppoDistribuzioneId(null); | |
ds.setDataCambioGruppo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-02-02 14:51:55")); | |
storico.add(ds); | |
ds = new DeviceStorico(); | |
ds.setUrbisCode("5202"); | |
ds.setGruppoDistribuzioneId(2110L); | |
ds.setDataCambioGruppo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-02-01 14:51:55")); | |
storico.add(ds); | |
ds = new DeviceStorico(); | |
ds.setUrbisCode("5202"); | |
ds.setGruppoDistribuzioneId(2107L); | |
ds.setDataCambioGruppo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-02-02 14:51:55")); | |
storico.add(ds); | |
ds = new DeviceStorico(); | |
ds.setUrbisCode("5202"); | |
ds.setGruppoDistribuzioneId(null); | |
ds.setDataCambioGruppo(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-02-03 14:51:55")); | |
storico.add(ds); | |
return storico; | |
} | |
private DeviceDTO popolaDeviceDTO(String codiceUrbis, List<DeviceStorico> storico) | |
{ | |
DeviceDTO deviceDto = new DeviceDTO(); | |
deviceDto.setCodiceUrbis(codiceUrbis); | |
List<GruppiDTO> listaGruppiDTO = new ArrayList<GruppiDTO>(); | |
storico.sort((deviceStorico1, deviceStorico2) -> deviceStorico1.getDataCambioGruppo().compareTo(deviceStorico2.getDataCambioGruppo())); | |
Set<Integer> days = storico.stream().map(device -> new DateTime(device.getDataCambioGruppo()).getDayOfMonth()).collect(Collectors.toSet()); | |
Set<Date> dates = storico.stream().map(d -> d.getDataCambioGruppo()).collect(Collectors.toSet()); | |
for (Integer day : days) | |
{ | |
GruppiDTO gruppi = new GruppiDTO(); | |
Optional<Date> date = dates.stream().filter(data -> new DateTime(data).getDayOfMonth() == day).findFirst(); //.map(d -> d.getDate()) | |
if (!date.isPresent()) | |
throw new RuntimeException("Nessuna data trovata"); | |
gruppi.setDataCambioGruppo(date.get()); | |
gruppi.setGruppiId(storico.stream().filter(deviceStorico -> new DateTime(deviceStorico.getDataCambioGruppo()).getDayOfMonth() == day) | |
.map(deviceStorico -> deviceStorico.getGruppoDistribuzioneId()).collect(Collectors.toList())); | |
listaGruppiDTO.add(gruppi); | |
} | |
deviceDto.setDateGruppiDTO(listaGruppiDTO); | |
System.out.println(deviceDto.toString()); | |
return deviceDto; | |
} | |
@SuppressWarnings("unused") | |
private class DeviceStorico | |
{ | |
private String urbisCode; | |
private Long gruppoDistribuzioneId; | |
private Date dataCambioGruppo; | |
public String getUrbisCode() | |
{ | |
return urbisCode; | |
} | |
public void setUrbisCode(String urbisCode) | |
{ | |
this.urbisCode = urbisCode; | |
} | |
public Long getGruppoDistribuzioneId() | |
{ | |
return gruppoDistribuzioneId; | |
} | |
public void setGruppoDistribuzioneId(Long gruppoDistribuzioneId) | |
{ | |
this.gruppoDistribuzioneId = gruppoDistribuzioneId; | |
} | |
public Date getDataCambioGruppo() | |
{ | |
return dataCambioGruppo; | |
} | |
public void setDataCambioGruppo(Date dataCambioGruppo) | |
{ | |
this.dataCambioGruppo = dataCambioGruppo; | |
} | |
} | |
public class DeviceDTO | |
{ | |
private Long deviceId; | |
private String codiceUrbis; | |
private Long gruppoDistribuzioneId; | |
private List<GruppiDTO> dateGruppiDTO; | |
public Long getDeviceId() | |
{ | |
return deviceId; | |
} | |
public void setDeviceId(Long deviceId) | |
{ | |
this.deviceId = deviceId; | |
} | |
public String getCodiceUrbis() | |
{ | |
return codiceUrbis; | |
} | |
public void setCodiceUrbis(String codiceUrbis) | |
{ | |
this.codiceUrbis = codiceUrbis; | |
} | |
public Long getGruppoDistribuzioneId() | |
{ | |
return gruppoDistribuzioneId; | |
} | |
public void setGruppoDistribuzioneId(Long gruppoDistribuzioneId) | |
{ | |
this.gruppoDistribuzioneId = gruppoDistribuzioneId; | |
} | |
public List<GruppiDTO> getDateGruppiDTO() | |
{ | |
return dateGruppiDTO; | |
} | |
public void setDateGruppiDTO(List<GruppiDTO> dateGruppiDTO) | |
{ | |
this.dateGruppiDTO = dateGruppiDTO; | |
} | |
@Override | |
public String toString() | |
{ | |
return String.format("DeviceDTO [deviceId=%s, codiceUrbis=%s, gruppoDistribuzioneId=%s, dateGruppiDTO=%s]", deviceId, codiceUrbis, | |
gruppoDistribuzioneId, dateGruppiDTO); | |
} | |
} | |
@SuppressWarnings("unused") | |
private class GruppiDTO | |
{ | |
private Date dataCambioGruppo; | |
private List<Long> gruppiId; | |
public Date getDataCambioGruppo() | |
{ | |
return dataCambioGruppo; | |
} | |
public void setDataCambioGruppo(Date dataCambioGruppo) | |
{ | |
this.dataCambioGruppo = dataCambioGruppo; | |
} | |
public List<Long> getGruppiId() | |
{ | |
return gruppiId; | |
} | |
public void setGruppiId(List<Long> gruppiId) | |
{ | |
this.gruppiId = gruppiId; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment