68
68
public class CsvSchema
69
69
implements FormatSchema ,
70
70
Iterable <CsvSchema .Column >,
71
- java .io .Serializable // since 2.4.3
71
+ java .io .Serializable // since 2.5
72
72
{
73
73
private static final long serialVersionUID = 1L ; // 2.5
74
74
@@ -261,9 +261,13 @@ public Column(int index, String name, ColumnType type, int arrayElementSep)
261
261
_next = null ;
262
262
}
263
263
264
- public Column (Column src , Column next )
264
+ public Column (Column src , Column next ) {
265
+ this (src , src ._index , next );
266
+ }
267
+
268
+ protected Column (Column src , int index , Column next )
265
269
{
266
- _index = src . _index ;
270
+ _index = index ;
267
271
_name = src ._name ;
268
272
_type = src ._type ;
269
273
_arrayElementSeparator = src ._arrayElementSeparator ;
@@ -295,6 +299,16 @@ public Column withNext(Column next) {
295
299
}
296
300
return new Column (this , next );
297
301
}
302
+
303
+ /**
304
+ * @since 2.7
305
+ */
306
+ public Column withNext (int index , Column next ) {
307
+ if ((_index == index ) && (_next == next )) {
308
+ return this ;
309
+ }
310
+ return new Column (this , index , next );
311
+ }
298
312
299
313
public int getIndex () { return _index ; }
300
314
public String getName () { return _name ; }
@@ -649,7 +663,7 @@ public CsvSchema(Column[] columns, int features,
649
663
_escapeChar = escapeChar ;
650
664
_lineSeparator = lineSeparator ;
651
665
_nullValue = nullValue ;
652
-
666
+
653
667
// and then we may need to create a mapping
654
668
if (_columns .length == 0 ) {
655
669
_columnsByName = Collections .emptyMap ();
@@ -686,7 +700,8 @@ protected CsvSchema(Column[] columns, int features,
686
700
* Copy constructor used for creating variants using
687
701
* <code>sortedBy()</code> methods.
688
702
*/
689
- protected CsvSchema (CsvSchema base , Column [] columns ) {
703
+ protected CsvSchema (CsvSchema base , Column [] columns )
704
+ {
690
705
_columns = _link (columns );
691
706
_features = base ._features ;
692
707
_columnSeparator = base ._columnSeparator ;
@@ -695,7 +710,16 @@ protected CsvSchema(CsvSchema base, Column[] columns) {
695
710
_lineSeparator = base ._lineSeparator ;
696
711
_arrayElementSeparator = base ._arrayElementSeparator ;
697
712
_nullValue = base ._nullValue ;
698
- _columnsByName = base ._columnsByName ;
713
+
714
+ // and then we may need to create a mapping
715
+ if (_columns .length == 0 ) {
716
+ _columnsByName = Collections .emptyMap ();
717
+ } else {
718
+ _columnsByName = new HashMap <String ,Column >(4 + _columns .length );
719
+ for (Column c : _columns ) {
720
+ _columnsByName .put (c .getName (), c );
721
+ }
722
+ }
699
723
}
700
724
701
725
/**
@@ -716,14 +740,16 @@ protected CsvSchema(CsvSchema base, int features) {
716
740
}
717
741
718
742
/**
719
- * Helper method used for chaining columns together using next-linkage
743
+ * Helper method used for chaining columns together using next-linkage,
744
+ * as well as ensuring that indexes are correct.
720
745
*/
721
- private static Column [] _link (Column [] orig ) {
746
+ private static Column [] _link (Column [] orig )
747
+ {
722
748
int i = orig .length ;
723
749
Column [] result = new Column [i ];
724
750
Column prev = null ;
725
751
for (; --i >= 0 ; ) {
726
- Column curr = orig [i ].withNext (prev );
752
+ Column curr = orig [i ].withNext (i , prev );
727
753
result [i ] = curr ;
728
754
prev = curr ;
729
755
}
@@ -922,7 +948,8 @@ public CsvSchema withoutColumns() {
922
948
*
923
949
* @since 2.4
924
950
*/
925
- public CsvSchema sortedBy (String ... columnNames ) {
951
+ public CsvSchema sortedBy (String ... columnNames )
952
+ {
926
953
LinkedHashMap <String ,Column > map = new LinkedHashMap <String ,Column >();
927
954
for (String colName : columnNames ) {
928
955
Column col = _columnsByName .get (colName );
0 commit comments