Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr19.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
20 /**
21 * POIs ordered by type. Section 18 is the index into this.
22 *
23 * @author Steve Ratcliffe
24 */
25 public class Mdr19 extends MdrSection implements HasHeaderFlags {
26 private List<Mdr11Record> pois;
27 private final List<Mdr18Record> poiTypes = new ArrayList<>();
28
29 public Mdr19(MdrConfig config) {
30 setConfig(config);
31 }
32
33 /**
34 * Sort the pois by type.
35 */
36 @Override
37 public void preWriteImpl() {
38 // For mainly historical reasons, we keep the element type in a number
39 // of different formats. Need to normalise it before sorting.
40 pois.sort((o1, o2) -> Integer.compare(MdrUtils.fullTypeToNaturalType(o1.getType()),
41 MdrUtils.fullTypeToNaturalType(o2.getType())));
42 }
43
44 /**
45 * Write out the contents of this section.
46 *
47 * @param writer Where to write it.
48 */
49 public void writeSectData(ImgFileWriter writer) {
50 int n = getSizes().getPoiSizeFlagged();
51 int flag = getSizes().getPoiFlag();
52
53 String lastName = null;
54 int lastType = -1;
55 int record = 1;
56 for (Mdr11Record p : pois) {
57 int index = p.getRecordNumber();
58 String name = p.getName();
59 if (!name.equals(lastName)) {
60 index |= flag;
61 lastName = name;
62 }
63 writer.putNu(n, index);
64
65 int type = MdrUtils.fullTypeToNaturalType(p.getType());
66 if (type != lastType) {
67 Mdr18Record mdr18 = new Mdr18Record();
68 mdr18.setType(type);
69 mdr18.setRecord(record);
70 poiTypes.add(mdr18);
71 lastType = type;
72 }
73 record++;
74 }
75
76 Mdr18Record m18 = new Mdr18Record();
77 m18.setRecord(record);
78 m18.setType(0xffff);
79 poiTypes.add(m18);
80 }
81
82 /**
83 * Release the copy of the pois. The other index is small and not worth
84 * worrying about.
85 */
86 @Override
87 protected void releaseMemory() {
88 pois = null;
89 }
90
91 /**
92 * Records in this section are record numbers into mdr 11 with a flag
93 * so has to be large enough for a flagged index.
94 *
95 * @return The size of a record in this section.
96 */
97 public int getItemSize() {
98 return getSizes().getPoiSizeFlagged();
99 }
100
101 /**
102 * Method to be implemented by subclasses to return the number of items in the section. This will only be valid after
103 * the section is completely finished etc.
104 *
105 * @return The number of items in the section.
106 */
107 protected int numberOfItems() {
108 return pois.size();
109 }
110
111 /**
112 * Not yet known.
113 *
114 * @return The correct value based on the contents of the section. Zero if nothing needs to be done.
115 */
116 public int getExtraValue() {
117 return getItemSize() - 1;
118 }
119
120 public void setPois(List<Mdr11Record> pois) {
121 this.pois = pois;
122 }
123
124 public List<Mdr18Record> getPoiTypes() {
125 return poiTypes;
126 }
127 }