2424
2525import java .io .Serializable ;
2626import java .util .Collections ;
27+ import java .util .HashSet ;
2728import java .util .LinkedHashMap ;
2829import java .util .List ;
2930import java .util .Map ;
3031import java .util .Objects ;
32+ import java .util .Set ;
3133import javax .annotation .Nullable ;
3234
3335/**
@@ -188,9 +190,17 @@ public ValueBinder<WriteBuilder> set(String columnName) {
188190 return binder ;
189191 }
190192
193+ /**
194+ * Returns a newly created {@code Mutation} based on the contents of the {@code Builder}.
195+ *
196+ * @throws IllegalStateException if any duplicate columns are present. Duplicate detection is
197+ * case-insensitive.
198+ */
191199 public Mutation build () {
192200 checkBindingInProgress (false );
193- return new Mutation (table , operation , columns .build (), values .build (), null );
201+ ImmutableList <String > columnNames = columns .build ();
202+ checkDuplicateColumns (columnNames );
203+ return new Mutation (table , operation , columnNames , values .build (), null );
194204 }
195205
196206 private void checkBindingInProgress (boolean expectInProgress ) {
@@ -200,6 +210,17 @@ private void checkBindingInProgress(boolean expectInProgress) {
200210 throw new IllegalStateException ("Incomplete binding for column " + currentColumn );
201211 }
202212 }
213+
214+ private void checkDuplicateColumns (ImmutableList <String > columnNames ) {
215+ Set <String > columnNameSet = new HashSet <>();
216+ for (String columnName : columnNames ) {
217+ columnName = columnName .toLowerCase ();
218+ if (columnNameSet .contains (columnName )) {
219+ throw new IllegalStateException ("Duplicate column: " + columnName );
220+ }
221+ columnNameSet .add (columnName );
222+ }
223+ }
203224 }
204225
205226 /** Returns the name of the table that this mutation will affect. */
@@ -247,9 +268,6 @@ public Map<String, Value> asMap() {
247268 LinkedHashMap <String , Value > map = new LinkedHashMap <>();
248269 for (int i = 0 ; i < columns .size (); ++i ) {
249270 Value existing = map .put (columns .get (i ), values .get (i ));
250- if (existing != null ) {
251- throw new IllegalStateException ("Duplicate column: " + columns .get (i ));
252- }
253271 }
254272 return Collections .unmodifiableMap (map );
255273 }
0 commit comments