Bits of documentation and cleanup for open sourcing
diff --git a/CHANGES b/CHANGES
index 533c0ae..781a854 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,7 @@
-0.49.1 2014-11-16
- - Major refactoring for new response messages (diewald)
+0.49.1 2014-11-25
+ - [cleanup] Major refactoring for new response messages (diewald)
+ - [cleanup] Removed deprecated error messaging (diewald)
+ - [documentation] Improved documentation for notifications (diewald)
0.49 2014-11-14
- This is a major version (prepared for the Evaluation Meeting)
diff --git a/pom.xml b/pom.xml
index e0742e1..9dc1e35 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,14 +14,6 @@
$mvn clean compile exec:java
-->
- <!--
- <parent>
- <groupId>KorAP-modules</groupId>
- <artifactId>KorAP-core-modules</artifactId>
- <version>LATEST</version>
- </parent>
- -->
-
<groupId>KorAP-modules</groupId>
<artifactId>KorAP-lucene-index</artifactId>
<version>0.49.1</version>
@@ -41,12 +33,10 @@
<email>diewald@ids-mannheim.de</email>
<url>http://nils-diewald.de</url>
</developer>
-
<developer>
<name>Eliza Margaretha</name>
<email>margaretha@ids-mannheim.de</email>
</developer>
-
</developers>
<properties>
@@ -151,13 +141,13 @@
<version>1.13-b01</version>
</dependency>
- <!-- JSON support -->
+ <!-- JSON support in Jersey -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
- <!-- Jackson -->
+ <!-- JSON support using Jackson -->
<!-- see https://github.com/FasterXML/jackson-core -->
<!-- https://github.com/FasterXML/jackson-databind -->
<dependency>
@@ -239,6 +229,26 @@
</configuration>
</plugin>
+ <!-- Formatter plugin for Eclipse based coding conventions -->
+ <!--
+ http://maven-java-formatter-plugin.googlecode.com/svn/site/0.4/usage.html
+ Call: mvn java-formatter:format
+ -->
+ <plugin>
+ <groupId>com.googlecode.maven-java-formatter-plugin</groupId>
+ <artifactId>maven-java-formatter-plugin</artifactId>
+ <version>0.4</version>
+ <!--
+ <executions>
+ <execution>
+ <goals>
+ <goal>format</goal>
+ </goals>
+ </execution>
+ </executions>
+ -->
+ </plugin>
+
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
diff --git a/src/main/java/de/ids_mannheim/korap/KorapResult.java b/src/main/java/de/ids_mannheim/korap/KorapResult.java
index 9b00870..9ac2aef 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapResult.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapResult.java
@@ -50,7 +50,6 @@
private JsonNode request;
- private boolean timeExceeded = false;
// Logger
// This is KorapMatch instead of KorapResult!
@@ -176,16 +175,6 @@
return this.itemsPerResource;
};
- public void setTimeExceeded (boolean timeout) {
- if (timeout)
- this.addWarning(682, "Search time exceeded");
- this.timeExceeded = timeout;
- };
-
- public boolean getTimeExceeded () {
- return this.timeExceeded;
- };
-
public String getQuery () {
return this.query;
};
@@ -249,9 +238,6 @@
json.put("totalResults", this.getTotalResults());
- if (this.timeExceeded)
- json.put("timeExceeded", this.timeExceeded);
-
// Add matches
if (this.matches != null)
json.putPOJO("matches", this.getMatches());
diff --git a/src/main/java/de/ids_mannheim/korap/response/KorapResponse.java b/src/main/java/de/ids_mannheim/korap/response/KorapResponse.java
index 6f43e32..40a75aa 100644
--- a/src/main/java/de/ids_mannheim/korap/response/KorapResponse.java
+++ b/src/main/java/de/ids_mannheim/korap/response/KorapResponse.java
@@ -9,23 +9,41 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import de.ids_mannheim.korap.response.Notifications;
+/**
+ * Base class for objects meant to be responded by the server.
+ *
+ * <p>
+ * <blockquote><pre>
+ * KorapResponse km = new KorapResponse();
+ * System.out.println(
+ * km.toJSON()
+ * );
+ * </pre></blockquote>
+ *
+ * @author Nils Diewald
+ * @see de.ids_mannheim.korap.response.Notifications
+ */
public class KorapResponse extends Notifications {
ObjectMapper mapper = new ObjectMapper();
- private String
- version,
- name,
- node,
- listener;
+ // TODO: Add timeout!!!
+
+ private String version, name, node, listener;
private String benchmark;
+ private boolean timeExceeded = false;
- // add timeout!!!
- // remove totalResults, totalTexts
+ /**
+ * Construct a new KorapResponse object.
+ *
+ * @return The new KorapResponse object
+ */
public KorapResponse () {};
/**
- * Get version of the index
+ * Get string representation of the backend's version.
+ *
+ * @return String representation of the backend's version
*/
@JsonIgnore
public String getVersion () {
@@ -35,18 +53,22 @@
};
/**
- * Set version number.
+ * Set the string representation of the backend's version.
*
- * @param version The version number of the index as
- * a string representation.
+ * @param version The string representation of the backend's version
+ * @return KorapResponse object for chaining
*/
@JsonIgnore
- public void setVersion (String version) {
+ public KorapResponse setVersion (String version) {
this.version = version;
+ return this;
};
/**
- * Get name the index
+ * Get string representation of the backend's name.
+ * All nodes in a cluster should have the same backend name.
+ *
+ * @return String representation of the backend's name
*/
@JsonIgnore
public String getName () {
@@ -56,27 +78,57 @@
};
/**
- * Set name.
+ * Set the string representation of the backend's name.
+ * All nodes in a cluster should have the same backend name.
*
- * @param name The name of the index as
- * a string representation.
+ * @param version The string representation of the backend's name
+ * @return KorapResponse object for chaining
*/
@JsonIgnore
- public void setName (String name) {
+ public KorapResponse setName (String name) {
this.name = name;
+ return this;
};
+ /**
+ * Get string representation of the node's name.
+ * Each node in a cluster has a unique name.
+ *
+ * @return String representation of the node's name
+ */
@JsonIgnore
public String getNode () {
return this.node;
};
+ /**
+ * Set the string representation of the node's name.
+ * Each node in a cluster has a unique name.
+ *
+ * @param version The string representation of the node's name
+ * @return KorapResponse object for chaining
+ */
@JsonIgnore
public KorapResponse setNode (String name) {
this.node = name;
return this;
};
+
+ @JsonIgnore
+ public void setTimeExceeded (boolean timeout) {
+ if (timeout)
+ this.addWarning(682, "Search time exceeded");
+ this.timeExceeded = timeout;
+ };
+
+
+ @JsonIgnore
+ public boolean getTimeExceeded () {
+ return this.timeExceeded;
+ };
+
+
@JsonIgnore
public KorapResponse setBenchmark (long t1, long t2) {
this.benchmark =
@@ -130,6 +182,9 @@
sb.append(this.getVersion());
};
+ if (this.timeExceeded)
+ json.put("timeExceeded", true);
+
if (sb.length() > 0)
json.put("version", sb.toString());
diff --git a/src/main/java/de/ids_mannheim/korap/response/Message.java b/src/main/java/de/ids_mannheim/korap/response/Message.java
index 4f7cd29..1fb1709 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Message.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Message.java
@@ -7,6 +7,20 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.*;
+/**
+ * A message for Notifications.
+ *
+ * <p>
+ * <blockquote><pre>
+ * Message m = new Message();
+ * m.setCode(614);
+ * m.setMessage("This is a new message");
+ * m.addParameter("MyClass");
+ * </pre></blockquote>
+ *
+ * @author Nils Diewald
+ * @see de.ids_mannheim.korap.response.Messages
+ */
public class Message implements Cloneable {
// Mapper for JSON serialization
ObjectMapper mapper = new ObjectMapper();
@@ -15,47 +29,99 @@
private int code = 0;
private LinkedList<String> parameters;
+ /**
+ * Construct a new message object.
+ *
+ * @param code Code number representing the message code
+ * @param msg String representation of the message
+ * @return The new message object
+ */
public Message (int code, String msg) {
this.code = code;
this.msg = msg;
};
+ /**
+ * Construct a new message object.
+ *
+ * @return The new empty message object
+ */
public Message () {};
+ /**
+ * Set the string representation of the message.
+ *
+ * @param msg String representation of the message
+ * @return Message object for chaining
+ */
@JsonIgnore
- public void setMessage (String msg) {
+ public Message setMessage (String msg) {
this.msg = msg;
+ return this;
};
+ /**
+ * Return the string representation of the message.
+ *
+ * @return String representation of the message
+ */
@JsonIgnore
public String getMessage () {
return this.msg;
};
+ /**
+ * Set the integer representation of the message.
+ *
+ * @param code Integer code representation of the message
+ * @return Message object for chaining
+ */
@JsonIgnore
- public void setCode (int code) {
+ public Message setCode (int code) {
this.code = code;
+ return this;
};
+ /**
+ * Return the integer code representation of the message.
+ *
+ * @return Integer code representation of the message
+ */
@JsonIgnore
public int getCode () {
return this.code;
};
- public void addParameter (String param) {
+
+ /**
+ * Add additional string parameters to the message.
+ *
+ * @return Message object for chaining
+ */
+ public Message addParameter (String param) {
if (this.parameters == null)
this.parameters = new LinkedList<String>();
this.parameters.add(param);
+ return this;
};
- public Object clone () throws CloneNotSupportedException
- {
+ /**
+ * Create a clone of the Message.
+ *
+ * @return The cloned message object
+ * @throws CloneNotSupportedException if message can't be cloned
+ */
+ public Object clone () throws CloneNotSupportedException {
Message clone = new Message();
+
+ // Copy message string
if (this.msg != null)
clone.msg = this.msg;
+ // Copy message code
clone.code = this.code;
+ // Copy parameters
if (this.parameters != null) {
for (String p : this.parameters) {
clone.addParameter(p);
@@ -65,7 +131,11 @@
return clone;
};
-
+ /**
+ * Serialize Message as a JsonNode.
+ *
+ * @return JsonNode representation of the message
+ */
public JsonNode toJSONnode () {
ArrayNode message = mapper.createArrayNode();
@@ -79,8 +149,15 @@
return (JsonNode) message;
};
+
/**
- * Get JSON string
+ * Serialize Message as a JSON string.
+ * <p>
+ * <blockquote><pre>
+ * [123, "You are not allowed to serialize these messages"]
+ * </pre></blockquote>
+ *
+ * @return String representation of the message
*/
public String toJSON () {
String msg = "";
@@ -88,9 +165,9 @@
return mapper.writeValueAsString(this.toJSONnode());
}
catch (Exception e) {
+ // Bad in case the message contains quotes!
msg = ", \"" + e.getLocalizedMessage() + "\"";
};
-
return
"[620, " +
"\"Unable to generate JSON\"" + msg + "]";
diff --git a/src/main/java/de/ids_mannheim/korap/response/Messages.java b/src/main/java/de/ids_mannheim/korap/response/Messages.java
index 1af8614..2239842 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Messages.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Messages.java
@@ -8,26 +8,78 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.*;
-import java.util.ArrayList;
-import java.util.List;
+import java.lang.*;
+import java.util.*;
/**
- * @author Nils Diewald
+ * A list of messages for Notifications.
*
- * An array of messages.
+ * <p>
+ * <blockquote><pre>
+ * Messages m = new Messages();
+ * m.add(614, "This is a new message");
+ * </pre></blockquote>
+ *
+ * @author Nils Diewald
+ * @see de.ids_mannheim.korap.response.Notifications
+ * @see de.ids_mannheim.korap.response.Message
*/
-public class Messages implements Cloneable {
+public class Messages implements Cloneable, Iterable<Message> {
- // Mapper for JSON serialization
+ // Create object mapper for JSON generation
ObjectMapper mapper = new ObjectMapper();
+
+ // List of messages
private ArrayList<Message> messages;
+ // Private class for iterator implementation
+ private class MessageIterator implements Iterator<Message> {
+ int index;
+
+ // Constructor
+ public MessageIterator () {
+ this.index = 0;
+ };
+
+ @Override
+ public boolean hasNext () {
+ return this.index < messages.size();
+ };
+
+ @Override
+ public Message next () {
+ return messages.get(this.index++);
+ };
+
+ @Override
+ public void remove () {
+ messages.remove(this.index);
+ };
+ };
+
+ /**
+ * Construct a new Messages object.
+ */
public Messages () {
this.messages = new ArrayList<Message>(3);
};
/**
- * Add message
+ * Get the iterator object.
+ *
+ * @return Iterator for Message object.
+ */
+ public Iterator<Message> iterator() {
+ return new MessageIterator();
+ };
+
+ /**
+ * Append a new message.
+ *
+ * @param code Integer code representation of the warning
+ * @param msg String representation of the warning
+ * @param terms Optional strings of additional information
+ * @return New Message object
*/
public Message add (int code,
String message,
@@ -41,15 +93,28 @@
};
/**
- * Add message
+ * Append an existing message.
+ *
+ * @param msg Message object to be added. Message will be cloned.
+ * @return Cloned Message object
*/
public Message add (Message msg) {
- messages.add(msg);
- return msg;
+ try {
+ Message msgClone = (Message) msg.clone();
+ messages.add(msgClone);
+ return msgClone;
+ }
+ catch (CloneNotSupportedException e) {
+ };
+ return (Message) null;
};
/**
- * Add message usng JsonNode
+ * Append an existing message comming from a JsonNode.
+ *
+ * @param node <code>JsonNode</code> representing a message
+ * @return New Message object
+ * @throws QueryException if notification is not well formed (Error 750)
*/
public Message add (JsonNode msg) throws QueryException {
if (!msg.isArray() || !msg.has(0))
@@ -84,37 +149,72 @@
/**
- * Add messages
+ * Append existing messages.
+ *
+ * @param msgs Messages object to be added. Messages will be cloned.
+ * @return Messages object for chaining.
*/
- public void add (Messages msgs) {
- for (Message msg : msgs.getMessages()) {
- this.add(msg);
+ public Messages add (Messages msgs) {
+ try {
+ for (Message msg : msgs.getMessages())
+ this.add((Message) msg.clone());
+ }
+ catch (CloneNotSupportedException e) {
};
+ return this;
};
/**
- * Clear all messages
+ * Clear all messages.
+ *
+ * @return Messages object for chaining
*/
- public void clear () {
- messages.clear();
+ public Messages clear () {
+ this.messages.clear();
+ return this;
};
+ /**
+ * Get the number of the messages.
+ *
+ * @param Integer representing the number of messages in the list.
+ */
public int size () {
return this.messages.size();
};
+
+ /**
+ * Return a specific message based on an index.
+ *
+ * @param index The index of the message in the list of messages.
+ * @return The message in case it exists, otherwise <code>null</code>
+ */
@JsonIgnore
public Message get (int index) {
+ if (index >= this.size())
+ return (Message) null;
return this.messages.get(index);
};
+ /**
+ * Return all messages.
+ *
+ * @return List of all Message objects
+ */
@JsonIgnore
public List<Message> getMessages () {
return this.messages;
};
- public Object clone () throws CloneNotSupportedException
- {
+
+ /**
+ * Create a clone of the Messages.
+ *
+ * @return The cloned messages object
+ * @throws CloneNotSupportedException if messages can't be cloned
+ */
+ public Object clone () throws CloneNotSupportedException {
Messages clone = new Messages();
for (Message m : this.messages) {
clone.add((Message) m.clone());
@@ -124,7 +224,9 @@
};
/**
- * Get JSON node
+ * Serialize Messages as a JsonNode.
+ *
+ * @return JsonNode representation of all messages
*/
public JsonNode toJSONnode () {
ArrayNode messageArray = mapper.createArrayNode();
@@ -133,8 +235,18 @@
return (JsonNode) messageArray;
};
+
/**
- * Get JSON string
+ * Serialize Messages as a JSON string.
+ * <p>
+ * <blockquote><pre>
+ * [
+ * [123, "You are not allowed to serialize these messages"],
+ * [124, "Your request was invalid"]
+ * ]
+ * </pre></blockquote>
+ *
+ * @return String representation of all messages
*/
public String toJSON () {
String msg = "";
@@ -142,6 +254,7 @@
return mapper.writeValueAsString(this.toJSONnode());
}
catch (Exception e) {
+ // Bad in case the message contains quotes!
msg = ", \"" + e.getLocalizedMessage() + "\"";
};
diff --git a/src/main/java/de/ids_mannheim/korap/response/Notifications.java b/src/main/java/de/ids_mannheim/korap/response/Notifications.java
index 361fb9a..4b985f7 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Notifications.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Notifications.java
@@ -13,12 +13,31 @@
import de.ids_mannheim.korap.util.QueryException;
/**
- * Unified notification class for KorAP related errors,
+ * A unified notification class for KorAP related errors,
* warnings and messages.
*
+ * <p>
+ * The object contains lists of errors, warnings and messages
+ * and new warnings, errors or messages are appended to these lists.
+ *
+ * <p>
+ * <blockquote><pre>
+ * Notifications n = new Notifications();
+ * n.addWarning(456, "Something went wrong");
+ * if (n.hasWarnings()) {
+ * for (Message msg : n.getWarnings())
+ * System.err.out(msg.getCode() + ": " + msg.getMessage());
+ * };
+ * System.err.println(n.toJSON());
+ * </pre></blockquote>
+ *
* @author Nils Diewald
+ * @see de.ids_mannheim.korap.response.Messages
*/
-
+/*
+ * This will be inherited most of the time as Java does not support roles
+ * and I have no idea how to do this more elegantly.
+ */
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Notifications {
@@ -31,50 +50,70 @@
messages;
/**
- * Add a new warning.
+ * Appends a new warning.
*
- * @param code Integer code representation of the warning
- * @param msg String representation of the warning
+ * @param code Integer code representation of the warning
+ * @param msg String representation of the warning
+ * @param terms Optional strings of additional information
+ * @return Notification object for chaining
*/
- public void addWarning (int code, String msg, String ... terms) {
+ public Notifications addWarning (int code, String msg, String ... terms) {
if (this.warnings == null)
this.warnings = new Messages();
this.warnings.add(code, msg, terms);
+ return this;
};
- public void addWarning (JsonNode msg) {
+ /**
+ * Appends a new warning.
+ *
+ * @param node <code>JsonNode</code> representing a warning message
+ * @return Notification object for chaining
+ */
+ public Notifications addWarning (JsonNode node) {
if (this.warnings == null)
this.warnings = new Messages();
try {
- this.warnings.add(msg);
+ this.warnings.add(node);
}
catch (QueryException qe) {
this.warnings.add(qe.getErrorCode(), qe.getMessage());
};
+
+ return this;
};
/**
- * Add new warnings.
+ * Appends new warnings.
*
- * @param msgs Array representation of the warning
+ * @param msgs <code>Messages</code> representing multiple warnings
+ * @return Notification object for chaining
*/
- public void addWarnings (Messages msgs) {
+ public Notifications addWarnings (Messages msgs) {
if (this.warnings == null)
this.warnings = msgs;
else
this.warnings.add(msgs);
+ return this;
};
/**
- * Get all warnings.
+ * Return all warnings.
+ *
+ * @return The <code>Messages</code> object representing all warnings
*/
public Messages getWarnings () {
return this.warnings;
};
-
+ /**
+ * Return a specific warning based on an index.
+ *
+ * @param index The index of the warning in the list of warnings.
+ * @return The message in case it exists, otherwise <code>null</code>
+ */
public Message getWarning (int index) {
if (this.warnings != null)
return this.warnings.get(index);
@@ -84,6 +123,8 @@
/**
* Check for warnings.
+ *
+ * @return <tt>true</tt> in case there are warnings, otherwise <tt>false</tt>
*/
public boolean hasWarnings () {
if (this.warnings == null || this.warnings.size() == 0)
@@ -92,18 +133,27 @@
};
/**
- * Add a new error.
+ * Appends a new error.
*
- * @param code Integer code representation of the error
- * @param msg String representation of the error
+ * @param code Integer code representation of the error
+ * @param msg String representation of the error
+ * @param terms Optional strings of additional information
+ * @return Notification object for chaining
*/
- public void addError (int code, String msg, String ... terms) {
+ public Notifications addError (int code, String msg, String ... terms) {
if (this.errors == null)
this.errors = new Messages();
this.errors.add(code, msg, terms);
+ return this;
};
- public void addError (JsonNode msg) {
+ /**
+ * Appends a new error.
+ *
+ * @param node <code>JsonNode</code> representing an error message
+ * @return Notification object for chaining
+ */
+ public Notifications addError (JsonNode msg) {
if (this.errors == null)
this.errors = new Messages();
try {
@@ -112,37 +162,54 @@
catch (QueryException qe) {
this.errors.add(qe.getErrorCode(), qe.getMessage());
};
+
+ return this;
};
/**
- * Add new warnings.
+ * Appends new errors.
*
- * @param msgs Array representation of the warning
+ * @param msgs <code>Messages</code> representing multiple errors
+ * @return Notification object for chaining
*/
- public void addErrors (Messages msgs) {
+ public Notifications addErrors (Messages msgs) {
if (this.errors == null)
this.errors = msgs;
else
this.errors.add(msgs);
+ return this;
};
+
/**
- * Get all errors.
+ * Return all errors.
+ *
+ * @return The <code>Messages</code> object representing all errors
*/
public Messages getErrors () {
return this.errors;
};
+
+ /**
+ * Return a specific error based on an index.
+ *
+ * @param index The index of the error in the list of errors.
+ * @return The message in case it exists, otherwise <code>null</code>
+ */
public Message getError (int index) {
if (this.errors != null)
return this.errors.get(index);
return (Message) null;
};
+
/**
* Check for errors.
+ *
+ * @return <tt>true</tt> in case there are errors, otherwise <tt>false</tt>
*/
public boolean hasErrors () {
if (this.errors == null || this.errors.size() == 0)
@@ -152,18 +219,27 @@
/**
- * Add a new message.
+ * Appends a new message.
*
- * @param code Integer code representation of the message
- * @param msg String representation of the message
+ * @param code Integer code representation of the message
+ * @param msg String representation of the message
+ * @param terms Optional strings of additional information
+ * @return Notification object for chaining
*/
- public void addMessage (int code, String msg, String ... terms) {
+ public Notifications addMessage (int code, String msg, String ... terms) {
if (this.messages == null)
this.messages = new Messages();
this.messages.add(code, msg, terms);
+ return this;
};
- public void addMessage (JsonNode msg) {
+ /**
+ * Appends a new message.
+ *
+ * @param node <code>JsonNode</code> representing a message
+ * @return Notification object for chaining
+ */
+ public Notifications addMessage (JsonNode msg) {
if (this.messages == null)
this.messages = new Messages();
try {
@@ -172,28 +248,40 @@
catch (QueryException qe) {
this.messages.add(qe.getErrorCode(), qe.getMessage());
};
+ return this;
};
+
/**
- * Add new warnings.
+ * Appends new messages.
*
- * @param msgs Array representation of the warning
+ * @param msgs <code>Messages</code> representing multiple messages
+ * @return Notification object for chaining
*/
- public void addMessages (Messages msgs) {
+ public Notifications addMessages (Messages msgs) {
if (this.messages == null)
this.messages = msgs;
else
this.messages.add(msgs);
+ return this;
};
/**
- * Get all messages.
+ * Return all messages.
+ *
+ * @return The <code>Messages</code> object representing all messages
*/
public Messages getMessages () {
return this.messages;
};
+ /**
+ * Return a specific message based on an index.
+ *
+ * @param index The index of the message in the list of messages.
+ * @return The message in case it exists, otherwise <code>null</code>
+ */
public Message getMessage (int index) {
if (this.messages != null)
return this.messages.get(index);
@@ -203,6 +291,8 @@
/**
* Check for messages.
+ *
+ * @return <tt>true</tt> in case there are messages, otherwise <tt>false</tt>
*/
public boolean hasMessages () {
if (this.messages == null || this.messages.size() == 0)
@@ -212,9 +302,12 @@
/**
- * Copy notifications
+ * Copy notifications from one notification object.
+ *
+ * @param notes Notification object to copy notifications from.
+ * @return Notification object for chaining
*/
- public void copyNotificationsFrom (Notifications notes) {
+ public Notifications copyNotificationsFrom (Notifications notes) {
try {
if (notes.hasErrors())
this.addErrors((Messages) notes.getErrors().clone());
@@ -225,14 +318,17 @@
}
catch (CloneNotSupportedException cnse) {
};
- return;
+ return this;
};
/**
- * Copy notifications from JsonNode
+ * Copy notifications from a JsonNode object.
+ *
+ * @param request Notifications containing JsonNode.
+ * @return Notification object for chaining
*/
- public void copyNotificationsFrom (JsonNode request) {
+ public Notifications copyNotificationsFrom (JsonNode request) {
// Add warnings from JSON
if (request.has("warnings") &&
@@ -259,11 +355,15 @@
for (JsonNode msg : msgs)
this.addError(msg);
};
+
+ return this;
};
/**
- * Serialize response to JSON node.
+ * Serialize Notifications as a JsonNode.
+ *
+ * @return JsonNode representation of all warnings, errors, and messages
*/
public JsonNode toJSONnode () {
ObjectNode json = mapper.createObjectNode();
@@ -280,7 +380,21 @@
};
/**
- * Serialize response to JSON string.
+ * Serialize Notifications as a JSON string.
+ * <p>
+ * <blockquote><pre>
+ * {
+ * "errors": [
+ * [123, "You are not allowed to serialize these messages"],
+ * [124, "Your request was invalid"]
+ * ],
+ * "messages" : [
+ * [125, "Class is deprecated", "Notifications"]
+ * ]
+ * }
+ * </pre></blockquote>
+ *
+ * @return String representation of all warnings, errors, and messages
*/
public String toJSON () {
String msg = "";
@@ -291,6 +405,7 @@
return mapper.writeValueAsString(node);
}
catch (Exception e) {
+ // Bad in case the message contains quotes!
msg = ", \"" + e.getLocalizedMessage() + "\"";
};
@@ -303,60 +418,16 @@
/**
* Clear all notifications.
+ *
+ * @return Notification object for chaining
*/
- public void clearNotifications () {
+ public Notifications clearNotifications () {
if (this.warnings != null)
this.warnings.clear();
if (this.messages != null)
this.messages.clear();
if (this.errors != null)
this.errors.clear();
- };
-
-
- // Remove:
- @Deprecated
- public void addError (String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void addWarning (String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void addMessage (String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void setError (String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void setWarning (String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void setMessage (String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void setError (int code, String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void setWarning (int code, String msg) {
- System.err.println("DEPRECATED " + msg);
- };
-
- @Deprecated
- public void setMessage (int code, String msg) {
- System.err.println("DEPRECATED " + msg);
+ return this;
};
};
diff --git a/src/test/java/de/ids_mannheim/korap/response/TestMessage.java b/src/test/java/de/ids_mannheim/korap/response/TestMessage.java
index 6ecdf73..440e027 100644
--- a/src/test/java/de/ids_mannheim/korap/response/TestMessage.java
+++ b/src/test/java/de/ids_mannheim/korap/response/TestMessage.java
@@ -1,5 +1,6 @@
package de.ids_mannheim.korap.response;
+import de.ids_mannheim.korap.response.Message;
import de.ids_mannheim.korap.response.Messages;
import static org.junit.Assert.*;
@@ -36,4 +37,17 @@
km.add(614,"Test");
assertEquals("[[612,\"Foo\"],[613,\"Bar\",\"Instanz\"],[614,\"Test\"]]", km.toJSON());
};
+
+ @Test
+ public void CheckIterability () {
+ Messages km = new Messages();
+ km.add(612,"Foo");
+ km.add(613,"Bar", "Instanz");
+ km.add(614,"Test");
+ String test = "";
+ for (Message msg : km)
+ test += msg.getCode();
+
+ assertEquals(test, "612613614");
+ };
};