1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.configuration.internal

File MemoryConfigurationSource.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
22
8
1
129
76
16
0.73
2.75
8
2

Classes

Class Line # Actions
MemoryConfigurationSource 44 22 0% 16 4
0.894736889.5%
 

Contributing tests

This file is covered by 483 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.configuration.internal;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24    import java.util.Map;
25    import java.util.concurrent.ConcurrentHashMap;
26   
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.beanutils.ConvertUtils;
31    import org.xwiki.component.annotation.Component;
32   
33    /**
34    * Runtime configuration source stored in memory.
35    * <p>
36    * Can be modified by lookuping the component and using {@link MemoryConfigurationSource#setProperty(String, Object)}.
37    *
38    * @version $Id: 22e5ceaec93cf21a5f9778b3a8190dace370e4ad $
39    * @since 3.5M1
40    */
41    @Component
42    @Singleton
43    @Named("memory")
 
44    public class MemoryConfigurationSource extends AbstractConfigurationSource
45    {
46    /**
47    * The properties.
48    */
49    private Map<String, Object> properties = new ConcurrentHashMap<String, Object>();
50   
51    /**
52    * @param key the key for the value to add to the configuration
53    * @param value the value to add in the configuration
54    */
 
55  264 toggle public void setProperty(String key, Object value)
56    {
57  264 this.properties.put(key, value);
58    }
59   
60    /**
61    * @param key the key associated to the property to remove
62    */
 
63  2 toggle public void removeProperty(String key)
64    {
65  2 this.properties.remove(key);
66    }
67   
 
68  29259 toggle @SuppressWarnings("unchecked")
69    @Override
70    public <T> T getProperty(String key, T defaultValue)
71    {
72  29259 T result;
73   
74  29259 if (this.properties.containsKey(key)) {
75  260 Object value = this.properties.get(key);
76  260 if (value != null && defaultValue != null && !defaultValue.getClass().isInstance(value)) {
77  180 value = ConvertUtils.convert(value, defaultValue.getClass());
78    }
79  260 result = (T) value;
80    } else {
81  28999 result = defaultValue;
82    }
83   
84  29259 return result;
85    }
86   
 
87  1729 toggle @Override
88    public <T> T getProperty(String key, Class<T> valueClass)
89    {
90  1729 T result;
91   
92  1729 if (this.properties.containsKey(key)) {
93  19 Object value = this.properties.get(key);
94  19 if (value != null && valueClass != null && !valueClass.isInstance(value)) {
95  0 value = ConvertUtils.convert(value, valueClass);
96    }
97  19 result = (T) value;
98    } else {
99  1710 result = getDefault(valueClass);
100    }
101   
102  1729 return result;
103    }
104   
 
105  604 toggle @SuppressWarnings("unchecked")
106    @Override
107    public <T> T getProperty(String key)
108    {
109  604 return (T) this.properties.get(key);
110    }
111   
 
112  2 toggle @Override
113    public List<String> getKeys()
114    {
115  2 return new ArrayList<String>(this.properties.keySet());
116    }
117   
 
118  25 toggle @Override
119    public boolean containsKey(String key)
120    {
121  25 return this.properties.containsKey(key);
122    }
123   
 
124  0 toggle @Override
125    public boolean isEmpty()
126    {
127  0 return this.properties.isEmpty();
128    }
129    }