Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr25.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2011.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.mdr;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import uk.me.parabola.imgfmt.app.ImgFileWriter;
19 import uk.me.parabola.imgfmt.app.srt.Sort;
20 import uk.me.parabola.imgfmt.app.srt.SortKey;
21
22 /**
23 * Cities sorted by country.
24 *
25 * @author Steve Ratcliffe
26 */
27 public class Mdr25 extends MdrSection {
28 private final List<Mdr5Record> cities = new ArrayList<>();
29
30 public Mdr25(MdrConfig config) {
31 setConfig(config);
32 }
33
34 /**
35 * Cities are sorted by country and then by the mdr5 city record number.
36 * @param list The complete list of cities from mdr5.
37 */
38 public void sortCities(List<Mdr5Record> list) {
39 Sort sort = getConfig().getSort();
40
41 List<SortKey<Mdr5Record>> keys = new ArrayList<>();
42 for (Mdr5Record c : list) {
43 SortKey<Mdr5Record> key = sort.createSortKey(c, c.getMdrCountry().getName(), c.getGlobalCityIndex());
44 keys.add(key);
45 }
46
47 keys.sort(null);
48
49 String lastName = null;
50 Mdr5Record lastCity = null;
51 int record = 0;
52 for (SortKey<Mdr5Record> key : keys) {
53 Mdr5Record city = key.getObject();
54
55 if (lastCity == null || !city.getName().equals(lastCity.getName())
56 || !city.getRegionName().equals(lastCity.getRegionName())
57 || !city.getCountryName().equals(lastCity.getCountryName())) {
58 record++;
59
60 // Record in the 29 index if there is one for this record
61 Mdr14Record mdrCountry = city.getMdrCountry();
62 Mdr29Record mdr29 = mdrCountry.getMdr29();
63 String name = mdr29.getName();
64 assert mdrCountry.getName().equals(name);
65 if (!name.equals(lastName)) {
66 mdr29.setMdr25(record);
67 lastName = name;
68 }
69 cities.add(city);
70 lastCity = city;
71 }
72 }
73 }
74
75 /**
76 * Write out the contents of this section.
77 *
78 * @param writer Where to write it.
79 */
80 public void writeSectData(ImgFileWriter writer) {
81 int size = getItemSize();
82 for (Mdr5Record city : cities) {
83 writer.putNu(size, city.getGlobalCityIndex());
84 }
85 }
86
87 /**
88 * One field pointing to a city. Not flagged.
89 */
90 public int getItemSize() {
91 return getSizes().getCitySize();
92 }
93
94 /**
95 * The number of records in this section.
96 *
97 * @return The number of items in the section.
98 */
99 protected int numberOfItems() {
100 return cities.size();
101 }
102 }