16
16
import java .nio .charset .Charset ;
17
17
import java .nio .file .Files ;
18
18
import java .nio .file .Paths ;
19
- import java .util .*;
19
+ import java .util .ArrayList ;
20
+ import java .util .Collection ;
21
+ import java .util .Collections ;
22
+ import java .util .Iterator ;
23
+ import java .util .LinkedHashMap ;
24
+ import java .util .LinkedList ;
25
+ import java .util .List ;
26
+ import java .util .Map ;
27
+ import java .util .Set ;
28
+
20
29
import org .apache .commons .csv .CSVFormat ;
21
30
import org .apache .commons .csv .CSVParser ;
22
31
import org .apache .commons .csv .CSVRecord ;
23
32
import org .eclipse .epsilon .common .util .FileUtil ;
24
33
import org .eclipse .epsilon .common .util .StringProperties ;
25
34
import org .eclipse .epsilon .eol .exceptions .EolRuntimeException ;
26
- import org .eclipse .epsilon .eol .exceptions .models .*;
35
+ import org .eclipse .epsilon .eol .exceptions .models .EolEnumerationValueNotFoundException ;
36
+ import org .eclipse .epsilon .eol .exceptions .models .EolModelElementTypeNotFoundException ;
37
+ import org .eclipse .epsilon .eol .exceptions .models .EolModelLoadingException ;
38
+ import org .eclipse .epsilon .eol .exceptions .models .EolNotInstantiableModelElementTypeException ;
27
39
import org .eclipse .epsilon .eol .models .CachedModel ;
28
40
import org .eclipse .epsilon .eol .models .IRelativePathResolver ;
29
41
58
70
*/
59
71
public class CsvModel extends CachedModel <Map <String , Object >> {
60
72
73
+ /**
74
+ * Various Epsilon languages assume that different model
75
+ * elements will have different hashcodes (e.g. ETL). This
76
+ * subclass of LinkedHashMap reverts to system identity-based
77
+ * hashcodes, to ensure that.
78
+ */
79
+ protected static class Row extends LinkedHashMap <String , Object > {
80
+ private static final long serialVersionUID = 1L ;
81
+
82
+ @ Override
83
+ public int hashCode () {
84
+ return System .identityHashCode (this );
85
+ }
86
+
87
+ @ Override
88
+ public boolean equals (Object other ) {
89
+ return this == other ;
90
+ }
91
+ }
92
+
61
93
public static final String HEADERLESS_FIELD_NAME = "field" ;
62
94
63
95
/** The Constant PROPERTY_FILE. */
@@ -357,7 +389,7 @@ private String concatenateMap() {
357
389
StringBuilder output = new StringBuilder ();
358
390
if (this .knownHeaders ) {
359
391
// First line is the headers
360
- Iterator <String > keyIt = (( LinkedList < Map < String , Object >>) rows ). getFirst ( ).keySet ().iterator ();
392
+ Iterator <String > keyIt = rows . get ( 0 ).keySet ().iterator ();
361
393
output .append (keyIt .next ());
362
394
while (keyIt .hasNext ()) {
363
395
output .append (this .fieldSeparator );
@@ -412,12 +444,13 @@ protected Collection<Map<String, Object>> getAllOfKindFromModel(String kind) thr
412
444
* @see org.eclipse.epsilon.eol.models.CachedModel#createInstanceInModel(java.lang.String)
413
445
*/
414
446
@ Override
415
- protected Map < String , Object > createInstanceInModel (String type ) throws EolModelElementTypeNotFoundException , EolNotInstantiableModelElementTypeException {
447
+ protected Row createInstanceInModel (String type ) throws EolModelElementTypeNotFoundException , EolNotInstantiableModelElementTypeException {
416
448
if (!"Row" .equals (type )) {
417
449
throw new EolModelElementTypeNotFoundException (this .name , type );
418
450
}
419
- Map <String , Object > returnVal = new LinkedHashMap <>();
420
- for (String key : ((LinkedList <Map <String , Object >>) rows ).getFirst ().keySet ()) {
451
+
452
+ Row returnVal = new Row ();
453
+ for (String key : rows .get (0 ).keySet ()) {
421
454
returnVal .put (key , "" );
422
455
}
423
456
rows .add (returnVal );
@@ -440,7 +473,7 @@ public boolean hasProperty(String type, String property) throws EolModelElementT
440
473
if (!this .knownHeaders ) {
441
474
return property .equals (HEADERLESS_FIELD_NAME );
442
475
} else {
443
- return (( LinkedHashMap < String , Object >) (( LinkedList < Map < String , Object >>) rows ). getFirst () ).keySet ().contains (property );
476
+ return rows . get ( 0 ).keySet ().contains (property );
444
477
}
445
478
}
446
479
@@ -471,7 +504,7 @@ protected static List<Map<String, Object>> createRows(BufferedReader reader,
471
504
try (CSVParser records = csvFormat .parse (reader )) {
472
505
if (knownHeaders ) {
473
506
for (CSVRecord record : records ) {
474
- LinkedHashMap < String , Object > row = new LinkedHashMap <> ();
507
+ Row row = new Row ();
475
508
if (!varargsHeaders ) {
476
509
for (Map .Entry <String , String > entry : record .toMap ().entrySet ()) {
477
510
row .put (entry .getKey (), entry .getValue ());
@@ -502,7 +535,7 @@ protected static List<Map<String, Object>> createRows(BufferedReader reader,
502
535
for (CSVRecord record : records ) {
503
536
List <String > values = new ArrayList <>();
504
537
record .iterator ().forEachRemaining (values ::add );
505
- LinkedHashMap < String , Object > row = new LinkedHashMap <> ();
538
+ Row row = new Row ();
506
539
row .put ("field" , values );
507
540
rows .add (row );
508
541
}
0 commit comments