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 )) return Boolean.getBoolean( values.get( id ));
69  		return parent == null ? false : parent.getBoolean( id );
70  	}
71  
72  	public void setBoolean(String id, boolean value)
73  	{
74  		String oldValue = getString( id, null );
75  		values.put( id, Boolean.toString( value ));
76  		
77  		for( SettingsListener listener : listeners )
78  		{
79  			listener.settingChanged( id, oldValue, Boolean.toString( value ));
80  		}
81  	}
82  
83  	public long getLong(String id, long defaultValue)
84  	{
85  		if( values.containsKey( id ))
86  		{
87  			try
88  			{
89  				return Long.parseLong(values.get(id));
90  			}
91  			catch (NumberFormatException e)
92  			{
93  			}			
94  		}
95  		
96  		return defaultValue;
97  	}
98  	
99  	public void addSettingsListener(SettingsListener listener)
100 	{
101 		listeners.add( listener );
102 	}
103 
104 	public void removeSettingsListener(SettingsListener listener)
105 	{
106 		listeners.remove( listener );
107 	}
108 
109 	public void clearSetting(String id)
110 	{
111 		values.remove( id );
112 	}
113 
114 	public void setLong( String id, long value )
115 	{
116 		values.put( id, Long.toString( value ) );
117 	}
118 }