View Javadoc

1   package org.apache.torque.generator.outlet;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * The result of an outlet. Can either be a String or a byte array.
30   */
31  public class OutletResult
32  {
33      /** The string result. */
34      private String stringResult;
35  
36      /** The byte array result. */
37      private byte[] byteArrayResult;
38  
39      /**
40       * Constructor for a String result.
41       *
42       * @param stringResult the String result.
43       */
44      public OutletResult(String stringResult)
45      {
46          this.stringResult = stringResult;
47      }
48  
49      /**
50       * Constructor for a byte array result.
51       *
52       * @param byteArrayResult the byte array result.
53       */
54      public OutletResult(byte[] byteArrayResult)
55      {
56          this.byteArrayResult = byteArrayResult;
57      }
58  
59      /**
60       * Returns the string result.
61       *
62       * @return the string result, or null if this class contains
63       *         a byte array result.
64       */
65      public String getStringResult()
66      {
67          return stringResult;
68      }
69  
70      /**
71       * Returns the byte array result.
72       *
73       * @return the byte array result, or null if this class contains
74       *         a string result.
75       */
76      public byte[] getByteArrayResult()
77      {
78          return byteArrayResult;
79      }
80  
81      /**
82       * Returns whether the result type is String.
83       * Note: If the instance was constructed with null byte array,
84       * this method also returns true.
85       *
86       * @return false if the contained byteArray is not null, true otherwise.
87       */
88      public boolean isStringResult()
89      {
90          return (byteArrayResult == null);
91      }
92  
93      /**
94       * Returns whether the result type is byte array.
95       * Note: If the instance was constructed with null string,
96       * this method also returns true.
97       *
98       * @return false if the contained String is not null, true otherwise.
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      * Concatenates an array of OutletResults.
138      *
139      * @param input the OutletResults to concatenate,
140      *        not null, not empty, must not contain null, all of the same type
141      *        (either all string or all byte array).
142      * @return the concatenated input.
143      *
144      * @throws GeneratorException if the input outlet results are
145      *         of a different type.
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      * Concatenates a list of OutletResults.
159      *
160      * @param input the OutletResults to concatenate,
161      *        not null, not empty, must not contain null, all of the same type
162      *        (either all string or all byte array).
163      * @return the concatenated input.
164      *
165      * @throws GeneratorException if the input outlet results are
166      *         of a different type.
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 }