getField() and getSubfield() methods

This commit is contained in:
Jörg Prante 2017-11-16 09:29:40 +01:00
parent 3fb01e1b2c
commit 195abf74de
7 changed files with 65 additions and 20 deletions

View file

@ -1,6 +1,6 @@
group = org.xbib group = org.xbib
name = marc name = marc
version = 1.0.15 version = 1.0.16
xbib-content.version = 1.0.7 xbib-content.version = 1.0.7
xbib-bibliographic-character-sets.version = 1.0.0 xbib-bibliographic-character-sets.version = 1.0.0

View file

@ -1057,6 +1057,10 @@ public final class Marc {
return this; return this;
} }
public RecordLabel getRecordLabel() {
return recordLabel;
}
public Marc.Builder addField(MarcField marcField) { public Marc.Builder addField(MarcField marcField) {
boolean keymatch = keyPattern == null || marcField.matchKey(keyPattern) != null; boolean keymatch = keyPattern == null || marcField.matchKey(keyPattern) != null;
boolean valuematch = valuePattern == null || marcField.matchValue(valuePattern) != null; boolean valuematch = valuePattern == null || marcField.matchValue(valuePattern) != null;

View file

@ -22,13 +22,16 @@ import org.xbib.marc.label.RecordLabel;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
/** /**
* A MARC field. * A MARC field.
*/ */
public class MarcField implements Comparable<MarcField> { public class MarcField implements Comparable<MarcField> {
public static final MarcField EMPTY = builder().build(); private static final MarcField EMPTY = builder().build();
private static final Subfield EMPTY_SUBFIELD = new Subfield(null, null);
public static final String KEY_DELIMITER = "$"; public static final String KEY_DELIMITER = "$";
@ -73,6 +76,14 @@ public class MarcField implements Comparable<MarcField> {
return new Builder(); return new Builder();
} }
public static MarcField emptyMarcField() {
return EMPTY;
}
public static Subfield emptySubfield() {
return EMPTY_SUBFIELD;
}
/** /**
* Return the MARC field tag. * Return the MARC field tag.
* @return the tag * @return the tag
@ -113,12 +124,28 @@ public class MarcField implements Comparable<MarcField> {
return subfields; return subfields;
} }
/**
* Return all subfields of a given subfield ID. Multiple occurences may occur.
* @param subfieldId subfield ID
* @return list of subfields
*/
public LinkedList<Subfield> getSubfield(String subfieldId) {
return subfields.stream()
.filter(subfield -> subfield.getId().equals(subfieldId))
.collect(Collectors.toCollection(LinkedList::new));
}
/** /**
* Return first subfield or null. Avoids NoSuchElementException of java.util.LinkedList. * Return first subfield or null. Avoids NoSuchElementException of java.util.LinkedList.
* @return first subfield or null * @return first subfield or null
*/ */
public Subfield getFirstSubfield() { public Subfield getFirstSubfield() {
return subfields.isEmpty() ? Subfield.EMPTY_SUBFIELD : subfields.getFirst(); return subfields.isEmpty() ? MarcField.emptySubfield() : subfields.getFirst();
}
public String getFirstSubfieldValue(String subfieldId) {
LinkedList<Subfield> list = getSubfield(subfieldId);
return list.isEmpty() ? null : list.getFirst().getValue();
} }
/** /**
@ -126,7 +153,12 @@ public class MarcField implements Comparable<MarcField> {
* @return last subfield or null * @return last subfield or null
*/ */
public Subfield getLastSubfield() { public Subfield getLastSubfield() {
return subfields.isEmpty() ? Subfield.EMPTY_SUBFIELD : subfields.getLast(); return subfields.isEmpty() ? MarcField.emptySubfield() : subfields.getLast();
}
public String getLastSubfieldValue(String subfieldId) {
LinkedList<Subfield> list = getSubfield(subfieldId);
return list.isEmpty() ? null : list.getLast().getValue();
} }
/** /**
@ -415,6 +447,12 @@ public class MarcField implements Comparable<MarcField> {
return this; return this;
} }
public Builder subfield(Subfield subfield) {
subfields.add(subfield);
subfieldIds.add(subfield.getId());
return this;
}
/** /**
* Set subfield with subfield ID and value. * Set subfield with subfield ID and value.
* @param subfieldId the subfield ID * @param subfieldId the subfield ID
@ -472,8 +510,7 @@ public class MarcField implements Comparable<MarcField> {
public Builder value(RecordLabel recordLabel, String value) { public Builder value(RecordLabel recordLabel, String value) {
if (value.length() > 0) { if (value.length() > 0) {
int len = recordLabel.getSubfieldIdentifierLength() - 1; /* minus length of US separator char */ int len = recordLabel.getSubfieldIdentifierLength() - 1; /* minus length of US separator char */
boolean createSubfields = len > 0 && value.length() > len; if (!isControl() && len > 0 && value.length() > len) {
if (createSubfields) {
String id = value.substring(0, len); String id = value.substring(0, len);
String content = value.substring(len); String content = value.substring(len);
subfield(id, content); subfield(id, content);
@ -525,12 +562,6 @@ public class MarcField implements Comparable<MarcField> {
indicator(raw.substring(3, pos)); indicator(raw.substring(3, pos));
value(format, type, recordLabel, raw.substring(pos)); value(format, type, recordLabel, raw.substring(pos));
} }
/*int subfieldidlen = label.getSubfieldIdentifierLength();
if (raw.length() >= pos + subfieldidlen) {
String subfieldId = raw.substring(pos, pos + subfieldidlen);
this.subfields.add(new Subfield(subfieldId, raw.substring(pos + subfieldidlen)));
subfieldIds.add(subfieldId);
}*/
} }
return this; return this;
} }
@ -610,8 +641,6 @@ public class MarcField implements Comparable<MarcField> {
*/ */
public static class Subfield { public static class Subfield {
static final Subfield EMPTY_SUBFIELD = new Subfield(null, null);
private final String id; private final String id;
private final String value; private final String value;

View file

@ -110,6 +110,16 @@ public class MarcRecord extends LinkedHashMap<String, Object> {
return marcFields; return marcFields;
} }
/**
* Return the MARC fields of this record with a given tag.
*
* @return the MARC field list matching the given tag.
*/
public List<MarcField> getFields(String tag) {
return marcFields.stream().filter(marcField -> marcField.getTag().equals(tag))
.collect(Collectors.toList());
}
/** /**
* Return a list of MARC fields of this record where key pattern matches were found. * Return a list of MARC fields of this record where key pattern matches were found.
* *
@ -117,8 +127,10 @@ public class MarcRecord extends LinkedHashMap<String, Object> {
* @return a list of MARC fields * @return a list of MARC fields
*/ */
public List<MarcField> filterKey(Pattern pattern) { public List<MarcField> filterKey(Pattern pattern) {
return marcFields.stream().map(field -> return marcFields.stream()
field.matchKey(pattern)).filter(Objects::nonNull).collect(Collectors.toList()); .map(field -> field.matchKey(pattern))
.filter(Objects::nonNull)
.collect(Collectors.toList());
} }
/** /**

View file

@ -316,7 +316,7 @@ public class MarcFieldTransformer extends LinkedHashMap<String, MarcField> {
} }
public Builder drop(MarcField a) { public Builder drop(MarcField a) {
map.put(getTransformKey(a), MarcField.EMPTY); map.put(getTransformKey(a), MarcField.emptyMarcField());
return this; return this;
} }

View file

@ -40,7 +40,7 @@ public class MarcFieldTransformers extends LinkedList<MarcFieldTransformer> {
if (key != null) { if (key != null) {
MarcField transformedMarcField = marcFieldTransformer. MarcField transformedMarcField = marcFieldTransformer.
transform(op, marcField, key, lastBuilt); transform(op, marcField, key, lastBuilt);
if (!transformedMarcField.equals(MarcField.EMPTY)) { if (!transformedMarcField.equals(MarcField.emptyMarcField())) {
if (op == MarcFieldTransformer.Operator.TAIL && list.size() > 0) { if (op == MarcFieldTransformer.Operator.TAIL && list.size() > 0) {
list.removeLast(); // tail operation means to nullify previous result list.removeLast(); // tail operation means to nullify previous result
} }

View file

@ -115,7 +115,7 @@ public class MarcFieldTransformerTest {
.build(); .build();
MarcField a = MarcField.builder().tag("001").subfield("a", "Hello").subfield("b", "World").build(); MarcField a = MarcField.builder().tag("001").subfield("a", "Hello").subfield("b", "World").build();
MarcField b = marcFieldTransformer.transform(a); MarcField b = marcFieldTransformer.transform(a);
assertEquals(MarcField.EMPTY, b); assertEquals(MarcField.emptyMarcField(), b);
} }
@Test @Test
@ -126,7 +126,7 @@ public class MarcFieldTransformerTest {
.build(); .build();
MarcField a = MarcField.builder().tag("001").subfield("a", "Hello").subfield("b", "World").build(); MarcField a = MarcField.builder().tag("001").subfield("a", "Hello").subfield("b", "World").build();
MarcField b = marcFieldTransformer.transform(a); MarcField b = marcFieldTransformer.transform(a);
assertEquals(MarcField.EMPTY, b); assertEquals(MarcField.emptyMarcField(), b);
} }
@Test @Test