• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkiraly / metadata-qa-marc / #1527

22 Aug 2025 02:21PM UTC coverage: 90.345%. Remained the same
#1527

push

pkiraly
Improve timeline handling

5191 of 6416 new or added lines in 219 files covered. (80.91%)

886 existing lines in 78 files now uncovered.

36717 of 40641 relevant lines covered (90.34%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/de/gwdg/metadataqa/marc/analysis/DataElementCounter.java
1
package de.gwdg.metadataqa.marc.analysis;
2

3
import de.gwdg.metadataqa.marc.MarcSubfield;
4
import de.gwdg.metadataqa.marc.dao.DataField;
5
import de.gwdg.metadataqa.marc.dao.record.BibliographicRecord;
6
import org.apache.commons.io.FileUtils;
7

8
import java.io.File;
9
import java.io.IOException;
10
import java.nio.charset.StandardCharsets;
11
import java.util.ArrayList;
12
import java.util.LinkedHashMap;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.logging.Logger;
16

17
public class DataElementCounter {
18

19
  public enum Basis{EXISTENCE, OCCURENCE}
×
20

21
  private static final Logger logger = Logger.getLogger(DataElementCounter.class.getCanonicalName());
×
22

23
  List<DataElement> elements = new ArrayList<>();
×
24
  Map<String, List<DataElement>> tags = new LinkedHashMap<>();
×
25
  private final String header;
26
  private final Basis basis;
27

28
  public DataElementCounter(String dir, String fileName, Basis basis) {
×
29
    this.basis = basis;
×
30
    File file = new File(dir, fileName);
×
31
    String firstLine = "";
×
32
    try {
33
      List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
×
34
      firstLine = lines.get(0);
×
35
      String[] topFields = firstLine.split(",");
×
36
      for (String field : topFields) {
×
37
        String[] parts = field.split("\\$");
×
38
        DataElement element = new DataElement(parts[0], parts[1]);
×
39
        elements.add(element);
×
40
        tags.computeIfAbsent(element.field, s -> new ArrayList<>());
×
41
        tags.get(element.field).add(element);
×
42
      }
43
    } catch (IOException e) {
×
44
      logger.severe(e.getLocalizedMessage());
×
45
    }
×
46
    this.header = firstLine;
×
47
  }
×
48

49
  public List<Integer> count(BibliographicRecord marcRecord) {
50
    List<Integer> counts = new ArrayList<>();
×
51
    for (Map.Entry<String, List<DataElement>> entry : tags.entrySet()) {
×
NEW
52
      List<DataField> instances = marcRecord.getDatafieldsByTag(entry.getKey());
×
53
      if (instances == null || instances.isEmpty()) {
×
54
        for (int i=0; i<entry.getValue().size(); i++) {
×
55
          counts.add(0);
×
56
        }
57
      } else {
58
        Map<String, Integer> result = new LinkedHashMap<>();
×
59
        for (DataField instance : instances) {
×
60
          for (DataElement element : entry.getValue()) {
×
61
            result.computeIfAbsent(element.subfield, s -> 0);
×
62
            List<MarcSubfield> subfields = instance.getSubfield(element.subfield);
×
63
            if (subfields != null && !subfields.isEmpty()) {
×
64
              result.put(element.subfield, result.get(element.subfield) + subfields.size());
×
65
            }
66
          }
×
67
        }
×
68
        for (DataElement element : entry.getValue()) {
×
69
          int score = result.get(element.subfield);
×
70
          if (basis.equals(Basis.EXISTENCE) && score >= 1)
×
71
            score = 1;
×
72
          counts.add(score);
×
73
        }
×
74
      }
75
    }
×
76
    return counts;
×
77
  }
78

79
  public String getHeader() {
80
    return header;
×
81
  }
82

83
  class DataElement {
84
    String field;
85
    String subfield;
86
    String key;
87

88
    public DataElement(String field, String subfield) {
×
89
      this.field = field;
×
90
      this.subfield = subfield;
×
91
      this.key = field + "$" + subfield;
×
92
    }
×
93
  }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc