View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // ToStringVisitor.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.example.datamodel.visitor;
22  
23  import java.text.*;
24  import java.util.*;
25  
26  import org.apache.commons.lang.*;
27  import org.perfectjpattern.core.behavioral.visitor.*;
28  import org.perfectjpattern.example.datamodel.*;
29  
30  /**
31   * Visits any data model type and provides generating a String representation 
32   *
33   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
34   * @version $ $Date: Dec 6, 2008 6:25:02 PM $
35   */
36  public 
37  class ToStringVisitor
38  extends AbstractVisitor<Object>
39  {
40      //------------------------------------------------------------------------
41      // public
42      //------------------------------------------------------------------------
43      public void
44      visitCustomer(Customer aCustomer)
45      {
46          Validate.notNull(aCustomer, "'aCustomer' must not be null");
47          
48          String myPattern = theBundle.getString(aCustomer.getClass().
49              getSimpleName());
50          
51          StringBuilder myResult = new StringBuilder();
52          
53          StringBuilder myBuilder = new StringBuilder();
54          for (Order myOrder : aCustomer.getOrders())
55          {
56              myBuilder.append(LINE_BREAK);
57  
58              visit(myOrder);
59              myBuilder.append(getResult());
60          }
61          
62          myResult.append(LINE_BREAK);
63          myResult.append(
64              "**************************************************************");
65          myResult.append(LINE_BREAK);
66          myResult.append(MessageFormat.format(myPattern, aCustomer.getId(), 
67              aCustomer.getName()));
68          myResult.append(myBuilder.toString());
69          myResult.append(LINE_BREAK);
70          myResult.append(
71              "**************************************************************");
72          
73          setResult(myResult.toString());
74      }
75      
76      //------------------------------------------------------------------------
77      public void
78      visitOrder(Order anOrder)
79      {
80          Validate.notNull(anOrder, "'anOrder' must not be null");
81  
82          String myPattern = theBundle.getString(anOrder.getClass().
83              getSimpleName());
84          
85          StringBuilder myBuilder = new StringBuilder();
86          for (Product myProduct : anOrder.getProducts())
87          {
88              myBuilder.append(LINE_BREAK);
89  
90              visit(myProduct);
91              myBuilder.append(getResult());
92          }
93  
94          StringBuilder myResult = new StringBuilder();
95          myResult.append(MessageFormat.format(myPattern, anOrder.getId(), 
96              anOrder.getDate())); 
97          myResult.append(myBuilder.toString());
98          
99          setResult(myResult.toString());
100     }
101 
102     //------------------------------------------------------------------------
103     public void
104     visitProduct(Product aProduct)
105     {
106         Validate.notNull(aProduct, "'aProduct' must not be null");
107 
108         String myPattern = theBundle.getString(aProduct.getClass().
109             getSimpleName());
110         
111         String myResult = MessageFormat.format(myPattern, aProduct.getId(), 
112             aProduct.getName(), aProduct.getListPrice()); 
113         
114         setResult(myResult);
115     }
116 
117     //------------------------------------------------------------------------
118     public void
119     visitMovie(Movie aMovie)
120     {
121         Validate.notNull(aMovie, "'aMovie' must not be null");
122         
123         String myPattern = theBundle.getString(aMovie.getClass().
124             getSimpleName());
125         
126         StringBuilder myResult = new StringBuilder();
127         
128         myResult.append(LINE_BREAK);
129         myResult.append(
130             "**************************************************************");
131         myResult.append(LINE_BREAK);
132         myResult.append(MessageFormat.format(myPattern, aMovie.getId(), 
133             aMovie.getTitle(), aMovie.getDirector(), aMovie.getYear()));
134         
135         setResult(myResult.toString());
136     }
137 
138     //------------------------------------------------------------------------
139     /**
140      * Returns the result
141      *
142      * @return the result
143      */
144     public final String 
145     getResult()
146     {
147         return theResult;
148     }
149     
150     //------------------------------------------------------------------------
151     // protected
152     //------------------------------------------------------------------------
153     /**
154      * Sets the result
155      *
156      * @param anResult the result to set
157      */
158     protected final void 
159     setResult(String anResult)
160     {
161         theResult = anResult;
162     }
163 
164     //------------------------------------------------------------------------
165     // members
166     //------------------------------------------------------------------------
167     private static final String LINE_BREAK = System.getProperty(
168         "line.separator");
169     private final ResourceBundle theBundle = ResourceBundle.getBundle(
170         getClass().getSimpleName());
171     private String theResult;
172 }