1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.oneandone.sushi;
17
18 import net.oneandone.sushi.fs.Node;
19 import net.oneandone.sushi.fs.World;
20 import net.oneandone.sushi.metadata.Type;
21 import net.oneandone.sushi.metadata.reflect.ReflectSchema;
22
23 import java.io.IOException;
24
25
26
27
28
29
30 public class PropertiesSample {
31 private static final World world = new World();
32
33
34 public static void main(String[] args) throws IOException {
35 Node file;
36 Config config;
37
38 file = world.getTemp().createTempFile();
39 config = new Config();
40 config.save(file);
41 System.out.println("default config created:");
42 System.out.println(file.readString());
43
44 config.javaHome = "changed";
45 config.version = 2;
46 config.save(file);
47 System.out.println("saved changes:");
48 System.out.println(file.readString());
49
50 config = Config.load(file);
51 System.out.println("loaded config: " + config);
52 }
53
54 public static class Config {
55 private static final Type TYPE = new ReflectSchema(world).type(Config.class);
56
57 public static Config load(Node file) throws IOException {
58 return (Config) TYPE.loadProperties(file.readProperties()).get();
59 }
60
61 public String javaHome;
62 public int version;
63 public Complex complex;
64
65 public Config() {
66 this("", 0, new Complex());
67 }
68
69 public Config(String javaHome, int version, Complex complex) {
70 this.javaHome = javaHome;
71 this.version = version;
72 this.complex = complex;
73 }
74
75 public void save(Node file) throws IOException {
76 file.writeProperties(TYPE.instance(this).toProperties());
77 }
78
79 @Override
80 public String toString() {
81 return "javaHome=" + javaHome + ", version=" + version;
82 }
83 }
84
85 public static class Complex {
86 public int left;
87 public int right;
88 }
89 }