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
name = marc
version = 2.9.5
version = 2.9.6
org.gradle.warning.mode = ALL

View file

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

View file

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