33import java .nio .file .Paths ;
44import java .util .HashMap ;
55import java .util .Map ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
68
79public class concoredocker {
810 private static Map <String , Object > iport = new HashMap <>();
@@ -40,7 +42,8 @@ public static void main(String[] args) {
4042 System .out .println ("converted sparams: " + sparams );
4143 }
4244 try {
43- params = literalEval (sparams );
45+ // literalEval returns a proper Map for "{...}"
46+ params = (Map <String , Object >) literalEval (sparams );
4447 } catch (Exception e ) {
4548 System .out .println ("bad params: " + sparams );
4649 }
@@ -51,15 +54,17 @@ public static void main(String[] args) {
5154 defaultMaxTime (100 );
5255 }
5356
57+ @ SuppressWarnings ("unchecked" )
5458 private static Map <String , Object > parseFile (String filename ) throws IOException {
5559 String content = new String (Files .readAllBytes (Paths .get (filename )));
56- return literalEval (content );
60+ return ( Map < String , Object >) literalEval (content ); // Casted to Map
5761 }
5862
5963 private static void defaultMaxTime (int defaultValue ) {
6064 try {
6165 String content = new String (Files .readAllBytes (Paths .get (inpath + "1/concore.maxtime" )));
62- maxtime = literalEval (content ).size ();
66+ // changed assumption from map to list for maxtime, as it usually represents a list of time steps
67+ maxtime = ((List <?>) literalEval (content )).size ();
6368 } catch (IOException e ) {
6469 maxtime = defaultValue ;
6570 }
@@ -90,10 +95,10 @@ private static Object read(int port, String name, String initstr) {
9095 retrycount ++;
9196 }
9297 s += ins ;
93- Object [] inval = new Map [] { literalEval (ins ) };
98+ Object [] inval = (( List <?>) literalEval (ins )). toArray (); // FIXED: Casted to List, converted to Array
9499 int simtime = Math .max ((int ) inval [0 ], 0 ); // assuming simtime is an integer
95100 return inval [1 ];
96- } catch (IOException | InterruptedException e ) {
101+ } catch (IOException | InterruptedException | ClassCastException e ) {
97102 return initstr ;
98103 }
99104 }
@@ -132,7 +137,7 @@ private static Object[] initVal(String simtimeVal) {
132137 int simtime = 0 ;
133138 Object [] val = new Object [] {};
134139 try {
135- Object [] arrayVal = new Map [] { literalEval (simtimeVal ) };
140+ Object [] arrayVal = (( List <?>) literalEval (simtimeVal )). toArray (); // FIXED: Casted to List, converted to Array
136141 simtime = (int ) arrayVal [0 ]; // assuming simtime is an integer
137142 val = new Object [arrayVal .length - 1 ];
138143 System .arraycopy (arrayVal , 1 , val , 0 , val .length );
@@ -142,8 +147,38 @@ private static Object[] initVal(String simtimeVal) {
142147 return val ;
143148 }
144149
145- private static Map <String , Object > literalEval (String s ) {
150+ // custom parser
151+ private static Object literalEval (String s ) {
152+ s = s .trim ();
153+ if (s .startsWith ("{" ) && s .endsWith ("}" )) {
154+ Map <String , Object > map = new HashMap <>();
155+ String content = s .substring (1 , s .length () - 1 );
156+ if (content .isEmpty ()) return map ;
157+ for (String pair : content .split ("," )) {
158+ String [] kv = pair .split (":" );
159+ if (kv .length == 2 ) map .put ((String ) parseVal (kv [0 ]), parseVal (kv [1 ]));
160+ }
161+ return map ;
162+ } else if (s .startsWith ("[" ) && s .endsWith ("]" )) {
163+ List <Object > list = new ArrayList <>();
164+ String content = s .substring (1 , s .length () - 1 );
165+ if (content .isEmpty ()) return list ;
166+ for (String val : content .split ("," )) {
167+ list .add (parseVal (val ));
168+ }
169+ return list ;
170+ }
171+ return parseVal (s );
172+ }
146173
147- return new HashMap <>();
174+ // helper: Converts Python types to Java primitives
175+ private static Object parseVal (String s ) {
176+ s = s .trim ().replace ("'" , "" ).replace ("\" " , "" );
177+ if (s .equalsIgnoreCase ("True" )) return true ;
178+ if (s .equalsIgnoreCase ("False" )) return false ;
179+ if (s .equalsIgnoreCase ("None" )) return null ;
180+ try { return Integer .parseInt (s ); } catch (NumberFormatException e1 ) {
181+ try { return Double .parseDouble (s ); } catch (NumberFormatException e2 ) { return s ; }
182+ }
148183 }
149- }
184+ }
0 commit comments