I am looking for an abstraction from which to solve a class of problems similar to the following:
I have this ordered ResultSet (in this case sorted for NATION,REGION,DISTRICT,COUNTRY):
CITY' Table
| NATION | REGION | DISTRICT | COUNTRY | POPULATION |
--------------------------------------------------------
ITALIA CAMPANIA CE AVERSA 70.000
ITALIA CAMPANIA CE CASERTA 500.000
ITALIA CAMPANIA CE MARCIANISE 5.000.000
ITALIA CAMPANIA SA BATTIPAGLIA 50.000
ITALIA CAMPANIA SA EBOLI 60.000
ITALIA CAMPANIA SA SARNO 70.000
ITALIA LAZIO RM CAMPAGNANO 50.000
ITALIA LAZIO RM FORMELLO 500.000
ITALIA LAZIO RM ROMA 5.000.000
ITALIA LAZIO VT BOLSENA 50.000
ITALIA LAZIO VT FALERIA 60.000
ITALIA LAZIO VT NEPI 80.000
....
ALBANIA ...
...
RUSSIA....
...
I want to get this output as a list of City objects in which the different totals (groupings) will be included of the population.
| NATION | REGION | DISTRICT | COUNTRY | POPULATION |
--------------------------------------------------------
ITALIA CAMPANIA CE AVERSA 70.000
ITALIA CAMPANIA CE CASERTA 500.000
ITALIA CAMPANIA CE MARCIANISE 5.000.000
ITALIA CAMPANIA SA BATTIPAGLIA 50.000
ITALIA CAMPANIA SA EBOLI 60.000
ITALIA CAMPANIA SA SARNO 70.000
ITALIA LAZIO RM CAMPAGNANO 50.000
ITALIA LAZIO RM FORMELLO 500.000
ITALIA LAZIO RM ROMA 5.000.000
ITALIA LAZIO VT BOLSENA 50.000
ITALIA LAZIO VT FALERIA 60.000
ITALIA LAZIO VT NEPI 80.000
... ... ... ... ...
TOT. CE 5.570.000
TOT. SA 230.000
TOT. RM 5.550.000
TOT. VT 240.000
TOT. CAMPANIA 5.800.000
TOT. LAZIO 5.790.000
TOT. ITALIA 11.590.000
ALBANIA ... ... ... ...
... ... ... ... ...
TOT. XX XXX
TOT. YY YYY
... ...
TOT. ALBANIA ZZZ
RUSSIA ... ... ... ...
... ... ... ... ...
TOT. XX XXX
TOT. YY YYY
... ...
TOT. RUSSIA ZZZ
...
I thought of an algorithm in which I use a map to scan the changes that occur for each column.
Map map = new LinkedHashMap<>(); //preserve insertion order List cityList;
class City {
private BigDecimal population = 0;
...
//getter/setter...
public static final City createBean(ResultSet rs){
City city = new City();
population = rs.getBigDecimal("POPULATION");
...
return city;
}
public add(City bean) {
this.population.add(bean.getPopulation());
...
}
}
Algorithm:
boolean isFirst = true;
ResultSet rs = .... from persistence
while(rs.next()) {
City current = City.createBean(rs);
cityList.add(current);
if(isFirst) {
isFirst = false;
map.put(current.getNation(),new City()); //tot.nation
map.put(current.getRegion(),new City()); //tot.region
map.put(current.getDistrict(),new City()); //tot.district
}
if(!map.containsKey(current.getNation())) { //main breaking for nation
//there was a change of nation then load the map on the cityList
forach key in map
cityList.add(map.getKey(key));
map.clear();
map.put(current.getNation(),new City()); //tot.nation
map.put(current.getRegion(),new City()); //tot.region
map.put(current.getDistrict(),new City()); //tot.district
}
map.get(current.getNation()).add(current);
map.get(current.getRegion()).add(current);
map.get(current.getDistrict()).add(current);
}
Can you direct me to a design with which you can abstract this logic?
I would like to produce a component with these characteristics: - unbound from the persistence system (ResultSet) - unbound from City
Thank you all for every feedback you will give me.