1414public abstract class BaseElement {
1515
1616 private static final String JAVA_IO_TEMPDIR = System .getProperty ("java.io.tmpdir" );
17- public static final String EV3DEV_FAKE_SYSTEM_PATH = JAVA_IO_TEMPDIR + "/" + " ev3dev_fake_system" ;
17+ public static final String EV3DEV_FAKE_SYSTEM_PATH = Paths . get ( JAVA_IO_TEMPDIR , " ev3dev_fake_system"). toString () ;
1818
1919 protected static final String LEGO_PORT_PATH = "lego-port" ;
2020 protected static final String PORT = "port" ;
@@ -34,14 +34,9 @@ public abstract class BaseElement {
3434 protected static final String SENSOR_ADDRESS = "address" ;
3535 protected static final String SENSOR_MODE = "mode" ;
3636 protected static final String SENSOR_COMMAND = "command" ;
37- protected static final String VALUE0 = "value0" ;
38- protected static final String VALUE1 = "value1" ;
39- protected static final String VALUE2 = "value2" ;
40- protected static final String VALUE3 = "value3" ;
41- protected static final String VALUE4 = "value4" ;
42- protected static final String VALUE5 = "value5" ;
43- protected static final String VALUE6 = "value6" ;
44- protected static final String VALUE7 = "value7" ;
37+
38+ protected static final String SENSOR1_BASE = Paths .get (EV3DEV_FAKE_SYSTEM_PATH , LEGO_SENSOR_PATH , SENSOR1 ).toString ();
39+ protected static final String PORT1_BASE = Paths .get (EV3DEV_FAKE_SYSTEM_PATH , LEGO_SENSOR_PATH , PORT1 ).toString ();
4540
4641 /**
4742 *
@@ -65,91 +60,32 @@ protected void createDirectories(final Path path) throws IOException {
6560 System .out .println (Files .exists (path ));
6661 }
6762
63+ protected void createDirectoriesDirect (final String first , final String ... more ) throws IOException {
64+ createDirectories (Paths .get (first , more ));
65+ }
66+
6867 protected void createFile (final Path path ) throws IOException {
69- if (!Files .exists (path )) {
68+ if (!Files .exists (path )) {
7069 Files .createFile (path );
7170 }
7271 }
7372
73+ protected void writeFileDirect (final String contents ,
74+ final String first ,
75+ final String ... more ) throws IOException {
76+ createFile (Paths .get (first , more ), contents );
77+ }
78+
7479 protected void createFile (final Path path , final String value ) throws IOException {
7580 this .createFile (path );
7681 Files .write (path , value .getBytes ());
7782 }
7883
7984 protected void populateValues (final List <Integer > values ) throws IOException {
80-
81- if (values .size () >= 1 ) {
82- Path value0 = Paths .get (
83- EV3DEV_FAKE_SYSTEM_PATH + "/" +
84- LEGO_SENSOR_PATH + "/" +
85- SENSOR1 + "/" +
86- VALUE0 );
87- createFile (value0 , String .valueOf (values .get (0 )));
85+ for (int i = 0 ; i < values .size (); i ++) {
86+ Path value = Paths .get (SENSOR1_BASE , "value" + i );
87+ createFile (value , String .valueOf (values .get (i )));
8888 }
89-
90- if (values .size () >= 2 ) {
91- Path value1 = Paths .get (
92- EV3DEV_FAKE_SYSTEM_PATH + "/" +
93- LEGO_SENSOR_PATH + "/" +
94- SENSOR1 + "/" +
95- VALUE1 );
96- createFile (value1 , String .valueOf (values .get (1 )));
97- }
98-
99- if (values .size () >= 3 ) {
100- Path value2 = Paths .get (
101- EV3DEV_FAKE_SYSTEM_PATH + "/" +
102- LEGO_SENSOR_PATH + "/" +
103- SENSOR1 + "/" +
104- VALUE2 );
105- createFile (value2 , String .valueOf (values .get (2 )));
106- }
107-
108- if (values .size () >= 4 ) {
109- Path value3 = Paths .get (
110- EV3DEV_FAKE_SYSTEM_PATH + "/" +
111- LEGO_SENSOR_PATH + "/" +
112- SENSOR1 + "/" +
113- VALUE3 );
114- createFile (value3 , String .valueOf (values .get (3 )));
115- }
116-
117- if (values .size () >= 5 ) {
118- Path value4 = Paths .get (
119- EV3DEV_FAKE_SYSTEM_PATH + "/" +
120- LEGO_SENSOR_PATH + "/" +
121- SENSOR1 + "/" +
122- VALUE4 );
123- createFile (value4 , String .valueOf (values .get (4 )));
124- }
125-
126- if (values .size () >= 6 ) {
127- Path value5 = Paths .get (
128- EV3DEV_FAKE_SYSTEM_PATH + "/" +
129- LEGO_SENSOR_PATH + "/" +
130- SENSOR1 + "/" +
131- VALUE5 );
132- createFile (value5 , String .valueOf (values .get (5 )));
133- }
134-
135- if (values .size () >= 7 ) {
136- Path value6 = Paths .get (
137- EV3DEV_FAKE_SYSTEM_PATH + "/" +
138- LEGO_SENSOR_PATH + "/" +
139- SENSOR1 + "/" +
140- VALUE6 );
141- createFile (value6 , String .valueOf (values .get (6 )));
142- }
143-
144- if (values .size () >= 8 ) {
145- Path value7 = Paths .get (
146- EV3DEV_FAKE_SYSTEM_PATH + "/" +
147- LEGO_SENSOR_PATH + "/" +
148- SENSOR1 + "/" +
149- VALUE7 );
150- createFile (value7 , String .valueOf (values .get (7 )));
151- }
152-
15389 }
15490
15591}
0 commit comments