fiore@3
|
1 /*
|
fiore@3
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
fiore@3
|
3
|
fiore@3
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
fiore@3
|
5
|
fiore@3
|
6 This program is free software: you can redistribute it and/or modify
|
fiore@3
|
7 it under the terms of the GNU General Public License as published by
|
fiore@3
|
8 the Free Software Foundation, either version 3 of the License, or
|
fiore@3
|
9 (at your option) any later version.
|
fiore@3
|
10
|
fiore@3
|
11 This program is distributed in the hope that it will be useful,
|
fiore@3
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
fiore@3
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
fiore@3
|
14 GNU General Public License for more details.
|
fiore@3
|
15
|
fiore@3
|
16 You should have received a copy of the GNU General Public License
|
fiore@3
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
fiore@3
|
18 */
|
fiore@3
|
19 package uk.ac.qmul.eecs.ccmi.checkboxtree;
|
fiore@3
|
20
|
fiore@3
|
21 import java.io.BufferedReader;
|
fiore@3
|
22 import java.io.BufferedWriter;
|
fiore@3
|
23 import java.io.File;
|
fiore@3
|
24 import java.io.FileReader;
|
fiore@3
|
25 import java.io.FileWriter;
|
fiore@3
|
26 import java.io.IOException;
|
fiore@3
|
27 import java.util.Collection;
|
fiore@3
|
28 import java.util.Collections;
|
fiore@3
|
29 import java.util.HashSet;
|
fiore@3
|
30 import java.util.Iterator;
|
fiore@3
|
31 import java.util.Set;
|
fiore@3
|
32
|
fiore@3
|
33 /**
|
fiore@3
|
34 * The {@code SetProperties} class represents a persistent set of properties.
|
fiore@3
|
35 * The {@code SetProperties} can be saved to a stream or loaded from a stream.
|
fiore@3
|
36 *
|
fiore@3
|
37 * Unlike {@code java.util.Properties}, this class is not backed by a key-value map, but rather
|
fiore@3
|
38 * by a {@code Set<String>}. Therefore it only contains values. All the methods of this class are thread-safe,
|
fiore@3
|
39 * but {@code iterator()}. In order to safely iterate on the Set, the iteration must happen in a block syncronized
|
fiore@3
|
40 * on the Object returned by {@link #getMonitor()}.
|
fiore@3
|
41 *
|
fiore@3
|
42 * For a description of the methods of the {@code Set} interface, see {@code java.util.Set}
|
fiore@3
|
43 *
|
fiore@3
|
44 * @see java.util.Properties
|
fiore@3
|
45 * @see java.util.Collections#synchronizedSet(Set)
|
fiore@3
|
46 */
|
fiore@3
|
47 public class SetProperties implements Set<String> {
|
fiore@3
|
48 public SetProperties() {
|
fiore@3
|
49 delegate = Collections.synchronizedSet(new HashSet<String>());
|
fiore@3
|
50 }
|
fiore@3
|
51
|
fiore@3
|
52 public SetProperties(Collection<? extends String> collection) {
|
fiore@3
|
53 delegate = Collections.synchronizedSet(new HashSet<String>(collection));
|
fiore@3
|
54 }
|
fiore@3
|
55
|
fiore@3
|
56 public SetProperties(int initialCapacity) {
|
fiore@3
|
57 delegate = Collections.synchronizedSet(new HashSet<String>(initialCapacity));
|
fiore@3
|
58 }
|
fiore@3
|
59
|
fiore@3
|
60 public SetProperties(int initialCapacity, float loadFactor){
|
fiore@3
|
61 delegate = Collections.synchronizedSet(new HashSet<String>(initialCapacity, loadFactor));
|
fiore@3
|
62 }
|
fiore@3
|
63
|
fiore@3
|
64 /* DELEGATE METHODS */
|
fiore@3
|
65 @Override
|
fiore@3
|
66 public boolean add(String arg0) {
|
fiore@3
|
67 return delegate.add(arg0);
|
fiore@3
|
68 }
|
fiore@3
|
69
|
fiore@3
|
70 @Override
|
fiore@3
|
71 public boolean addAll(Collection<? extends String> arg0) {
|
fiore@3
|
72 return delegate.addAll(arg0);
|
fiore@3
|
73 }
|
fiore@3
|
74
|
fiore@3
|
75 @Override
|
fiore@3
|
76 public void clear() {
|
fiore@3
|
77 delegate.clear();
|
fiore@3
|
78 }
|
fiore@3
|
79
|
fiore@3
|
80 @Override
|
fiore@3
|
81 public boolean contains(Object arg0) {
|
fiore@3
|
82 return delegate.contains(arg0);
|
fiore@3
|
83 }
|
fiore@3
|
84
|
fiore@3
|
85 @Override
|
fiore@3
|
86 public boolean containsAll(Collection<?> arg0) {
|
fiore@3
|
87 return delegate.containsAll(arg0);
|
fiore@3
|
88 }
|
fiore@3
|
89
|
fiore@3
|
90 @Override
|
fiore@3
|
91 public boolean equals(Object arg0) {
|
fiore@3
|
92 return delegate.equals(arg0);
|
fiore@3
|
93 }
|
fiore@3
|
94
|
fiore@3
|
95 @Override
|
fiore@3
|
96 public int hashCode() {
|
fiore@3
|
97 return delegate.hashCode();
|
fiore@3
|
98 }
|
fiore@3
|
99
|
fiore@3
|
100 @Override
|
fiore@3
|
101 public boolean isEmpty() {
|
fiore@3
|
102 return delegate.isEmpty();
|
fiore@3
|
103 }
|
fiore@3
|
104
|
fiore@3
|
105 @Override
|
fiore@3
|
106 public Iterator<String> iterator() {
|
fiore@3
|
107 return delegate.iterator();
|
fiore@3
|
108 }
|
fiore@3
|
109
|
fiore@3
|
110 @Override
|
fiore@3
|
111 public boolean remove(Object arg0) {
|
fiore@3
|
112 return delegate.remove(arg0);
|
fiore@3
|
113 }
|
fiore@3
|
114
|
fiore@3
|
115 @Override
|
fiore@3
|
116 public boolean removeAll(Collection<?> arg0) {
|
fiore@3
|
117 return delegate.removeAll(arg0);
|
fiore@3
|
118 }
|
fiore@3
|
119
|
fiore@3
|
120 @Override
|
fiore@3
|
121 public boolean retainAll(Collection<?> arg0) {
|
fiore@3
|
122 return delegate.retainAll(arg0);
|
fiore@3
|
123 }
|
fiore@3
|
124
|
fiore@3
|
125 @Override
|
fiore@3
|
126 public int size() {
|
fiore@3
|
127 return delegate.size();
|
fiore@3
|
128 }
|
fiore@3
|
129
|
fiore@3
|
130 @Override
|
fiore@3
|
131 public Object[] toArray() {
|
fiore@3
|
132 return delegate.toArray();
|
fiore@3
|
133 }
|
fiore@3
|
134
|
fiore@3
|
135 @Override
|
fiore@3
|
136 public <T> T[] toArray(T[] arg0) {
|
fiore@3
|
137 return delegate.toArray(arg0);
|
fiore@3
|
138 }
|
fiore@3
|
139
|
fiore@3
|
140 /**
|
fiore@3
|
141 * Stores the content of this set (strings) in a text file. The content can then be retrieved
|
fiore@3
|
142 * by calling {@code load()} passing as argument the same file as this method. The strings
|
fiore@3
|
143 * will be written on a row each.
|
fiore@3
|
144 *
|
fiore@3
|
145 * @param file A valid File where the content of this object will be stored
|
fiore@3
|
146 * @param comments A comment string that will be added at the beginning of the file.
|
fiore@3
|
147 *
|
fiore@3
|
148 * @throws IOException if an exception occurs while writing the file
|
fiore@3
|
149 */
|
fiore@3
|
150 public void store(File file, String comments) throws IOException{
|
fiore@3
|
151 synchronized(delegate){
|
fiore@3
|
152 if(file == null)
|
fiore@3
|
153 throw new IllegalArgumentException("File cannot be null");
|
fiore@3
|
154 FileWriter fWriter = new FileWriter(file);
|
fiore@3
|
155 BufferedWriter writer = new BufferedWriter(fWriter);
|
fiore@3
|
156 if(comments != null){
|
fiore@3
|
157 writer.write(COMMENTS_ESCAPE+" "+comments);
|
fiore@3
|
158 writer.newLine();
|
fiore@3
|
159 writer.newLine();
|
fiore@3
|
160 }
|
fiore@3
|
161
|
fiore@3
|
162 for(String property : this){
|
fiore@3
|
163 writer.write(property);
|
fiore@3
|
164 writer.newLine();
|
fiore@3
|
165 }
|
fiore@3
|
166 writer.close();
|
fiore@3
|
167 }
|
fiore@3
|
168 }
|
fiore@3
|
169
|
fiore@3
|
170 /**
|
fiore@3
|
171 * Loads the content of a file into this set. Whaen the file is read, each row is taken as an
|
fiore@3
|
172 * entry of the set.
|
fiore@3
|
173 *
|
fiore@3
|
174 * @param file the file where to read the entries from
|
fiore@3
|
175 * @throws IOException if an exception occurs while reading the file
|
fiore@3
|
176 */
|
fiore@3
|
177 public void load(File file) throws IOException{
|
fiore@3
|
178 synchronized(delegate){
|
fiore@3
|
179 if(file == null)
|
fiore@3
|
180 throw new IllegalArgumentException("File cannot be null");
|
fiore@3
|
181 FileReader fReader = new FileReader(file);
|
fiore@3
|
182 BufferedReader reader = new BufferedReader(fReader);
|
fiore@3
|
183 String line;
|
fiore@3
|
184 while((line = reader.readLine()) != null){
|
fiore@3
|
185 if(!line.isEmpty() && !line.trim().startsWith(COMMENTS_ESCAPE))
|
fiore@3
|
186 add(line);
|
fiore@3
|
187 }
|
fiore@3
|
188 reader.close();
|
fiore@3
|
189 }
|
fiore@3
|
190 }
|
fiore@3
|
191
|
fiore@3
|
192 /**
|
fiore@3
|
193 * Returns the Object all the methods (but {@code iterator()} of this {@code Set} are synchronized on. It can be used
|
fiore@3
|
194 * to safely iterate on this object without incurring in a race condition
|
fiore@3
|
195 *
|
fiore@3
|
196 * @return an {@code Object} to be used as synchronization monitor
|
fiore@3
|
197 *
|
fiore@3
|
198 * @see java.util.Collections#synchronizedSet(Set)
|
fiore@3
|
199 */
|
fiore@3
|
200 public Object getMonitor(){
|
fiore@3
|
201 return delegate;
|
fiore@3
|
202 }
|
fiore@3
|
203
|
fiore@3
|
204 private Set<String> delegate;
|
fiore@3
|
205 private static String COMMENTS_ESCAPE = "#";
|
fiore@3
|
206 }
|