package PropertiesOwn;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
public abstract class AProperties {
protected Prop
<Integer
> seed
= new Prop
<>(Integer.
class,
"seed");
protected Prop
<Integer
> liczbaAgentów
= new Prop
<>(Integer.
class,
"liczbaAgentów");
protected Prop
<Double
> prawdSpotkania
= new Prop
<>(Double.
class,
"prawdSpotkania");
protected Prop
<Double
> prawdZarażenia
= new Prop
<>(Double.
class,
"prawdZarażenia");
protected Prop
<Double
> prawdWyzdrowienia
= new Prop
<>(Double.
class,
"prawdWyzdrowienia");
protected Prop
<Double
> śmiertelność
= new Prop
<>(Double.
class,
"śmiertelność");
protected Prop
<Integer
> liczbaDni
= new Prop
<>(Integer.
class,
"liczbaDni");
protected Prop
<Integer
> śrZnajomych
= new Prop
<>(Integer.
class,
"śrZnajomych");
protected Prop
<String
> plikZRaportem
= new Prop
<>(String.
class,
"plikZRaportem");
protected ArrayList <Prop
> 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 void mergeProperties (AProperties aProperties) {
for (int i=0; i<collection.size(); i++) {
collection.get(i).set(aProperties.collection.get(i).get());
}
}
public void checkProperties () {
for (int i=0; i<collection.size(); i++) {
if (collection.get(i).get() == null)
Prop.valueNotExist(collection.get(i).str());
}
}
@Override
StringBuilder s = new StringBuilder();
for (Prop obj : collection) {
s.append(obj.str() + "=" + obj+"\n");
}
return s.toString();
}
public Integer getSeed
() { return seed.
get(); }
public Double getPrawdSpotkania
() { return prawdSpotkania.
get(); }
public Double getPrawdWyzdrowienia
() { return prawdWyzdrowienia.
get(); }
public Integer getLiczbaDni
() { return liczbaDni.
get(); }
public Double getPrawdZarażenia
() { return prawdZarażenia.
get(); }
public Double getŚmiertelność
() { return śmiertelność.
get(); }
public Integer getLiczbaAgentów
() { return liczbaAgentów.
get(); }
public Integer getŚrZnajomych
() { return śrZnajomych.
get(); }
public String getPlikZRaportem
() { return plikZRaportem.
get(); }
}
package PropertiesOwn;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
import java.util.Properties;
public class DefaultProperties extends AProperties {
@Override
public void read
(String configPath
) {
try {
for (Prop obj : collection) {
try {
obj.set(obj.getValue(appProps, obj.str()));
//Prop.valueNotExist(obj.str());
}
}
} catch (MalformedInputException e) {
System.
out.
println("default.properties nie jest tekstowy");
//e.printStackTrace();
System.
out.
println("Brak pliku default.properties");
}
}
}
package PropertiesOwn;
import java.lang.reflect.Array;
import java.util.Properties;
public class Prop <E extends Object> {
private E x;
private final Class<?> clazz;
Prop
(Class
<E
> 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
if (x != null)
return x.toString();
return "(null)";
}
return s;
}
protected static void incorrectValue
(Object value,
String var
) {
System.
out.
println("Niedozwolona wartość \"" + value.
toString() + "\" dla klucza " + var
);
}
protected static void valueNotExist
(String var
) {
System.
out.
println("Brak wartości dla klucza " + var
);
}
String x
= appProps.
getProperty(s
);
try {
//System.out.println("mam: " + this.x.getClass().getName());
if (clazz.
isAssignableFrom(Integer.
class))
else if (clazz.
isAssignableFrom(Double.
class))
else if (clazz.
isAssignableFrom(String.
class))
return (E) x;
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
) {
try {
for (Prop obj : collection) {
try {
obj.set(obj.getValue(appProps, obj.str()));
}
} catch (InvalidPropertiesFormatException e) {
System.
out.
println("simulation-conf.xml nie jest XML");
e.printStackTrace();
System.
out.
println("Brak pliku simulation-conf.xml");
e.printStackTrace();
e.printStackTrace();
}
}
}