Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr27.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 region name.
24 *
25 * @author Steve Ratcliffe
26 */
27 public class Mdr27 extends MdrSection {
28 private final List<Mdr5Record> cities = new ArrayList<>();
29
30 public Mdr27(MdrConfig config) {
31 setConfig(config);
32 }
33
34 /**
35 * Cities are sorted by region 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 Mdr13Record mdrRegion = c.getMdrRegion();
44 if (mdrRegion != null) {
45 SortKey<Mdr5Record> key = sort.createSortKey(c, mdrRegion.getName(), c.getGlobalCityIndex());
46 keys.add(key);
47 }
48 }
49
50 keys.sort(null);
51
52 String lastName = null;
53 int record = 0;
54 for (SortKey<Mdr5Record> key : keys) {
55 record++;
56 Mdr5Record city = key.getObject();
57
58 Mdr13Record mdrRegion = city.getMdrRegion();
59 Mdr28Record mdr28 = mdrRegion.getMdr28();
60 String name = mdr28.getName();
61 if (!name.equals(lastName)) {
62 mdr28.setMdr27(record);
63 lastName = name;
64 }
65
66 cities.add(city);
67 }
68 }
69
70 /**
71 * Write out the contents of this section.
72 *
73 * @param writer Where to write it.
74 */
75 public void writeSectData(ImgFileWriter writer) {
76 int size = getItemSize();
77 for (Mdr5Record city : cities) {
78 writer.putNu(size, city.getGlobalCityIndex());
79 }
80 }
81
82 /**
83 * The size of a record in the section. This is not a constant and might vary
84 * on various factors, such as the file version, if we are preparing for a
85 * device, the number of maps etc.
86 *
87 * @return The size of a record in this section.
88 */
89 public int getItemSize() {
90 return getSizes().getCitySize();
91 }
92
93 /**
94 * The number of records in this section.
95 *
96 * @return The number of items in the section.
97 */
98 protected int numberOfItems() {
99 return cities.size();
100 }
101 }