annotate java/src/uk/ac/qmul/eecs/ccmi/checkboxtree/SetProperties.java @ 1:e3935c01cde2 tip

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