1 /*
2 Copyright (c) 2007 Health Market Science, Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package com.healthmarketscience.common.util;
18
19 import java.io.IOException;
20
21 /**
22 * Simple extension of AppendableExt that swallows IOExceptions under the
23 * assumption that it will generally be used with Appendables which do not
24 * actually throw any IOExceptions (i.e. StringBuilder). Default constructors
25 * will use a StringBuilder for the underlying Appendable.
26 * <p>
27 * In the event that an underlying Appendable is used which does actually
28 * thrown IOExceptions, they will be trapped, and at any time the last
29 * IOException can be obtained using {@link #getIOException}.
30 * </p>
31 * <p>
32 * This class is not thread-safe.
33 * </p>
34 */
35 public class StringAppendableExt extends AppendableExt
36 {
37
38 /**
39 * The last IOException thrown by an append call
40 */
41 private IOException _ioException;
42
43 /**
44 * Initialize a new StringAppendableExt based on a StringBuilder.
45 */
46 public StringAppendableExt() {
47 this(new StringBuilder(), null);
48 }
49
50 /**
51 * Initialize a new StringAppendableExt based on a StringBuilder, with a
52 * specified initial capacity.
53 *
54 * @param capacity the initial capacity for the StringBuilder.
55 */
56 public StringAppendableExt(int capacity) {
57 this(capacity, null);
58 }
59
60 /**
61 * Initialize a new StringAppendableExt based on a StringBuilder, with a
62 * specified initial capacity and given context.
63 *
64 * @param size the initial capacity for the StringBuilder.
65 * @param context initial append context
66 */
67 public StringAppendableExt(int size, Object context) {
68 this(new StringBuilder(size), context);
69 }
70
71 /**
72 * Initialize a new StringAppendableExt based on the given Appendable.
73 *
74 * @param app initial underlying Appendable
75 */
76 public StringAppendableExt(Appendable app) {
77 this(app, null);
78 }
79
80 /**
81 * Initialize a new StringAppendableExt based on the given Appendable and
82 * context.
83 *
84 * @param app initial underlying Appendable
85 * @param context initial append context
86 */
87 public StringAppendableExt(Appendable app, Object context) {
88 super(app, context);
89 }
90
91 @Override
92 public StringAppendableExt append(char c)
93 {
94 try {
95 super.append(c);
96 } catch(IOException e) {
97 _ioException = e;
98 }
99 return this;
100 }
101
102 @Override
103 public StringAppendableExt append(CharSequence s)
104 {
105 try {
106 super.append(s);
107 } catch(IOException e) {
108 _ioException = e;
109 }
110 return this;
111 }
112
113 @Override
114 public StringAppendableExt append(CharSequence s, int start, int end)
115 {
116 try {
117 super.append(s, start, end);
118 } catch(IOException e) {
119 _ioException = e;
120 }
121 return this;
122 }
123
124 @Override
125 public StringAppendableExt append(Appendee a)
126 {
127 try {
128 super.append(a);
129 } catch(IOException e) {
130 _ioException = e;
131 }
132 return this;
133 }
134
135 @Override
136 public StringAppendableExt append(Object o)
137 {
138 try {
139 super.append(o);
140 } catch(IOException e) {
141 _ioException = e;
142 }
143 return this;
144 }
145
146 @Override
147 public StringAppendableExt append(Iterable<?> iable, Object delimiter)
148 {
149 try {
150 super.append(iable, delimiter);
151 } catch(IOException e) {
152 _ioException = e;
153 }
154 return this;
155 }
156
157 /**
158 * Get the last IOException thrown by an append call, or {@code null} if
159 * no IOException was caught.
160 *
161 * @return the last IOException thrown by an append call, or {@code null}
162 * if no IOException was caught.
163 */
164 public IOException getIOException() {
165 return _ioException;
166 }
167
168 }