1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.application.state.impl;
21
22 import com.cosylab.application.state.State;
23 import com.cosylab.application.state.StateStorage;
24
25 import java.io.BufferedInputStream;
26 import java.io.BufferedOutputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38
39
40
41
42
43
44
45
46 abstract public class DefaultStateStorage implements StateStorage
47 {
48 protected ArrayList states = new ArrayList();
49 protected StateStorage delegate = null;
50
51
52
53
54 public DefaultStateStorage()
55 {
56 super();
57 }
58
59
60
61
62
63
64 public DefaultStateStorage(StateStorage other)
65 {
66 super();
67 delegate = other;
68
69
70 }
71
72
73
74
75 public void add(State state)
76 {
77 states.add(state);
78 }
79
80
81
82
83 public void addAll(List states)
84 {
85 this.states.addAll(states);
86 }
87
88 protected InputStream getInputStream(String filePath, String fileName)
89 {
90
91 File file = new File(filePath + File.separatorChar + fileName);
92
93 if(!file.exists() || !file.canRead()) {
94 return null;
95 }
96
97 BufferedInputStream bis;
98
99 try {
100 bis = new BufferedInputStream(new FileInputStream(file));
101 } catch(FileNotFoundException e) {
102 return null;
103 }
104
105 return bis;
106 }
107
108 protected OutputStream getOutputStream(String filePath, String fileName)
109 throws IOException
110 {
111
112 File file = new File(filePath + File.separatorChar + fileName);
113
114
115 if(!file.getParentFile().exists()) {
116 file.getParentFile().mkdirs();
117 }
118
119
120 return new BufferedOutputStream(new FileOutputStream(file));
121 }
122
123
124
125
126 abstract public void load(String filePath, String applicationName)
127 throws IOException;
128
129
130
131
132
133
134
135
136
137 abstract public void store(String filePath, String applicationName)
138 throws IOException;
139
140
141
142
143 public List getStates()
144 {
145 return states;
146 }
147
148
149
150
151 public boolean contains(State st) {
152 return states.contains(st);
153 }
154
155
156
157
158 public Iterator iterator() {
159 return states.iterator();
160 }
161
162
163
164
165 public void remove(State st) {
166 states.remove(st);
167 }
168
169 }
170
171