View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
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   * This class is an abstract class that can be extended by the
42   * <code>StateStorage</code> implementators.
43   *
44   * @author dvitas
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  	 * Creates a new DefaultStateStorage object.
61  	 *
62  	 * @param other DOCUMENT ME!
63  	 */
64  	public DefaultStateStorage(StateStorage other)
65  	{
66  		super();
67  		delegate = other;
68  
69  		//states.addAll(other.getStates());
70  	}
71  
72  	/* (non-Javadoc)
73  	 * @see com.cosylab.application.state.StateStorage#add(com.cosylab.application.state.State)
74  	 */
75  	public void add(State state)
76  	{
77  		states.add(state);
78  	}
79  
80  	/* (non-Javadoc)
81  	 * @see com.cosylab.application.state.StateStorage#add(java.util.ArrayList)
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  		// if file exists
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 		// prepare the file object
112 		File file = new File(filePath + File.separatorChar + fileName);
113 
114 		// make path to the file if it doesn't exists jet
115 		if(!file.getParentFile().exists()) {
116 			file.getParentFile().mkdirs();
117 		}
118 
119 		// create output stream
120 		return new BufferedOutputStream(new FileOutputStream(file));
121 	}
122 
123 	/* (non-Javadoc)
124 	 * @see com.cosylab.application.state.StateStorage#load(java.io.InputStream)
125 	 */
126 	abstract public void load(String filePath, String applicationName)
127 		throws IOException;
128 
129 	/**
130 	 * DOCUMENT ME!
131 	 *
132 	 * @param filePath DOCUMENT ME!
133 	 * @param applicationName DOCUMENT ME!
134 	 *
135 	 * @throws IOException DOCUMENT ME!
136 	 */
137 	abstract public void store(String filePath, String applicationName)
138 		throws IOException;
139 
140 	/* (non-Javadoc)
141 	 * @see com.cosylab.application.state.StateStorage#getStates()
142 	 */
143 	public List getStates()
144 	{
145 		return states;
146 	}
147 	
148 	/* (non-Javadoc)
149 	 * @see com.cosylab.application.state.StateStorage#contains(com.cosylab.application.state.State)
150 	 */
151 	public boolean contains(State st) {
152 		return states.contains(st);
153 	}
154 	
155 	/* (non-Javadoc)
156 	 * @see com.cosylab.application.state.StateStorage#iterator()
157 	 */
158 	public Iterator iterator() {
159 		return states.iterator();
160 	}
161 	
162 	/* (non-Javadoc)
163 	 * @see com.cosylab.application.state.StateStorage#remove(com.cosylab.application.state.State)
164 	 */
165 	public void remove(State st) {
166 		states.remove(st);
167 	}
168 	
169 }
170 
171 /* __oOo__ */