1 package paolomind.multitalk.netmessage.impl;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import paolomind.multitalk.netmessage.IMessageFactory;
8 import paolomind.multitalk.netmessage.IMessage;
9 import paolomind.multitalk.netmessage.MessageException;
10
11
12
13
14
15
16 public class MultiMessageFactoryImpl implements IMessageFactory {
17
18
19
20
21 public static final String SEPARATOR;
22
23
24 private IMessageFactory factory;
25
26 static {
27 String s = System.getProperty("mexseparator");
28 if (s == null) {
29 s = "\n-\n";
30 }
31 SEPARATOR = s;
32 }
33
34
35
36
37
38
39 public final IMessage createMessage() {
40 return new MultiMessageImpl();
41 }
42
43
44
45
46
47
48
49
50 public final IMessage createMessage(final String s) throws MessageException {
51 return new MultiMessageImpl(s);
52 }
53
54
55
56
57
58
59 private class MultiMessageImpl implements paolomind.multitalk.netmessage.IMessage {
60
61
62 private static final long serialVersionUID = -3548197602594421396L;
63
64 private List infos;
65
66
67 public MultiMessageImpl() {
68 infos = new ArrayList();
69 }
70
71
72
73
74
75 public MultiMessageImpl(final String mex) throws MessageException {
76 this();
77 int s = 0, e;
78 int n = mex.length();
79 do {
80 e = mex.indexOf(SEPARATOR, s);
81 if (e < 0) {
82 e = n;
83 }
84 infos.add(factory.createMessage(mex.substring(s, e)));
85 s = e + SEPARATOR.length();
86 } while (s < n);
87 }
88
89
90
91
92
93 public final Object getInfo() {
94 if (infos.isEmpty()) {
95 return null;
96 }
97 IMessage s = (IMessage) infos.remove(0);
98 return s;
99 }
100
101
102
103
104
105 public final void addInfo(final Object info) {
106 try {
107 this.addInfo((IMessage) info);
108 } catch (ClassCastException e) {
109 throw new ClassCastException("l'argomento deve essere un tipo message");
110 }
111 }
112
113
114
115
116 public final void addInfo(final IMessage info) {
117 this.infos.add(info);
118 }
119
120
121
122
123
124 public final Object viewInfo() {
125 return this.infos.get(0);
126 }
127
128
129
130
131
132 public final String toString() {
133 StringBuffer m = new StringBuffer();
134 final int n = this.infos.size();
135 Iterator it = this.infos.iterator();
136 for (int i = 0; i < n - 1; i++) {
137 m.append((it.next().toString()) + SEPARATOR);
138 }
139 if (it.hasNext()) {
140 m.append(it.next().toString());
141 }
142 return m.toString();
143 }
144
145
146
147
148
149 public final int hashCode() {
150 return toString().hashCode();
151 }
152
153
154
155
156
157
158 public final boolean equals(final Object o) {
159 return (o != null && this.hashCode() == o.hashCode());
160 }
161
162
163
164
165
166
167 public void addInfo(final String varname, final Object value) {
168 throw new UnsupportedOperationException("operazione non consentita");
169 }
170
171
172
173
174
175
176 public String[] getNameValue() throws MessageException {
177 throw new UnsupportedOperationException("operazione non consentita");
178 }
179 }
180
181
182
183
184
185 public final void setFactory(final IMessageFactory pfactory) {
186 this.factory = pfactory;
187 }
188 }