View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.settings;
14  
15  import java.util.HashSet;
16  import java.util.Set;
17  
18  import com.eviware.soapui.model.settings.Settings;
19  import com.eviware.soapui.model.settings.SettingsListener;
20  import com.eviware.soapui.support.types.StringToStringMap;
21  
22  /***
23   * Default Settings implementation
24   * 
25   * @author Ole.Matzura
26   */
27  
28  public class SettingsImpl implements Settings
29  {
30  	private final Settings parent;
31  	private final StringToStringMap values = new StringToStringMap();
32  	private final Set<SettingsListener> listeners = new HashSet<SettingsListener>();
33  
34  	public SettingsImpl( Settings parent )
35  	{
36  		this.parent = parent;
37  	}
38  	
39  	public boolean isSet( String id )
40  	{
41  		return values.containsKey( id );
42  	}
43  
44  	public String getString(String id, String defaultValue)
45  	{
46  		if( values.containsKey( id )) return values.get( id ) ;
47  		return parent == null ? defaultValue : parent.getString( id, defaultValue );
48  	}
49  
50  	public void setString(String id, String value)
51  	{
52  		String oldValue = getString( id, null );
53  		values.put( id, value );
54  		
55  		for( SettingsListener listener : listeners )
56  		{
57  			listener.settingChanged( id, oldValue, value );
58  		}
59  	}
60  
61  	public boolean getBoolean(String id )
62  	{
63  		if( values.containsKey( id )) return Boolean.getBoolean( values.get( id ));
64  		return parent == null ? false : parent.getBoolean( id );
65  	}
66  
67  	public void setBoolean(String id, boolean value)
68  	{
69  		String oldValue = getString( id, null );
70  		values.put( id, Boolean.toString( value ));
71  		
72  		for( SettingsListener listener : listeners )
73  		{
74  			listener.settingChanged( id, oldValue, Boolean.toString( value ));
75  		}
76  	}
77  
78  	public long getLong(String id, long defaultValue)
79  	{
80  		if( values.containsKey( id ))
81  		{
82  			try
83  			{
84  				return Long.parseLong(values.get(id));
85  			}
86  			catch (NumberFormatException e)
87  			{
88  			}			
89  		}
90  		
91  		return defaultValue;
92  	}
93  	
94  	public void addSettingsListener(SettingsListener listener)
95  	{
96  		listeners.add( listener );
97  	}
98  
99  	public void removeSettingsListener(SettingsListener listener)
100 	{
101 		listeners.remove( listener );
102 	}
103 
104 	public void clearSetting(String id)
105 	{
106 		values.remove( id );
107 	}
108 
109 	public void setLong( String id, long value )
110 	{
111 		values.put( id, Long.toString( value ) );
112 	}
113 }