1
|
package eu.dnetlib.pace.model.gt;
|
2
|
|
3
|
import java.util.Collections;
|
4
|
import java.util.Comparator;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
|
8
|
import com.google.common.base.Function;
|
9
|
import com.google.common.collect.ComparisonChain;
|
10
|
import com.google.common.collect.Iterables;
|
11
|
import com.google.common.collect.Lists;
|
12
|
import com.google.common.collect.Maps;
|
13
|
import com.google.common.collect.Ordering;
|
14
|
import com.google.gson.Gson;
|
15
|
import com.google.gson.GsonBuilder;
|
16
|
|
17
|
public class GTAuthor implements Comparable<GTAuthor> {
|
18
|
|
19
|
private String id;
|
20
|
private Author author;
|
21
|
private Authors merged;
|
22
|
private CoAuthors coAuthors;
|
23
|
private boolean anchor;
|
24
|
|
25
|
public GTAuthor() {}
|
26
|
|
27
|
public GTAuthor(final String id, final Authors merged, final CoAuthors coAuthors, final boolean anchor) {
|
28
|
super();
|
29
|
|
30
|
if ((merged == null) || merged.isEmpty())
|
31
|
throw new IllegalArgumentException("empty merged author set, id: " + id);
|
32
|
|
33
|
this.author = pickAuthor(merged);
|
34
|
this.id = id;
|
35
|
this.merged = merged;
|
36
|
this.coAuthors = coAuthors;
|
37
|
this.anchor = anchor;
|
38
|
}
|
39
|
|
40
|
class AuthorFrequency extends Author {
|
41
|
|
42
|
private Integer frequency = new Integer(1);
|
43
|
|
44
|
public AuthorFrequency(final Author a) {
|
45
|
super(a);
|
46
|
}
|
47
|
|
48
|
public void increment() {
|
49
|
setFrequency(getFrequency() + 1);
|
50
|
}
|
51
|
|
52
|
public Integer getFrequency() {
|
53
|
return frequency;
|
54
|
}
|
55
|
|
56
|
public void setFrequency(final Integer frequency) {
|
57
|
this.frequency = frequency;
|
58
|
}
|
59
|
}
|
60
|
|
61
|
private Author pickAuthor(final Authors merged) {
|
62
|
final List<AuthorFrequency> freq = getFrequencies(merged);
|
63
|
Collections.sort(freq, Collections.reverseOrder(new Comparator<AuthorFrequency>() {
|
64
|
|
65
|
@Override
|
66
|
public int compare(final AuthorFrequency o1, final AuthorFrequency o2) {
|
67
|
return ComparisonChain.start().compare(o1.getFullname().length(), o2.getFullname().length()).compare(o1.getFrequency(), o2.getFrequency())
|
68
|
.result();
|
69
|
}
|
70
|
}));
|
71
|
|
72
|
return Iterables.getFirst(freq, null);
|
73
|
}
|
74
|
|
75
|
private List<AuthorFrequency> getFrequencies(final Authors merged) {
|
76
|
final Map<String, Integer> countMap = Maps.newHashMap();
|
77
|
for (final Author a : merged) {
|
78
|
final Integer count = countMap.get(a.getFullname());
|
79
|
if (count == null) {
|
80
|
countMap.put(a.getFullname(), new Integer(1));
|
81
|
} else {
|
82
|
countMap.put(a.getFullname(), count + 1);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
return Lists.newArrayList(Iterables.transform(merged, new Function<Author, AuthorFrequency>() {
|
87
|
|
88
|
@Override
|
89
|
public AuthorFrequency apply(final Author a) {
|
90
|
final AuthorFrequency af = new AuthorFrequency(a);
|
91
|
final Integer freq = countMap.get(af.getFullname());
|
92
|
af.setFrequency(freq);
|
93
|
return af;
|
94
|
}
|
95
|
}));
|
96
|
}
|
97
|
|
98
|
public String getId() {
|
99
|
return id;
|
100
|
}
|
101
|
|
102
|
public void setId(final String id) {
|
103
|
this.id = id;
|
104
|
}
|
105
|
|
106
|
public Author getAuthor() {
|
107
|
return author;
|
108
|
}
|
109
|
|
110
|
public void setAuthor(final Author author) {
|
111
|
this.author = author;
|
112
|
}
|
113
|
|
114
|
public boolean hasMerged() {
|
115
|
return (getMerged() != null) && !getMerged().isEmpty();
|
116
|
}
|
117
|
|
118
|
public Authors getMerged() {
|
119
|
return merged;
|
120
|
}
|
121
|
|
122
|
public void setMerged(final Authors merged) {
|
123
|
this.merged = merged;
|
124
|
}
|
125
|
|
126
|
public boolean hasCoAuthors() {
|
127
|
return (getCoAuthors() != null) && !getCoAuthors().isEmpty();
|
128
|
}
|
129
|
|
130
|
public CoAuthors getCoAuthors() {
|
131
|
return coAuthors;
|
132
|
}
|
133
|
|
134
|
public void setCoAuthors(final CoAuthors coAuthors) {
|
135
|
this.coAuthors = coAuthors;
|
136
|
}
|
137
|
|
138
|
public boolean isAnchor() {
|
139
|
return anchor;
|
140
|
}
|
141
|
|
142
|
public void setAnchor(final boolean anchor) {
|
143
|
this.anchor = anchor;
|
144
|
}
|
145
|
|
146
|
public static GTAuthor fromJson(final String json) {
|
147
|
final Gson gson = new Gson();
|
148
|
return gson.fromJson(json, GTAuthor.class);
|
149
|
}
|
150
|
|
151
|
public static GTAuthor fromOafJson(final String json) {
|
152
|
final GsonBuilder gson = new GsonBuilder();
|
153
|
gson.registerTypeAdapter(GTAuthor.class, new GTAuthorOafSerialiser());
|
154
|
return gson.create().fromJson(json, GTAuthor.class);
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public String toString() {
|
159
|
return new Gson().toJson(this);
|
160
|
}
|
161
|
|
162
|
@Override
|
163
|
public int hashCode() {
|
164
|
return getId().hashCode();
|
165
|
}
|
166
|
|
167
|
@Override
|
168
|
public int compareTo(final GTAuthor o) {
|
169
|
return ComparisonChain.start()
|
170
|
.compare(this.getId(), o.getId(), Ordering.natural().nullsLast())
|
171
|
.result();
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
public boolean equals(final Object o) {
|
176
|
return (o instanceof GTAuthor) && getId().equals(((GTAuthor) o).getId());
|
177
|
}
|
178
|
|
179
|
}
|