77
77
public class CsvSchema
78
78
implements FormatSchema ,
79
79
Iterable <CsvSchema .Column >,
80
- java .io .Serializable // since 2.4.3
80
+ java .io .Serializable // since 2.5
81
81
{
82
82
private static final long serialVersionUID = 1L ; // 2.5
83
83
@@ -289,9 +289,13 @@ public Column(int index, String name, ColumnType type, String arrayElementSep)
289
289
_next = null ;
290
290
}
291
291
292
- public Column (Column src , Column next )
292
+ public Column (Column src , Column next ) {
293
+ this (src , src ._index , next );
294
+ }
295
+
296
+ protected Column (Column src , int index , Column next )
293
297
{
294
- _index = src . _index ;
298
+ _index = index ;
295
299
_name = src ._name ;
296
300
_type = src ._type ;
297
301
_arrayElementSeparator = src ._arrayElementSeparator ;
@@ -334,6 +338,16 @@ public Column withNext(Column next) {
334
338
}
335
339
return new Column (this , next );
336
340
}
341
+
342
+ /**
343
+ * @since 2.7
344
+ */
345
+ public Column withNext (int index , Column next ) {
346
+ if ((_index == index ) && (_next == next )) {
347
+ return this ;
348
+ }
349
+ return new Column (this , index , next );
350
+ }
337
351
338
352
public int getIndex () { return _index ; }
339
353
public String getName () { return _name ; }
@@ -768,7 +782,7 @@ public CsvSchema(Column[] columns, int features,
768
782
_escapeChar = escapeChar ;
769
783
_lineSeparator = lineSeparator ;
770
784
_nullValue = nullValue ;
771
-
785
+
772
786
// and then we may need to create a mapping
773
787
if (_columns .length == 0 ) {
774
788
_columnsByName = Collections .emptyMap ();
@@ -805,7 +819,8 @@ protected CsvSchema(Column[] columns, int features,
805
819
* Copy constructor used for creating variants using
806
820
* <code>sortedBy()</code> methods.
807
821
*/
808
- protected CsvSchema (CsvSchema base , Column [] columns ) {
822
+ protected CsvSchema (CsvSchema base , Column [] columns )
823
+ {
809
824
_columns = _link (columns );
810
825
_features = base ._features ;
811
826
_columnSeparator = base ._columnSeparator ;
@@ -814,7 +829,16 @@ protected CsvSchema(CsvSchema base, Column[] columns) {
814
829
_lineSeparator = base ._lineSeparator ;
815
830
_arrayElementSeparator = base ._arrayElementSeparator ;
816
831
_nullValue = base ._nullValue ;
817
- _columnsByName = base ._columnsByName ;
832
+
833
+ // and then we may need to create a mapping
834
+ if (_columns .length == 0 ) {
835
+ _columnsByName = Collections .emptyMap ();
836
+ } else {
837
+ _columnsByName = new HashMap <String ,Column >(4 + _columns .length );
838
+ for (Column c : _columns ) {
839
+ _columnsByName .put (c .getName (), c );
840
+ }
841
+ }
818
842
}
819
843
820
844
/**
@@ -835,14 +859,16 @@ protected CsvSchema(CsvSchema base, int features) {
835
859
}
836
860
837
861
/**
838
- * Helper method used for chaining columns together using next-linkage
862
+ * Helper method used for chaining columns together using next-linkage,
863
+ * as well as ensuring that indexes are correct.
839
864
*/
840
- private static Column [] _link (Column [] orig ) {
865
+ private static Column [] _link (Column [] orig )
866
+ {
841
867
int i = orig .length ;
842
868
Column [] result = new Column [i ];
843
869
Column prev = null ;
844
870
for (; --i >= 0 ; ) {
845
- Column curr = orig [i ].withNext (prev );
871
+ Column curr = orig [i ].withNext (i , prev );
846
872
result [i ] = curr ;
847
873
prev = curr ;
848
874
}
@@ -1080,7 +1106,8 @@ public CsvSchema withoutColumns() {
1080
1106
*
1081
1107
* @since 2.4
1082
1108
*/
1083
- public CsvSchema sortedBy (String ... columnNames ) {
1109
+ public CsvSchema sortedBy (String ... columnNames )
1110
+ {
1084
1111
LinkedHashMap <String ,Column > map = new LinkedHashMap <String ,Column >();
1085
1112
for (String colName : columnNames ) {
1086
1113
Column col = _columnsByName .get (colName );
0 commit comments