1 package org.apache.torque.generator.outlet;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Arrays;
23
24 import org.apache.commons.lang.builder.EqualsBuilder;
25 import org.apache.commons.lang.builder.HashCodeBuilder;
26 import org.apache.torque.generator.GeneratorException;
27
28
29
30
31 public class OutletResult
32 {
33
34 private String stringResult;
35
36
37 private byte[] byteArrayResult;
38
39
40
41
42
43
44 public OutletResult(String stringResult)
45 {
46 this.stringResult = stringResult;
47 }
48
49
50
51
52
53
54 public OutletResult(byte[] byteArrayResult)
55 {
56 this.byteArrayResult = byteArrayResult;
57 }
58
59
60
61
62
63
64
65 public String getStringResult()
66 {
67 return stringResult;
68 }
69
70
71
72
73
74
75
76 public byte[] getByteArrayResult()
77 {
78 return byteArrayResult;
79 }
80
81
82
83
84
85
86
87
88 public boolean isStringResult()
89 {
90 return (byteArrayResult == null);
91 }
92
93
94
95
96
97
98
99
100 public boolean isByteArrayResult()
101 {
102 return (stringResult == null);
103 }
104
105 @Override
106 public int hashCode()
107 {
108 HashCodeBuilder hashCodeBuilder = new HashCodeBuilder()
109 .append(stringResult)
110 .append(byteArrayResult);
111 return hashCodeBuilder.toHashCode();
112 }
113
114 @Override
115 public boolean equals(Object obj)
116 {
117 if (this == obj)
118 {
119 return true;
120 }
121 if (obj == null)
122 {
123 return false;
124 }
125 if (getClass() != obj.getClass())
126 {
127 return false;
128 }
129 OutletResult other = (OutletResult) obj;
130 EqualsBuilder equalsBuilder = new EqualsBuilder()
131 .append(stringResult, other.stringResult)
132 .append(byteArrayResult, other.byteArrayResult);
133 return equalsBuilder.isEquals();
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147 public static OutletResult concatenate(OutletResult... input)
148 throws GeneratorException
149 {
150 if (input == null)
151 {
152 throw new NullPointerException("input must not be null");
153 }
154 return concatenate(Arrays.asList(input));
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 public static OutletResult concatenate(Iterable<OutletResult> input)
169 throws GeneratorException
170 {
171 if (input == null)
172 {
173 throw new NullPointerException("input must not be null");
174 }
175 if (!input.iterator().hasNext())
176 {
177 throw new IllegalArgumentException("input must not be empty");
178 }
179 Boolean isStringResult = null;
180 for (OutletResult part : input)
181 {
182 if (!part.isByteArrayResult())
183 {
184 isStringResult = true;
185 break;
186 }
187 if (!part.isStringResult())
188 {
189 isStringResult = false;
190 break;
191 }
192 }
193 if (isStringResult == null)
194 {
195 return new OutletResult((String) null);
196 }
197 if (Boolean.TRUE.equals(isStringResult))
198 {
199 StringBuilder result = new StringBuilder();
200 for (OutletResult part : input)
201 {
202 if (!part.isStringResult())
203 {
204 throw new GeneratorException(
205 "first OutletResult to concatenate is a "
206 + "String result but a following result is a "
207 + "byte array."
208 + " All concatenated results must be "
209 + "of the same type");
210 }
211 String partContent = part.getStringResult();
212 if (partContent != null)
213 {
214 result.append(partContent);
215 }
216 }
217 return new OutletResult(result.toString());
218 }
219 int totalLength = 0;
220 for (OutletResult part : input)
221 {
222 if (!part.isByteArrayResult())
223 {
224 throw new GeneratorException(
225 "first OutletResult to concatenate is a "
226 + "byte array result but a following result is a "
227 + "String result."
228 + " All concatenated results must be "
229 + "of the same type");
230 }
231 byte[] partContent = part.getByteArrayResult();
232 if (partContent != null)
233 {
234 totalLength += partContent.length;
235 }
236 }
237 byte[] result = new byte[totalLength];
238 int alreadyFilledBytes = 0;
239 for (OutletResult part : input)
240 {
241 byte[] partContent = part.getByteArrayResult();
242 if (partContent == null)
243 {
244 continue;
245 }
246 System.arraycopy(partContent,
247 0,
248 result,
249 alreadyFilledBytes,
250 partContent.length);
251 alreadyFilledBytes += partContent.length;
252
253 }
254 return new OutletResult(result);
255 }
256 }