1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
32
33
34
35
36 public
37 class ToStringVisitor
38 extends AbstractVisitor<Object>
39 {
40
41
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
141
142
143
144 public final String
145 getResult()
146 {
147 return theResult;
148 }
149
150
151
152
153
154
155
156
157
158 protected final void
159 setResult(String anResult)
160 {
161 theResult = anResult;
162 }
163
164
165
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 }