1 package eu.fbk.dkm.pikes.raid;
2
3 import com.google.common.collect.ForwardingSet;
4 import ixa.kaflib.Opinion;
5
6 import java.util.Comparator;
7 import java.util.Set;
8 import java.util.SortedSet;
9 import java.util.TreeSet;
10
11
12
13
14
15 public class OpinionSet extends ForwardingSet<OpinionSet.OpinionEntry> {
16
17 public class OpinionEntry {
18 private Opinion opinion;
19 private Integer size;
20
21 public OpinionEntry(Opinion opinion) {
22 this.opinion = opinion;
23 this.size = opinion.getOpinionExpression().getTerms().size();
24 }
25
26 public Opinion getOpinion() {
27 return opinion;
28 }
29
30 public Integer getSize() {
31 return size;
32 }
33
34 @Override
35 public String toString() {
36 return "OpinionEntry{" +
37 "opinion=" + opinion +
38 ", size=" + size +
39 '}';
40 }
41 }
42
43 private class OpinionEntryComparator implements Comparator<OpinionEntry> {
44
45 boolean desc = false;
46
47 public OpinionEntryComparator(boolean desc) {
48 this.desc = desc;
49 }
50
51 @Override
52 public int compare(OpinionEntry o1, OpinionEntry o2) {
53 int result;
54 if (desc) {
55 result = o2.getSize() - o1.getSize();
56 }
57 else {
58 result = o1.getSize() - o2.getSize();
59 }
60 if (result == 0) {
61 result = o1.getOpinion().getId().compareTo(o2.getOpinion().getId());
62 }
63
64 return result;
65 }
66
67 }
68
69 SortedSet<OpinionEntry> support;
70
71 public OpinionSet() {
72 this(false);
73 }
74
75 @Override
76 protected Set<OpinionEntry> delegate() {
77 return support;
78 }
79
80 public OpinionSet(boolean desc) {
81 support = new TreeSet<>(new OpinionEntryComparator(desc));
82 }
83
84 public void add(Opinion opinion) {
85 OpinionEntry entry;
86 try {
87 entry = new OpinionEntry(opinion);
88 } catch (Exception e) {
89 return;
90 }
91 support.add(entry);
92 }
93
94 @Override
95 public String toString() {
96 return support.toString();
97 }
98 }