View Javadoc

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