view java/src/uk/ac/qmul/eecs/ccmi/checkboxtree/SetProperties.java @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents 9e67171477bc
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.ccmi.checkboxtree;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * The {@code SetProperties} class represents a persistent set of properties. 
 * The {@code SetProperties} can be saved to a stream or loaded from a stream. 
 * 
 * Unlike {@code java.util.Properties}, this class is not backed by a key-value map, but rather
 * by a {@code Set<String>}. Therefore it only contains values. All the methods of this class are thread-safe,
 * but {@code iterator()}. In order to safely iterate on the Set, the iteration must happen in a block syncronized
 * on the Object returned by {@link #getMonitor()}. 
 * 
 * For a description of the methods of the {@code Set} interface, see {@code java.util.Set}
 * 
 * @see java.util.Properties
 * @see java.util.Collections#synchronizedSet(Set)
 */
public class SetProperties implements Set<String> {
	public SetProperties() { 
		delegate = Collections.synchronizedSet(new HashSet<String>());
	}

	public SetProperties(Collection<? extends String> collection) {
		delegate = Collections.synchronizedSet(new HashSet<String>(collection));
	}

	public SetProperties(int initialCapacity) {
		delegate = Collections.synchronizedSet(new HashSet<String>(initialCapacity));
	}
	
	public SetProperties(int initialCapacity, float loadFactor){
		delegate = Collections.synchronizedSet(new HashSet<String>(initialCapacity, loadFactor));
	}

	/* DELEGATE METHODS */
	@Override
	public boolean add(String arg0) {
		return delegate.add(arg0);
	}

	@Override
	public boolean addAll(Collection<? extends String> arg0) {
		return delegate.addAll(arg0);
	}

	@Override
	public void clear() {
		delegate.clear();
	}

	@Override
	public boolean contains(Object arg0) {
		return delegate.contains(arg0);
	}

	@Override
	public boolean containsAll(Collection<?> arg0) {
		return delegate.containsAll(arg0);
	}

	@Override
	public boolean equals(Object arg0) {
		return delegate.equals(arg0);
	}

	@Override
	public int hashCode() {
		return delegate.hashCode();
	}

	@Override
	public boolean isEmpty() {
		return delegate.isEmpty();
	}

	@Override	
	public Iterator<String> iterator() {
		return delegate.iterator();
	}

	@Override
	public boolean remove(Object arg0) {
		return delegate.remove(arg0);
	}

	@Override
	public boolean removeAll(Collection<?> arg0) {
		return delegate.removeAll(arg0);
	}

	@Override
	public boolean retainAll(Collection<?> arg0) {
		return delegate.retainAll(arg0);
	}

	@Override
	public int size() {
		return delegate.size();
	}

	@Override
	public Object[] toArray() {
		return delegate.toArray();
	}

	@Override
	public <T> T[] toArray(T[] arg0) {
		return delegate.toArray(arg0);
	}

	/**
	 * Stores the content of this set (strings) in a text file. The content can then be retrieved 
	 * by calling {@code load()} passing as argument the same file as this method. The strings 
	 * will be written on a row each. 
	 * 
	 * @param file A valid File where the content of this object will be stored  
	 * @param comments A comment string that will be added at the beginning of the file. 
	 *         
	 * @throws IOException if an exception occurs while writing the file
	 */
	public void store(File file, String comments) throws IOException{
		synchronized(delegate){
			if(file == null)
				throw new IllegalArgumentException("File cannot be null");
			FileWriter fWriter = new FileWriter(file);
			BufferedWriter writer = new BufferedWriter(fWriter);
			if(comments != null){
				writer.write(COMMENTS_ESCAPE+" "+comments);
				writer.newLine();
				writer.newLine();
			}
				
			for(String property : this){
				writer.write(property);
				writer.newLine();
			}
			writer.close();
		}
	}
	
	/**
	 * Loads the content of a file into this set. Whaen the file is read, each row is taken as an
	 * entry of the set.   
	 *  
	 * @param file the file where to read the entries from
	 * @throws IOException if an exception occurs while reading the file 
	 */
	public void load(File file) throws IOException{
		synchronized(delegate){
			if(file == null)
				throw new IllegalArgumentException("File cannot be null");
			FileReader fReader = new FileReader(file);
			BufferedReader reader = new BufferedReader(fReader);
			String line;
			while((line = reader.readLine()) != null){
				if(!line.isEmpty() && !line.trim().startsWith(COMMENTS_ESCAPE))
					add(line);
			}
			reader.close();
		}
	}
	
	/**
	 * Returns the Object all the methods (but {@code iterator()} of this {@code Set} are synchronized on. It can be used 
	 * to safely iterate on this object without incurring in a race condition   
	 *  
	 * @return an {@code Object} to be used as synchronization monitor  
	 * 
	 * @see java.util.Collections#synchronizedSet(Set) 
	 */
	public Object getMonitor(){
		return delegate;
	}
	
	private Set<String> delegate;
	private static String COMMENTS_ESCAPE = "#";
}