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