@@ -85,16 +85,25 @@ public String toString() {
8585
8686 private ArrayList <DataVariable > variables = new ArrayList <>();
8787
88- // public static void main(String[] args) {
89- // PDataStorage ds = new PDataStorage();
90-
91- // ds.add("teststring", "asdfe4[]");
92- // ds.save("testfile.paulsen");
93-
94- // ds.read("testfile.paulsen");
95- // String s = ds.getString("teststring");
96- // System.out.println(s);
97- // }
88+ // public static void main(String[] args) {
89+ //
90+ // PDataStorage ds = new PDataStorage();
91+ //
92+ // ds.add("string", "asdöfljk");
93+ // ds.add("int", 0);
94+ // ds.add("long", 0L);
95+ // ds.add("float", 0.0f);
96+ // ds.add("bool", false);
97+ // ds.save("testfile.paulsen");
98+ //
99+ // ds = new PDataStorage();
100+ // ds.read("testfile.paulsen");
101+ // System.out.println(ds.getString("string"));
102+ // System.out.println(ds.getInteger("int"));
103+ // System.out.println(ds.getLong("long"));
104+ // System.out.println(ds.getFloat("float"));
105+ // System.out.println(ds.getBoolean("bool"));
106+ // }
98107
99108 public void save (String path ) {
100109 PFile file = new PFile (path );
@@ -107,11 +116,14 @@ public void save(String path) {
107116 }
108117
109118 /**
110- * Reads dataStorage-file and stores all Data in this object
119+ * Reads dataStorage-file and stores all Data in this object.
120+ * <br>
121+ * Ignores Variables with no valid value
111122 *
112123 * @param path
124+ * @throws IllegalArgumentException when file does not meet the expectations of this format
113125 */
114- public void read (String path ) {
126+ public void read (String path ) throws IllegalArgumentException {
115127 if (PFolder .isFolder (path ))
116128 return ;
117129
@@ -123,7 +135,6 @@ public void read(String path) {
123135 String variableName = "" ;
124136 String data = "" ;
125137
126- loop :
127138 for (int i = 0 ; i < file .length (); i ++) {
128139 char c = file .charAt (i );
129140
@@ -145,8 +156,7 @@ public void read(String path) {
145156 currentDataType = DataType .BOOLEAN ;
146157 break ;
147158 default :
148- System .err .println ("[DataStorage]::could not identify datatype '" + c + "' at pos " + i );
149- break loop ;
159+ throw new IllegalArgumentException ("could not identify datatype '\" + c + \" ' at pos " + i );
150160 }
151161 hasBeenDataType = true ;
152162 i ++; // jump over [
@@ -160,8 +170,13 @@ public void read(String path) {
160170 if (c != ']' ) {
161171 data += c ;
162172 } else {
163- addData (currentDataType , variableName .replace ("^<<^" , "[" ).replace ("^>>^" , "]" ),
164- data .replace ("^<<^" , "[" ).replace ("^>>^" , "]" ));
173+ try {
174+ addData (currentDataType , variableName .replace ("^<<^" , "[" ).replace ("^>>^" , "]" ),
175+ data .replace ("^<<^" , "[" ).replace ("^>>^" , "]" ));
176+ } catch (IllegalArgumentException e ) {
177+ } catch (Exception e ) {
178+ throw e ;
179+ }
165180 variableName = "" ;
166181 data = "" ;
167182 hasBeenDataType = false ;
@@ -172,12 +187,19 @@ public void read(String path) {
172187 }
173188 }
174189
175- public boolean addData (DataType type , String name , String data ) {
190+ public boolean addData (DataType type , String name , String data ) throws IllegalArgumentException {
176191 try {
177192 switch (type ) {
178193 case BOOLEAN :
179- add (name , Boolean .valueOf (data ));
180- break ;
194+ if (data != null ) {
195+ if (data .toLowerCase ().equals ("true" ))
196+ add (name , true );
197+ else if (data .toLowerCase ().equals ("false" ))
198+ add (name , false );
199+ else
200+ throw new IllegalArgumentException ("Could not find \" " + type + " " + name + "\" " );
201+ break ;
202+ }
181203 case FLOAT :
182204 add (name , Float .valueOf (data ));
183205 break ;
@@ -188,15 +210,14 @@ public boolean addData(DataType type, String name, String data) {
188210 add (name , Long .valueOf (data ));
189211 break ;
190212 case STRING :
191- add (name , String .valueOf (data ));
213+ add (name , data == null ? "" : String .valueOf (data ));
192214 break ;
193215 default :
194216 System .err .println ("[DataStorage]::UNKNOWN datatype!" );
195217 return false ;
196218 }
197219 } catch (Exception e ) {
198- System .err .println ("[DataStorage]::Some error occured when adding data!" );
199- e .printStackTrace ();
220+ throw new IllegalArgumentException ("Can not parse the data into this DataType!" );
200221 }
201222 return true ;
202223 }
@@ -221,31 +242,31 @@ public void add(String name, boolean data) {
221242 variables .add (new DataVariable (name , data ));
222243 }
223244
224- public Object get (DataType type , String name ) {
245+ public Object get (DataType type , String name ) throws IllegalArgumentException {
225246 for (DataVariable v : variables ) {
226247 if (v .dt == type && v .name .equals (name ))
227248 return v .obj ;
228249 }
229- return null ;
250+ throw new IllegalArgumentException ( "Could not find \" " + type + " " + name + " \" " ) ;
230251 }
231252
232- public String getString (String name ) {
253+ public String getString (String name ) throws IllegalArgumentException {
233254 return (String ) get (DataType .STRING , name );
234255 }
235256
236- public long getLong (String name ) {
257+ public long getLong (String name ) throws IllegalArgumentException {
237258 return (long ) get (DataType .LONG , name );
238259 }
239260
240- public int getInteger (String name ) {
261+ public int getInteger (String name ) throws IllegalArgumentException {
241262 return (int ) get (DataType .INTEGER , name );
242263 }
243264
244- public float getFloat (String name ) {
265+ public float getFloat (String name ) throws IllegalArgumentException {
245266 return (float ) get (DataType .FLOAT , name );
246267 }
247268
248- public boolean getBoolean (String name ) {
269+ public boolean getBoolean (String name ) throws IllegalArgumentException {
249270 return (boolean ) get (DataType .BOOLEAN , name );
250271 }
251272
0 commit comments