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()
35  	{
36  		this( null );
37  	}
38  	
39  	public SettingsImpl( Settings parent )
40  	{
41  		this.parent = parent;
42  	}
43  	
44  	public boolean isSet( String id )
45  	{
46  		return values.containsKey( id );
47  	}
48  
49  	public String getString(String id, String defaultValue)
50  	{
51  		if( values.containsKey( id )) return values.get( id ) ;
52  		return parent == null ? defaultValue : parent.getString( id, defaultValue );
53  	}
54  
55  	public void setString(String id, String value)
56  	{
57  		String oldValue = getString( id, null );
58  		values.put( id, value );
59  		
60  		for( SettingsListener listener : listeners )
61  		{
62  			listener.settingChanged( id, oldValue, value );
63  		}
64  	}
65  
66  	public boolean getBoolean(String id )
67  	{
68  		if( values.containsKey( id ))
69  			return Boolean.parseBoolean( values.get( id ) );
70  		return parent == null ? false : parent.getBoolean( id );
71  	}
72  
73  	public void setBoolean(String id, boolean value)
74  	{
75  		String oldValue = getString( id, null );
76  		values.put( id, Boolean.toString( value ));
77  		
78  		for( SettingsListener listener : listeners )
79  		{
80  			listener.settingChanged( id, oldValue, Boolean.toString( value ));
81  		}
82  	}
83  
84  	public long getLong(String id, long defaultValue)
85  	{
86  		if( values.containsKey( id ))
87  		{
88  			try
89  			{
90  				return Long.parseLong(values.get(id));
91  			}
92  			catch (NumberFormatException e)
93  			{
94  			}			
95  		}
96  		
97  		return defaultValue;
98  	}
99  	
100 	public void addSettingsListener(SettingsListener listener)
101 	{
102 		listeners.add( listener );
103 	}
104 
105 	public void removeSettingsListener(SettingsListener listener)
106 	{
107 		listeners.remove( listener );
108 	}
109 
110 	public void clearSetting(String id)
111 	{
112 		values.remove( id );
113 	}
114 
115 	public void setLong( String id, long value )
116 	{
117 		values.put( id, Long.toString( value ) );
118 	}
119 }