1616import java .nio .charset .Charset ;
1717import java .nio .file .Files ;
1818import 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+
2029import org .apache .commons .csv .CSVFormat ;
2130import org .apache .commons .csv .CSVParser ;
2231import org .apache .commons .csv .CSVRecord ;
2332import org .eclipse .epsilon .common .util .FileUtil ;
2433import org .eclipse .epsilon .common .util .StringProperties ;
2534import 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 ;
2739import org .eclipse .epsilon .eol .models .CachedModel ;
2840import org .eclipse .epsilon .eol .models .IRelativePathResolver ;
2941
5870 */
5971public class CsvModel extends CachedModel <Map <String , Object >> {
6072
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+
6193 public static final String HEADERLESS_FIELD_NAME = "field" ;
6294
6395 /** The Constant PROPERTY_FILE. */
@@ -357,7 +389,7 @@ private String concatenateMap() {
357389 StringBuilder output = new StringBuilder ();
358390 if (this .knownHeaders ) {
359391 // 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 ();
361393 output .append (keyIt .next ());
362394 while (keyIt .hasNext ()) {
363395 output .append (this .fieldSeparator );
@@ -412,12 +444,13 @@ protected Collection<Map<String, Object>> getAllOfKindFromModel(String kind) thr
412444 * @see org.eclipse.epsilon.eol.models.CachedModel#createInstanceInModel(java.lang.String)
413445 */
414446 @ Override
415- protected Map < String , Object > createInstanceInModel (String type ) throws EolModelElementTypeNotFoundException , EolNotInstantiableModelElementTypeException {
447+ protected Row createInstanceInModel (String type ) throws EolModelElementTypeNotFoundException , EolNotInstantiableModelElementTypeException {
416448 if (!"Row" .equals (type )) {
417449 throw new EolModelElementTypeNotFoundException (this .name , type );
418450 }
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 ()) {
421454 returnVal .put (key , "" );
422455 }
423456 rows .add (returnVal );
@@ -440,7 +473,7 @@ public boolean hasProperty(String type, String property) throws EolModelElementT
440473 if (!this .knownHeaders ) {
441474 return property .equals (HEADERLESS_FIELD_NAME );
442475 } else {
443- return (( LinkedHashMap < String , Object >) (( LinkedList < Map < String , Object >>) rows ). getFirst () ).keySet ().contains (property );
476+ return rows . get ( 0 ).keySet ().contains (property );
444477 }
445478 }
446479
@@ -471,7 +504,7 @@ protected static List<Map<String, Object>> createRows(BufferedReader reader,
471504 try (CSVParser records = csvFormat .parse (reader )) {
472505 if (knownHeaders ) {
473506 for (CSVRecord record : records ) {
474- LinkedHashMap < String , Object > row = new LinkedHashMap <> ();
507+ Row row = new Row ();
475508 if (!varargsHeaders ) {
476509 for (Map .Entry <String , String > entry : record .toMap ().entrySet ()) {
477510 row .put (entry .getKey (), entry .getValue ());
@@ -502,7 +535,7 @@ protected static List<Map<String, Object>> createRows(BufferedReader reader,
502535 for (CSVRecord record : records ) {
503536 List <String > values = new ArrayList <>();
504537 record .iterator ().forEachRemaining (values ::add );
505- LinkedHashMap < String , Object > row = new LinkedHashMap <> ();
538+ Row row = new Row ();
506539 row .put ("field" , values );
507540 rows .add (row );
508541 }
0 commit comments