Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr26.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 * An index into mdr28 (region names), sorted by country name.
24 *
25 * @author Steve Ratcliffe
26 */
27 public class Mdr26 extends MdrSection {
28 private final List<Mdr28Record> index = new ArrayList<>();
29
30 public Mdr26(MdrConfig config) {
31 setConfig(config);
32 }
33
34 public void sortMdr28(List<Mdr28Record> in) {
35 Sort sort = getConfig().getSort();
36
37 List<SortKey<Mdr28Record>> sortList = new ArrayList<>();
38 int record = 0;
39 for (Mdr28Record mdr28 : in) {
40 SortKey<Mdr28Record> key = sort.createSortKey(mdr28, mdr28.getMdr14().getName(), ++record);
41 sortList.add(key);
42 }
43 sortList.sort(null);
44
45 addToIndex(sortList);
46 }
47
48 private void addToIndex(List<SortKey<Mdr28Record>> sortList) {
49 String lastName = null;
50 int record26 = 0;
51 for (SortKey<Mdr28Record> key : sortList) {
52 record26++;
53 Mdr28Record mdr28 = key.getObject();
54 Mdr14Record mdr14 = mdr28.getMdr14();
55 assert mdr14 != null;
56
57 // For each new name, set up the mdr29 record for it.
58 String name = mdr14.getName();
59 if (!name.equals(lastName)) {
60 Mdr29Record mdr29 = mdr14.getMdr29();
61 mdr29.setMdr26(record26);
62 lastName = name;
63 }
64 index.add(mdr28);
65 }
66 }
67
68 /**
69 * Write out the contents of this section.
70 *
71 * @param writer Where to write it.
72 */
73 public void writeSectData(ImgFileWriter writer) {
74 int size = getSizes().getSize(28);
75 for (Mdr28Record record : index) {
76 writer.putNu(size, record.getIndex());
77 }
78 }
79
80 /**
81 * The size of a record in the section. This is not a constant and might vary
82 * on various factors, such as the file version, if we are preparing for a
83 * device, the number of maps etc.
84 *
85 * @return The size of a record in this section.
86 */
87 public int getItemSize() {
88 return getSizes().getSize(28);
89 }
90
91 /**
92 * The number of records in this section.
93 *
94 * @return The number of items in the section.
95 */
96 protected int numberOfItems() {
97 return index.size();
98 }
99 }