package PropertiesOwn; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collection; import java.util.Properties; public abstract class AProperties { protected Properties appProps; protected Prop seed = new Prop<>(Integer.class, "seed"); protected Prop liczbaAgentów = new Prop<>(Integer.class, "liczbaAgentów"); protected Prop prawdSpotkania = new Prop<>(Double.class, "prawdSpotkania"); protected Prop prawdZarażenia = new Prop<>(Double.class, "prawdZarażenia"); protected Prop prawdWyzdrowienia = new Prop<>(Double.class, "prawdWyzdrowienia"); protected Prop śmiertelność = new Prop<>(Double.class, "śmiertelność"); protected Prop liczbaDni = new Prop<>(Integer.class, "liczbaDni"); protected Prop śrZnajomych = new Prop<>(Integer.class, "śrZnajomych"); protected Prop plikZRaportem = new Prop<>(String.class, "plikZRaportem"); protected ArrayList collection = new ArrayList<>(); { collection.add(seed); collection.add(liczbaAgentów); collection.add(prawdSpotkania); collection.add(prawdZarażenia); collection.add(prawdWyzdrowienia); collection.add(śmiertelność); collection.add(liczbaDni); collection.add(śrZnajomych); collection.add(plikZRaportem); } public abstract void read(String x) throws FileNotFoundException; public void mergeProperties (AProperties aProperties) { for (int i=0; i { private E x; private final String s; private final Class clazz; Prop (Class clazz, String s) { this.clazz=clazz; this.s=s; } public E get() { return x; } public void set(E x) { if (x != null) this.x = x; } @Override public String toString() { if (x != null) return x.toString(); return "(null)"; } public String str () { return s; } protected static void incorrectValue(Object value, String var) { System.out.println("Niedozwolona wartość \"" + value.toString() + "\" dla klucza " + var); System.exit(0); } protected static void valueNotExist(String var) { System.out.println("Brak wartości dla klucza " + var); System.exit(0); } protected E getValue (Properties appProps, String s) throws NullPointerException{ String x = appProps.getProperty(s); try { //System.out.println("mam: " + this.x.getClass().getName()); if (clazz.isAssignableFrom(Integer.class)) return (E) Integer.valueOf(x); else if (clazz.isAssignableFrom(Double.class)) return (E) Double.valueOf(x); else if (clazz.isAssignableFrom(String.class)) return (E) String.valueOf(x); return (E) x; } catch (NumberFormatException e) { incorrectValue(x, s); } return null; } } package PropertiesOwn; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.InvalidPropertiesFormatException; import java.util.Properties; public class SimulationConf extends AProperties { @Override public void read(String configPath) { appProps = new Properties(); try { appProps.loadFromXML(new FileInputStream(configPath)); for (Prop obj : collection) { try { obj.set(obj.getValue(appProps, obj.str())); } catch (NullPointerException e) {} } } catch (InvalidPropertiesFormatException e) { System.out.println("simulation-conf.xml nie jest XML"); System.exit(0); e.printStackTrace(); } catch (FileNotFoundException e) { System.out.println("Brak pliku simulation-conf.xml"); System.exit(0); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }