1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.util; |
7 | |
8 | /** |
9 | * A utility class to build a statement. In addition to the methods supported by |
10 | * StringBuilder, it allows to add a text only in the second iteration. This |
11 | * simplified constructs such as: |
12 | * <pre> |
13 | * StringBuilder buff = new StringBuilder(); |
14 | * for (int i = 0; i < args.length; i++) { |
15 | * if (i > 0) { |
16 | * buff.append(", "); |
17 | * } |
18 | * buff.append(args[i]); |
19 | * } |
20 | * </pre> |
21 | * to |
22 | * <pre> |
23 | * StatementBuilder buff = new StatementBuilder(); |
24 | * for (String s : args) { |
25 | * buff.appendExceptFirst(", "); |
26 | * buff.append(a); |
27 | * } |
28 | *</pre> |
29 | */ |
30 | public class StatementBuilder { |
31 | |
32 | private final StringBuilder builder = new StringBuilder(); |
33 | private int index; |
34 | |
35 | /** |
36 | * Create a new builder. |
37 | */ |
38 | public StatementBuilder() { |
39 | // nothing to do |
40 | } |
41 | |
42 | /** |
43 | * Create a new builder. |
44 | * |
45 | * @param string the initial string |
46 | */ |
47 | public StatementBuilder(String string) { |
48 | builder.append(string); |
49 | } |
50 | |
51 | /** |
52 | * Append a text. |
53 | * |
54 | * @param s the text to append |
55 | * @return itself |
56 | */ |
57 | public StatementBuilder append(String s) { |
58 | builder.append(s); |
59 | return this; |
60 | } |
61 | |
62 | /** |
63 | * Append a character. |
64 | * |
65 | * @param c the character to append |
66 | * @return itself |
67 | */ |
68 | public StatementBuilder append(char c) { |
69 | builder.append(c); |
70 | return this; |
71 | } |
72 | |
73 | /** |
74 | * Append a number. |
75 | * |
76 | * @param x the number to append |
77 | * @return itself |
78 | */ |
79 | public StatementBuilder append(long x) { |
80 | builder.append(x); |
81 | return this; |
82 | } |
83 | |
84 | /** |
85 | * Reset the loop counter. |
86 | * |
87 | * @return itself |
88 | */ |
89 | public StatementBuilder resetCount() { |
90 | index = 0; |
91 | return this; |
92 | } |
93 | |
94 | /** |
95 | * Append a text, but only if appendExceptFirst was never called. |
96 | * |
97 | * @param s the text to append |
98 | */ |
99 | public void appendOnlyFirst(String s) { |
100 | if (index == 0) { |
101 | builder.append(s); |
102 | } |
103 | } |
104 | |
105 | /** |
106 | * Append a text, except when this method is called the first time. |
107 | * |
108 | * @param s the text to append |
109 | */ |
110 | public void appendExceptFirst(String s) { |
111 | if (index++ > 0) { |
112 | builder.append(s); |
113 | } |
114 | } |
115 | |
116 | @Override |
117 | public String toString() { |
118 | return builder.toString(); |
119 | } |
120 | |
121 | /** |
122 | * Get the length. |
123 | * |
124 | * @return the length |
125 | */ |
126 | public int length() { |
127 | return builder.length(); |
128 | } |
129 | |
130 | } |