1 | package com.framsticks.structure.messages; |
---|
2 | |
---|
3 | import java.util.Arrays; |
---|
4 | import java.util.Set; |
---|
5 | import java.util.TreeSet; |
---|
6 | |
---|
7 | import org.apache.commons.lang3.StringUtils; |
---|
8 | |
---|
9 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
10 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
11 | import com.framsticks.util.Misc; |
---|
12 | import com.framsticks.util.lang.Strings; |
---|
13 | |
---|
14 | /** |
---|
15 | * @author Piotr Sniegowski |
---|
16 | */ |
---|
17 | @FramsClassAnnotation(order = {"type", "pos", "id", "hint"}) |
---|
18 | public class ListChange { |
---|
19 | |
---|
20 | public static enum Action { |
---|
21 | Add, |
---|
22 | Remove, |
---|
23 | Modify |
---|
24 | } |
---|
25 | |
---|
26 | |
---|
27 | public ListChange(Action action, Integer position, String identifier, Object... hints) { |
---|
28 | this.action = action; |
---|
29 | this.position = position; |
---|
30 | this.identifier = identifier; |
---|
31 | if (hints.length != 0) { |
---|
32 | this.hints = new TreeSet<>(); |
---|
33 | for (Object h : hints) { |
---|
34 | this.hints.add(h.toString()); |
---|
35 | } |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * |
---|
41 | */ |
---|
42 | public ListChange() { |
---|
43 | } |
---|
44 | |
---|
45 | public Action getAction() { |
---|
46 | return action; |
---|
47 | } |
---|
48 | |
---|
49 | public Integer getPosition() { |
---|
50 | return position; |
---|
51 | } |
---|
52 | |
---|
53 | public String getIdentifier() { |
---|
54 | return identifier; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | public boolean hasHint(String hint) { |
---|
59 | if (hints == null) { |
---|
60 | return false; |
---|
61 | } |
---|
62 | return hints.contains(hint); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | public Action action = Action.Add; |
---|
67 | @ParamAnnotation(id = "pos", def = "-1") |
---|
68 | public Integer position; |
---|
69 | @ParamAnnotation(id = "id") |
---|
70 | public String identifier; |
---|
71 | |
---|
72 | protected Set<String> hints; |
---|
73 | |
---|
74 | @ParamAnnotation |
---|
75 | public String getHints() { |
---|
76 | return StringUtils.join(hints, ","); |
---|
77 | } |
---|
78 | |
---|
79 | @ParamAnnotation |
---|
80 | public void setHints(String hints) { |
---|
81 | if (!Strings.notEmpty(hints)) { |
---|
82 | this.hints = null; |
---|
83 | return; |
---|
84 | } |
---|
85 | this.hints = new TreeSet<>(); |
---|
86 | this.hints.addAll(Arrays.asList(StringUtils.split(hints, ","))); |
---|
87 | } |
---|
88 | |
---|
89 | @ParamAnnotation |
---|
90 | public Integer getType() { return action.ordinal(); } |
---|
91 | @ParamAnnotation |
---|
92 | public void setType(Integer type) { action = Action.values()[type]; } |
---|
93 | |
---|
94 | public String getBestIdentifier() { |
---|
95 | if (Strings.notEmpty(identifier)) { |
---|
96 | return identifier; |
---|
97 | } |
---|
98 | return position.toString(); |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public String toString() { |
---|
103 | StringBuilder b = new StringBuilder(); |
---|
104 | b.append(action).append(" ").append(identifier).append(" ").append(position); |
---|
105 | if (hints != null && !hints.isEmpty()) { |
---|
106 | b.append(" ").append(getHints()); |
---|
107 | } |
---|
108 | return b.toString(); |
---|
109 | } |
---|
110 | |
---|
111 | @Override |
---|
112 | public boolean equals(Object object) { |
---|
113 | if (object instanceof ListChange) { |
---|
114 | ListChange r = (ListChange) object; |
---|
115 | return Misc.equals(action, r.action) && Misc.equals(position, r.position) && Misc.equals(identifier, r.identifier) && Misc.equals(hints, r.hints); |
---|
116 | } |
---|
117 | return false; |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|