Skip to content

Commit

Permalink
Improve behaviour and Javadoc of PreambleEpilogueAdder
Browse files Browse the repository at this point in the history
The epilogue string is now only emitted if an object was received. The
preamble is only emitted if it is not empty (as it was already the case
with the epilogue string).

After resetting the stream a new preamble string is emitted when the
next object is received.
  • Loading branch information
cboehme committed Jul 8, 2016
1 parent eab074a commit 1494cbe
Showing 1 changed file with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright 2016 Christoph Böhme
* Copyright 2013, 2014 Deutsche Nationalbibliothek
*
* Licensed under the Apache License, Version 2.0 the "License";
Expand All @@ -24,9 +25,15 @@


/**
* Adds a String preamle and/or epilogue to the stream
* Emits a <i>preamble</i> string before the first string object in the
* stream and an <i>epilogue</i> string before the end of the stream.
* <p>
* The preamble and epilogue strings are only emitted if an object is received.
* If the preamble or epilogue string is empty, the respective string is not
* emitted.
*
* @author Markus Geipel
* @author Christoph Böhme
*
*/
@Description("Adds a String preamle and/or epilogue to the stream")
Expand All @@ -37,29 +44,66 @@ public final class PreambleEpilogueAdder extends DefaultObjectPipe<String, Objec

private String preamble = "";
private String epilogue = "";
private boolean init = true;

private boolean objectsReceived = false;

/**
* Sets the <i>preamble</i> string which is emitted before the first object.
* <p>
* The default preamble is an empty string. That means by default no
* preamble is emitted.
* <p>
* The parameter may only be changed before the first object is processed
* otherwise the change has no effect.
*
* @param preamble the preamble string
*/
public void setPreamble(final String preamble) {
this.preamble = preamble;
}

public String getPreamble() {
return preamble;
}

/**
* Sets the <i>epilogue</i> string which is emitted after the last object.
* <p>
* The default epilogue string is an empty string. That means by default no
* epilogue is emitted.
* <p>
* The parameter may be changed at any time. Its becomes effective when a
* <i>close-stream</i> event is received.
*
* @param epilogue the epilogue string
*/
public void setEpilogue(final String epilogue) {
this.epilogue = epilogue;
}

public void setPreamble(final String preamble) {
this.preamble = preamble;
public String getEpilogue() {
return epilogue;
}

@Override
public void process(final String obj) {
if(init){
if(!objectsReceived && !preamble.isEmpty()) {
getReceiver().process(preamble);
init = false;
}
objectsReceived = true;
getReceiver().process(obj);
}

@Override
protected void onCloseStream() {
if(!epilogue.isEmpty()){
if(objectsReceived && !epilogue.isEmpty()) {
getReceiver().process(epilogue);
}
}

@Override
protected void onResetStream() {
objectsReceived = false;
}

}

0 comments on commit 1494cbe

Please sign in to comment.