File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\StatementBuilder.java.html

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