Project

General

Profile

« Previous | Next » 

Revision 38362

utility methods and avoid NPEs

View differences:

modules/dnet-pace-core/trunk/src/main/java/eu/dnetlib/pace/model/gt/CoAuthor.java
1 1
package eu.dnetlib.pace.model.gt;
2 2

  
3
import org.apache.commons.lang.StringUtils;
4

  
3 5
import com.google.gson.Gson;
4 6

  
5 7
public class CoAuthor extends Author {
......
14 16
		super(author);
15 17
	}
16 18

  
19
	public boolean hasAnchorId() {
20
		return StringUtils.isNotBlank(getAnchorId());
21
	}
22

  
17 23
	public String getAnchorId() {
18 24
		return anchorId;
19 25
	}
modules/dnet-pace-core/trunk/src/main/java/eu/dnetlib/pace/model/gt/GTAuthorOafSerialiser.java
14 14

  
15 15
public class GTAuthorOafSerialiser implements JsonDeserializer<GTAuthor> {
16 16

  
17
	private static final String VALUE = "value";
18
	private static final String SECONDNAMES = "secondnames";
19
	private static final String FIRSTNAME = "firstname";
20
	private static final String FULLNAME = "fullname";
21
	private static final String ID = "id";
22
	private static final String MERGEDPERSON = "mergedperson";
23
	private static final String METADATA = "metadata";
24
	private static final String ANCHOR_ID = "anchorId";
25
	private static final String COAUTHOR = "coauthor";
26

  
17 27
	@Override
18 28
	public GTAuthor deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
19 29
		final GTAuthor gta = new GTAuthor();
......
27 37
	}
28 38

  
29 39
	private CoAuthors getCoAuthors(final JsonElement json) {
30
		return new CoAuthors(Lists.newArrayList(Iterables.transform(json.getAsJsonObject().get("coauthor").getAsJsonArray(),
40
		final JsonObject obj = json.getAsJsonObject();
41
		if (!obj.has(COAUTHOR)) return null;
42
		return new CoAuthors(Lists.newArrayList(Iterables.transform(obj.get(COAUTHOR).getAsJsonArray(),
31 43
				new Function<JsonElement, CoAuthor>() {
32 44

  
33 45
					@Override
34 46
					public CoAuthor apply(final JsonElement in) {
35 47
						final CoAuthor a = new CoAuthor(getAuthor(in));
36 48
						final JsonObject jsonObject = in.getAsJsonObject();
37
						if (jsonObject.has("anchorId")) {
38
							a.setAnchorId(jsonObject.get("anchorId").getAsString());
49
						if (jsonObject.has(ANCHOR_ID)) {
50
							a.setAnchorId(jsonObject.get(ANCHOR_ID).getAsString());
39 51
						}
40 52
						return a;
41 53
					}
......
50 62

  
51 63
		final JsonObject jso = json.getAsJsonObject();
52 64

  
53
		a.setId(jso.has("id") ? jso.get("id").getAsString() : null);
65
		a.setId(jso.has(ID) ? jso.get(ID).getAsString() : null);
54 66

  
55
		final JsonObject m = json.getAsJsonObject().get("metadata").getAsJsonObject();
56
		a.setFullname(getValue(m, "fullname"));
57
		a.setFirstname(getValue(m, "firstname"));
58
		a.setSecondnames(getValues(m, "secondnames"));
67
		final JsonObject jsonObject = json.getAsJsonObject();
68
		if (jsonObject.has(METADATA)) {
69
			final JsonObject m = jsonObject.get(METADATA).getAsJsonObject();
70
			a.setFullname(getValue(m, FULLNAME));
71
			a.setFirstname(getValue(m, FIRSTNAME));
72
			a.setSecondnames(getValues(m, SECONDNAMES));
73
		}
59 74
		return a;
60 75
	}
61 76

  
62 77
	private Authors getMerged(final JsonElement json) {
63
		return new Authors(Lists.newArrayList(Iterables.transform(json.getAsJsonObject().get("mergedperson").getAsJsonArray(),
78
		final JsonObject obj = json.getAsJsonObject();
79
		if (!obj.has(MERGEDPERSON)) return null;
80
		return new Authors(Lists.newArrayList(Iterables.transform(obj.get(MERGEDPERSON).getAsJsonArray(),
64 81
				new Function<JsonElement, Author>() {
65 82

  
66 83
					@Override
......
75 92

  
76 93
			@Override
77 94
			public String apply(final JsonElement in) {
78
				return in.getAsJsonObject().get("value").getAsString();
95
				return in.getAsJsonObject().get(VALUE).getAsString();
79 96
			}
80 97
		})) : null;
81 98
	}
82 99

  
83 100
	private String getValue(final JsonObject m, final String fieldName) {
84
		return m.has(fieldName) ? m.get(fieldName).getAsJsonObject().get("value").getAsString() : null;
101
		return m.has(fieldName) ? m.get(fieldName).getAsJsonObject().get(VALUE).getAsString() : null;
85 102
	}
86 103

  
87 104
}
modules/dnet-pace-core/trunk/src/main/java/eu/dnetlib/pace/model/gt/GTAuthor.java
26 26

  
27 27
	public GTAuthor(final String id, final Authors merged, final CoAuthors coAuthors, final boolean anchor) {
28 28
		super();
29

  
30
		if ((merged == null) || merged.isEmpty())
31
			throw new IllegalArgumentException("empty merged author set, id: " + id);
32

  
29 33
		this.author = pickAuthor(merged);
30 34
		this.id = id;
31 35
		this.merged = merged;
......
107 111
		this.author = author;
108 112
	}
109 113

  
114
	public boolean hasMerged() {
115
		return (getMerged() != null) && !getMerged().isEmpty();
116
	}
117

  
110 118
	public Authors getMerged() {
111 119
		return merged;
112 120
	}
......
115 123
		this.merged = merged;
116 124
	}
117 125

  
126
	public boolean hasCoAuthors() {
127
		return (getCoAuthors() != null) && !getCoAuthors().isEmpty();
128
	}
129

  
118 130
	public CoAuthors getCoAuthors() {
119 131
		return coAuthors;
120 132
	}
modules/dnet-pace-core/trunk/src/main/java/eu/dnetlib/pace/distance/PersonDistance.java
1 1
package eu.dnetlib.pace.distance;
2 2

  
3
import java.util.HashSet;
3 4
import java.util.Set;
4 5

  
5 6
import com.google.common.base.Function;
......
46 47
	}
47 48

  
48 49
	private Set<String> getAnchorIds(final CoAuthors ca) {
49
		return Sets.newHashSet(Iterables.filter(Iterables.transform(ca, new Function<CoAuthor, String>() {
50
		if ((ca == null) || ca.isEmpty()) return new HashSet<String>();
51
		final Iterable<String> anchorIds = Iterables.transform(ca, new Function<CoAuthor, String>() {
50 52

  
51 53
			@Override
52 54
			public String apply(final CoAuthor c) {
53 55
				return c.getAnchorId();
54 56
			}
55
		}), Predicates.notNull()));
57
		});
58
		final Iterable<String> filtered = Iterables.filter(anchorIds, Predicates.notNull());
59
		return Sets.newHashSet(filtered);
56 60
	}
57 61

  
58 62
	private Set<String> getSurnames(final CoAuthors ca) {
63
		if ((ca == null) || ca.isEmpty()) return new HashSet<String>();
59 64
		return Sets.newHashSet(Iterables.filter(Iterables.transform(ca, new Function<CoAuthor, String>() {
60 65

  
61 66
			@Override

Also available in: Unified diff