add subfield helpers

This commit is contained in:
Jörg Prante 2022-11-03 17:23:21 +01:00
parent 0037857591
commit a66fae1014
2 changed files with 34 additions and 1 deletions

View file

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

View file

@ -24,6 +24,8 @@ import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -104,6 +106,37 @@ public class MarcField implements Comparable<MarcField> {
return builder.subfields; return builder.subfields;
} }
/**
* Return the subfields of this MARC field as Map. Attention: this works only if there are unique subfield IDs.
* @return the subfields as map
*/
public Map<String, Object> getSubfieldsAsMap() {
return builder.subfields.stream().collect(Collectors.toMap(Subfield::getId, Subfield::getValue));
}
/**
* Return the subfields of this MARC field as Map. Attention: this works only if there are unique subfield IDs.
* @param mergeFunction the merge function
* @param supplier the supplier
* @return the subfields as map
*/
public Map<String, Object> getSubfieldsAsMap(BinaryOperator<Object> mergeFunction, Supplier<Map<String, Object>> supplier) {
return builder.subfields.stream().collect(Collectors.toMap(Subfield::getId, Subfield::getValue,
mergeFunction, supplier));
}
/**
* Collect subfield values of a specified collection of subfield IDs.
* @param subfieldIds the given collection of subfield IDs
* @return the list of subfield values
*/
public List<String> getSubfieldValues(Collection<String> subfieldIds) {
return builder.subfields.stream()
.filter(subfield -> subfieldIds.contains(subfield.getId()))
.map(Subfield::getValue)
.collect(Collectors.toList());
}
/** /**
* Return all subfields of a given subfield ID. Multiple occurences may occur. * Return all subfields of a given subfield ID. Multiple occurences may occur.
* @param subfieldId subfield ID * @param subfieldId subfield ID