Facebook
From das, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 231
  1. package PropertiesOwn;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.Properties;
  7.  
  8. public abstract class AProperties {
  9.     protected Properties appProps;
  10.  
  11.     protected Prop <Integer> seed = new Prop<>(Integer.class, "seed");
  12.     protected Prop <Integer> liczbaAgentów = new Prop<>(Integer.class, "liczbaAgentów");
  13.     protected Prop <Double> prawdSpotkania = new Prop<>(Double.class, "prawdSpotkania");
  14.     protected Prop <Double> prawdZarażenia = new Prop<>(Double.class, "prawdZarażenia");
  15.     protected Prop <Double> prawdWyzdrowienia = new Prop<>(Double.class, "prawdWyzdrowienia");
  16.     protected Prop <Double> śmiertelność = new Prop<>(Double.class, "śmiertelność");
  17.     protected Prop <Integer> liczbaDni = new Prop<>(Integer.class, "liczbaDni");
  18.     protected Prop <Integer> śrZnajomych = new Prop<>(Integer.class, "śrZnajomych");
  19.     protected Prop <String> plikZRaportem = new Prop<>(String.class, "plikZRaportem");
  20.  
  21.     protected ArrayList <Prop> collection = new ArrayList<>();
  22.  
  23.     {
  24.         collection.add(seed);
  25.         collection.add(liczbaAgentów);
  26.         collection.add(prawdSpotkania);
  27.         collection.add(prawdZarażenia);
  28.         collection.add(prawdWyzdrowienia);
  29.         collection.add(śmiertelność);
  30.         collection.add(liczbaDni);
  31.         collection.add(śrZnajomych);
  32.         collection.add(plikZRaportem);
  33.     }
  34.  
  35.     public abstract void read(String x) throws FileNotFoundException;
  36.  
  37.     public void mergeProperties (AProperties aProperties) {
  38.         for (int i=0; i<collection.size(); i++) {
  39.             collection.get(i).set(aProperties.collection.get(i).get());
  40.         }
  41.     }
  42.  
  43.     public void checkProperties () {
  44.         for (int i=0; i<collection.size(); i++) {
  45.             if (collection.get(i).get() == null)
  46.                 Prop.valueNotExist(collection.get(i).str());
  47.         }
  48.     }
  49.  
  50.     @Override
  51.     public String toString() {
  52.         StringBuilder s = new StringBuilder();
  53.         for (Prop obj : collection) {
  54.             s.append(obj.str() + "=" + obj+"\n");
  55.         }
  56.         return s.toString();
  57.     }
  58.  
  59.  
  60.     public Integer getSeed() { return seed.get(); }
  61.     public Double getPrawdSpotkania() { return prawdSpotkania.get(); }
  62.  
  63.     public Double getPrawdWyzdrowienia() { return prawdWyzdrowienia.get(); }
  64.  
  65.     public Integer getLiczbaDni() { return liczbaDni.get(); }
  66.  
  67.     public Double getPrawdZarażenia() { return prawdZarażenia.get(); }
  68.  
  69.     public Double getŚmiertelność() { return śmiertelność.get(); }
  70.  
  71.     public Integer getLiczbaAgentów() { return liczbaAgentów.get(); }
  72.  
  73.     public Integer getŚrZnajomych() { return śrZnajomych.get(); }
  74.  
  75.     public String getPlikZRaportem() { return plikZRaportem.get(); }
  76. }
  77.  
  78.  
  79.  
  80. package PropertiesOwn;
  81.  
  82. import java.io.FileInputStream;
  83. import java.io.IOException;
  84. import java.io.InputStreamReader;
  85. import java.nio.charset.Charset;
  86. import java.nio.charset.MalformedInputException;
  87. import java.util.Properties;
  88.  
  89. public class DefaultProperties extends AProperties {
  90.  
  91.     @Override
  92.     public void read(String configPath) {
  93.         appProps = new Properties();
  94.         try {
  95.             appProps.load(new InputStreamReader(new FileInputStream(configPath), Charset.forName("UTF-8")));
  96.             for (Prop obj : collection) {
  97.                 try {
  98.                     obj.set(obj.getValue(appProps, obj.str()));
  99.                 } catch (NullPointerException e) {
  100.                     //Prop.valueNotExist(obj.str());
  101.                 }
  102.             }
  103.         } catch (MalformedInputException e) {
  104.             System.out.println("default.properties nie jest tekstowy");
  105.             System.exit(0);
  106.         } catch (IOException e) {
  107.             //e.printStackTrace();
  108.             System.out.println("Brak pliku default.properties");
  109.             System.exit(0);
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115.  
  116. package PropertiesOwn;
  117.  
  118. import java.lang.reflect.Array;
  119. import java.util.Properties;
  120.  
  121. public class Prop <E extends Object> {
  122.     private E x;
  123.     private final String s;
  124.     private final Class<?> clazz;
  125.  
  126.     Prop (Class<E> clazz, String s) {
  127.         this.clazz=clazz;
  128.         this.s=s;
  129.     }
  130.  
  131.     public E get() {
  132.         return x;
  133.     }
  134.  
  135.     public void set(E x) {
  136.         if (x != null)
  137.             this.x = x;
  138.     }
  139.  
  140.     @Override
  141.     public String toString() {
  142.         if (x != null)
  143.             return x.toString();
  144.         return "(null)";
  145.     }
  146.  
  147.     public String str () {
  148.         return s;
  149.     }
  150.  
  151.     protected static void incorrectValue(Object value, String var) {
  152.         System.out.println("Niedozwolona wartość \"" + value.toString() + "\" dla klucza " + var);
  153.         System.exit(0);
  154.     }
  155.  
  156.     protected static void valueNotExist(String var) {
  157.         System.out.println("Brak wartości dla klucza " + var);
  158.         System.exit(0);
  159.     }
  160.  
  161.     protected E getValue (Properties appProps, String s) throws NullPointerException{
  162.         String x = appProps.getProperty(s);
  163.         try {
  164.             //System.out.println("mam: " + this.x.getClass().getName());
  165.             if (clazz.isAssignableFrom(Integer.class))
  166.                 return (E) Integer.valueOf(x);
  167.             else if (clazz.isAssignableFrom(Double.class))
  168.                 return (E) Double.valueOf(x);
  169.             else if (clazz.isAssignableFrom(String.class))
  170.                 return (E) String.valueOf(x);
  171.             return (E) x;
  172.         } catch (NumberFormatException e) {
  173.             incorrectValue(x, s);
  174.         }
  175.         return null;
  176.     }
  177. }
  178.  
  179.  
  180. package PropertiesOwn;
  181.  
  182. import java.io.FileInputStream;
  183. import java.io.FileNotFoundException;
  184. import java.io.IOException;
  185. import java.util.InvalidPropertiesFormatException;
  186. import java.util.Properties;
  187.  
  188. public class SimulationConf extends AProperties {
  189.     @Override
  190.     public void read(String configPath) {
  191.         appProps = new Properties();
  192.         try {
  193.             appProps.loadFromXML(new FileInputStream(configPath));
  194.             for (Prop obj : collection) {
  195.                 try {
  196.                     obj.set(obj.getValue(appProps, obj.str()));
  197.                 } catch (NullPointerException e) {}
  198.             }
  199.         } catch (InvalidPropertiesFormatException e) {
  200.             System.out.println("simulation-conf.xml nie jest XML");
  201.             System.exit(0);
  202.             e.printStackTrace();
  203.         } catch (FileNotFoundException e) {
  204.             System.out.println("Brak pliku simulation-conf.xml");
  205.             System.exit(0);
  206.             e.printStackTrace();
  207.         } catch (IOException e) {
  208.             e.printStackTrace();
  209.         }
  210.     }
  211. }
  212.  
  213.