@@ -453,21 +453,37 @@ public Builder(CsvSchema src)
453
453
_anyPropertyName = src ._anyPropertyName ;
454
454
}
455
455
456
+ /**
457
+ * NOTE: does NOT check for duplicate column names so it is possibly to
458
+ * accidentally add duplicates.
459
+ */
456
460
public Builder addColumn (String name ) {
457
461
int index = _columns .size ();
458
462
return addColumn (new Column (index , name ));
459
463
}
464
+
465
+ /**
466
+ * NOTE: does NOT check for duplicate column names so it is possibly to
467
+ * accidentally add duplicates.
468
+ */
460
469
public Builder addColumn (String name , ColumnType type ) {
461
470
int index = _columns .size ();
462
471
return addColumn (new Column (index , name , type ));
463
472
}
464
473
474
+ /**
475
+ * NOTE: does NOT check for duplicate column names so it is possibly to
476
+ * accidentally add duplicates.
477
+ */
465
478
public Builder addColumn (Column c ) {
466
479
_columns .add (c );
467
480
return this ;
468
481
}
469
482
470
483
/**
484
+ * NOTE: does NOT check for duplicate column names so it is possibly to
485
+ * accidentally add duplicates.
486
+ *
471
487
* @since 2.9
472
488
*/
473
489
public Builder addColumns (Iterable <Column > cs ) {
@@ -478,6 +494,9 @@ public Builder addColumns(Iterable<Column> cs) {
478
494
}
479
495
480
496
/**
497
+ * NOTE: does NOT check for duplicate column names so it is possibly to
498
+ * accidentally add duplicates.
499
+ *
481
500
* @since 2.9
482
501
*/
483
502
public Builder addColumns (Iterable <String > names , ColumnType type ) {
@@ -488,6 +507,24 @@ public Builder addColumns(Iterable<String> names, ColumnType type) {
488
507
return result ;
489
508
}
490
509
510
+ /**
511
+ * NOTE: unlike many other add methods, this method DOES check for, and
512
+ * discard, possible duplicate columns: that is, if this builder already
513
+ * has a column with same name as column to be added, existing column
514
+ * is retained and new column ignored.
515
+ *
516
+ * @since 2.9
517
+ */
518
+ public Builder addColumnsFrom (CsvSchema schema ) {
519
+ Builder result = this ;
520
+ for (Column col : schema ) {
521
+ if (!hasColumn (col .getName ())) {
522
+ result = result .addColumn (col );
523
+ }
524
+ }
525
+ return result ;
526
+ }
527
+
491
528
public Builder addArrayColumn (String name ) {
492
529
int index = _columns .size ();
493
530
return addColumn (new Column (index , name , ColumnType .ARRAY , "" ));
@@ -577,7 +614,25 @@ public int size() {
577
614
public Iterator <Column > getColumns () {
578
615
return _columns .iterator ();
579
616
}
580
-
617
+
618
+ /**
619
+ *<p>
620
+ * NOTE: this method requires linear scan over existing columns
621
+ * so it may be more efficient to use other types of lookups if
622
+ * available (for example, {@link CsvSchema#column(String)} has a
623
+ * hash lookup to use).
624
+ *
625
+ * @since 2.9
626
+ */
627
+ public boolean hasColumn (String name ) {
628
+ for (int i = 0 , end = _columns .size (); i < end ; ++i ) {
629
+ if (_columns .get (i ).getName ().equals (name )) {
630
+ return true ;
631
+ }
632
+ }
633
+ return false ;
634
+ }
635
+
581
636
/**
582
637
* Method for specifying whether Schema should indicate that
583
638
* a header line (first row that contains column names) is to be
@@ -1114,10 +1169,7 @@ public CsvSchema withoutEscapeChar() {
1114
1169
*/
1115
1170
@ Deprecated // in 2.7; remove in 2.8
1116
1171
public CsvSchema withArrayElementSeparator (char c ) {
1117
- return (Character .toString (c ).equals (_arrayElementSeparator )) ? this
1118
- : new CsvSchema (_columns , _features ,
1119
- _columnSeparator , _quoteChar , _escapeChar , _lineSeparator , Character .toString (c ),
1120
- _nullValue , _columnsByName , _anyPropertyName );
1172
+ return withArrayElementSeparator ( Character .toString (c ));
1121
1173
}
1122
1174
1123
1175
/**
@@ -1157,13 +1209,40 @@ public CsvSchema withNullValue(String nvl) {
1157
1209
(nvl == null ) ? null : nvl .toCharArray (),
1158
1210
_columnsByName , _anyPropertyName );
1159
1211
}
1160
-
1212
+
1161
1213
public CsvSchema withoutColumns () {
1162
1214
return new CsvSchema (NO_COLUMNS , _features ,
1163
1215
_columnSeparator , _quoteChar , _escapeChar , _lineSeparator , _arrayElementSeparator ,
1164
1216
_nullValue , _columnsByName , _anyPropertyName );
1165
1217
}
1166
1218
1219
+ /**
1220
+ * Mutant factory method that will try to combine columns of this schema with those
1221
+ * from `toAppend`, starting with columns of this instance, and ignoring
1222
+ * duplicates (if any) from argument `toAppend`.
1223
+ * All settings aside from column sets are copied from `this` instance.
1224
+ *<p>
1225
+ * As with all `withXxx()` methods this method never modifies `this` but either
1226
+ * returns it unmodified (if no new columns found from `toAppend`), or constructs
1227
+ * a new instance and returns that.
1228
+ *
1229
+ * @since 2.9
1230
+ */
1231
+ public CsvSchema withColumnsFrom (CsvSchema toAppend ) {
1232
+ int addCount = toAppend .size ();
1233
+ if (addCount == 0 ) {
1234
+ return this ;
1235
+ }
1236
+ Builder b = rebuild ();
1237
+ for (int i = 0 ; i < addCount ; ++i ) {
1238
+ Column col = toAppend .column (i );
1239
+ if (column (col .getName ()) == null ) {
1240
+ b .addColumn (col );
1241
+ }
1242
+ }
1243
+ return b .build ();
1244
+ }
1245
+
1167
1246
/**
1168
1247
* @since 2.7
1169
1248
*/
@@ -1318,9 +1397,20 @@ public String getNullValueString() {
1318
1397
public Iterator <Column > iterator () {
1319
1398
return Arrays .asList (_columns ).iterator ();
1320
1399
}
1321
-
1400
+
1401
+ /**
1402
+ * Accessor for finding out how many columns this schema defines.
1403
+ *
1404
+ * @return Number of columns this schema defines
1405
+ */
1322
1406
public int size () { return _columns .length ; }
1323
-
1407
+
1408
+ /**
1409
+ * Accessor for column at specified index (0-based); index having to be within
1410
+ *<pre>
1411
+ * 0 <= index < size()
1412
+ *</pre>
1413
+ */
1324
1414
public Column column (int index ) {
1325
1415
return _columns [index ];
1326
1416
}
0 commit comments