Skip to content

Commit

Permalink
Avoid NPEs when reading data
Browse files Browse the repository at this point in the history
Flips all the equals() calls to a `CONSTANT.equals(obj)` pattern to avoid
NullPointerExceptions if `obj` happens to be Null.
  • Loading branch information
claussni committed Jan 17, 2017
1 parent 5e9aea0 commit 8ede28c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public List<Datastream> getAugmentedFileDatastreams() throws SWORDException {
final Element fLocat = validateAndReturn("FLocat element", fileElement.getChild("FLocat", Namespaces.METS));
final String href = validateAndReturn("file content URL", fLocat.getAttributeValue("href", Namespaces.XLINK));
final URI uri = new URI(href);
final boolean isFile = uri.getScheme().equals("file");
final boolean isFile = "file".equals(uri.getScheme());
final String mimetype = validateAndReturn("mime type", fileElement.getAttributeValue("MIMETYPE"));
ds = buildDatastream(id, fileElement, href, mimetype, isFile);
} else {
Expand Down Expand Up @@ -177,8 +177,8 @@ public List<File> getTemporayFiles() throws SWORDException {
final Element fLocat = validateAndReturn("FLocat element", e.getChild("FLocat", Namespaces.METS));
final String href = validateAndReturn("file content URL", fLocat.getAttributeValue("href", Namespaces.XLINK));
final URI uri = new URI(href);
final boolean isFile = uri.getScheme().equals("file");
final boolean isTemporary = emptyIfNull(fLocat.getAttributeValue("USE")).equals("TEMPORARY");
final boolean isFile = "file".equals(uri.getScheme());
final boolean isTemporary = "TEMPORARY".equals(emptyIfNull(fLocat.getAttributeValue("USE")));
if (isFile && isTemporary) {
filesMarkedForRemoval.add(new File(uri));
}
Expand Down Expand Up @@ -293,7 +293,7 @@ private <E> E validateAndReturn(String description, E value) throws SWORDExcepti
}

private boolean isADeleteRequest(Element e) {
return e.getAttributeValue("USE") != null && e.getAttributeValue("USE").equals("DELETE");
return e.getAttributeValue("USE") != null && "DELETE".equals(e.getAttributeValue("USE"));
}

private String emptyIfNull(String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void validateObject(FedoraObject fedoraObject) throws SWORDException {
// ensure there is a MODS datastream
boolean modsExists = false;
for (Datastream ds : fedoraObject.getDatastreams()) {
if (ds.getId().equals("MODS")) {
if ("MODS".equals(ds.getId())) {
modsExists = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void datastreamGetsAdded() throws Exception {
CollectionUtils.find(argument.getAllValues(), new Predicate() {
@Override
public boolean evaluate(Object o) {
return ((Datastream) o).getId().equals("ATT-2");
return "ATT-2".equals(((Datastream) o).getId());
}
}));
}
Expand Down

0 comments on commit 8ede28c

Please sign in to comment.