You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatementBuilder.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.util;
  18. /**
  19. * A utility class to build a statement. In addition to the methods supported by
  20. * StringBuilder, it allows to add a text only in the second iteration. This
  21. * simplified constructs such as:
  22. *
  23. * <pre>
  24. * StringBuilder buff = new StringBuilder();
  25. * for (int i = 0; i &lt; args.length; i++) {
  26. * if (i &gt; 0) {
  27. * buff.append(&quot;, &quot;);
  28. * }
  29. * buff.append(args[i]);
  30. * }
  31. * </pre>
  32. *
  33. * to
  34. *
  35. * <pre>
  36. * StatementBuilder buff = new StatementBuilder();
  37. * for (String s : args) {
  38. * buff.appendExceptFirst(&quot;, &quot;);
  39. * buff.append(a);
  40. * }
  41. * </pre>
  42. */
  43. public class StatementBuilder {
  44. private final StringBuilder builder = new StringBuilder();
  45. private int index;
  46. /**
  47. * Create a new builder.
  48. */
  49. public StatementBuilder() {
  50. // nothing to do
  51. }
  52. /**
  53. * Create a new builder.
  54. *
  55. * @param string
  56. * the initial string
  57. */
  58. public StatementBuilder(String string) {
  59. builder.append(string);
  60. }
  61. /**
  62. * Append a text.
  63. *
  64. * @param s
  65. * the text to append
  66. * @return itself
  67. */
  68. public StatementBuilder append(String s) {
  69. builder.append(s);
  70. return this;
  71. }
  72. /**
  73. * Append a character.
  74. *
  75. * @param c
  76. * the character to append
  77. * @return itself
  78. */
  79. public StatementBuilder append(char c) {
  80. builder.append(c);
  81. return this;
  82. }
  83. /**
  84. * Append a number.
  85. *
  86. * @param x
  87. * the number to append
  88. * @return itself
  89. */
  90. public StatementBuilder append(long x) {
  91. builder.append(x);
  92. return this;
  93. }
  94. /**
  95. * Reset the loop counter.
  96. *
  97. * @return itself
  98. */
  99. public StatementBuilder resetCount() {
  100. index = 0;
  101. return this;
  102. }
  103. /**
  104. * Append a text, but only if appendExceptFirst was never called.
  105. *
  106. * @param s
  107. * the text to append
  108. */
  109. public void appendOnlyFirst(String s) {
  110. if (index == 0) {
  111. builder.append(s);
  112. }
  113. }
  114. /**
  115. * Append a text, except when this method is called the first time.
  116. *
  117. * @param s
  118. * the text to append
  119. */
  120. public void appendExceptFirst(String s) {
  121. if (index++ > 0) {
  122. builder.append(s);
  123. }
  124. }
  125. public void append(StatementBuilder sb) {
  126. builder.append(sb);
  127. }
  128. public void insert(int offset, char c) {
  129. builder.insert(offset, c);
  130. }
  131. public String toString() {
  132. return builder.toString();
  133. }
  134. /**
  135. * Get the length.
  136. *
  137. * @return the length
  138. */
  139. public int length() {
  140. return builder.length();
  141. }
  142. }