allow null marcFields in MarcRecord

This commit is contained in:
Jörg Prante 2022-11-21 18:36:13 +01:00
parent e45038d31a
commit dd40b8918e
3 changed files with 19 additions and 8 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = marc name = marc
version = 2.9.5 version = 2.9.6
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -22,13 +22,15 @@ jar {
} }
task sourcesJar(type: Jar, dependsOn: classes) { task sourcesJar(type: Jar, dependsOn: classes) {
dependsOn classes
classifier 'sources' classifier 'sources'
from sourceSets.main.allSource from sourceSets.main.allSource
} }
task javadocJar(type: Jar, dependsOn: javadoc) { task javadocJar(type: Jar, dependsOn: javadoc) {
dependsOn javadoc
classifier 'javadoc' classifier 'javadoc'
from sourceSets.main.allSource from javadoc.destinationDir
} }
artifacts { artifacts {

View file

@ -49,11 +49,10 @@ public class MarcRecord implements Map<String, Object> {
private transient RecordLabel recordLabel; private transient RecordLabel recordLabel;
private final transient List<MarcField> marcFields; private transient List<MarcField> marcFields;
private MarcRecord(Map<String, Object> delegate) { private MarcRecord(Map<String, Object> delegate) {
this.delegate = delegate; this.delegate = delegate;
this.marcFields = new LinkedList<>();
} }
/** /**
@ -107,6 +106,7 @@ public class MarcRecord implements Map<String, Object> {
RecordLabel recordLabel, RecordLabel recordLabel,
Collection<String> privateTags) { Collection<String> privateTags) {
MarcRecord marcRecord = new MarcRecord(map); MarcRecord marcRecord = new MarcRecord(map);
marcRecord.marcFields = new LinkedList<>();
Set<String> forbidden = new HashSet<>(privateTags); Set<String> forbidden = new HashSet<>(privateTags);
forbidden.add(formatTag); forbidden.add(formatTag);
forbidden.add(typeTag); forbidden.add(typeTag);
@ -214,8 +214,10 @@ public class MarcRecord implements Map<String, Object> {
* @param handler the handler * @param handler the handler
*/ */
public void all(Predicate<? super MarcField> predicate, MarcFieldHandler handler) { public void all(Predicate<? super MarcField> predicate, MarcFieldHandler handler) {
if (marcFields != null) {
marcFields.stream().filter(predicate).forEach(handler::field); marcFields.stream().filter(predicate).forEach(handler::field);
} }
}
/** /**
* Return all MARC fields of this record with a given tag and indicator. * Return all MARC fields of this record with a given tag and indicator.
@ -298,8 +300,10 @@ public class MarcRecord implements Map<String, Object> {
* @param handler the handler * @param handler the handler
*/ */
public void first(Predicate<? super MarcField> predicate, MarcFieldHandler handler) { public void first(Predicate<? super MarcField> predicate, MarcFieldHandler handler) {
if (marcFields != null) {
marcFields.stream().filter(predicate).findFirst().ifPresent(handler::field); marcFields.stream().filter(predicate).findFirst().ifPresent(handler::field);
} }
}
/** /**
* Return the first MARC field of this record with a given tag. * Return the first MARC field of this record with a given tag.
@ -350,8 +354,10 @@ public class MarcRecord implements Map<String, Object> {
* @param handler the handler * @param handler the handler
*/ */
public void any(Predicate<? super MarcField> predicate, MarcFieldHandler handler) { public void any(Predicate<? super MarcField> predicate, MarcFieldHandler handler) {
if (marcFields != null) {
marcFields.stream().filter(predicate).findAny().ifPresent(handler::field); marcFields.stream().filter(predicate).findAny().ifPresent(handler::field);
} }
}
/** /**
* Get any MARC field of this record that satisfies a given predicate. * Get any MARC field of this record that satisfies a given predicate.
@ -437,7 +443,7 @@ public class MarcRecord implements Map<String, Object> {
@Override @Override
public int hashCode() { public int hashCode() {
return (recordLabel.toString() + marcFields.toString()).hashCode(); return (recordLabel.toString() + (marcFields != null ? marcFields.toString() : "")).hashCode();
} }
public String toString() { public String toString() {
@ -447,6 +453,9 @@ public class MarcRecord implements Map<String, Object> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> createMapFromMarcFields(boolean stable) { private Map<String, Object> createMapFromMarcFields(boolean stable) {
Map<String, Object> map = stable ? new TreeMap<>() : new LinkedHashMap<>(); Map<String, Object> map = stable ? new TreeMap<>() : new LinkedHashMap<>();
if (marcFields == null) {
return map;
}
for (MarcField marcField : marcFields) { for (MarcField marcField : marcFields) {
String tag = marcField.getTag(); String tag = marcField.getTag();
int repeat; int repeat;