Throw exceptions if attributes are not set.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / PluginStoreConfigurationBackend.java
1 /*
2  * FreenetSone - PluginStoreConfigurationBackend.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet;
19
20 import net.pterodactylus.util.config.AttributeNotFoundException;
21 import net.pterodactylus.util.config.Configuration;
22 import net.pterodactylus.util.config.ConfigurationException;
23 import net.pterodactylus.util.config.ExtendedConfigurationBackend;
24 import freenet.pluginmanager.PluginStore;
25
26 /**
27  * Backend for a {@link Configuration} that is based on a {@link PluginStore}.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class PluginStoreConfigurationBackend implements ExtendedConfigurationBackend {
32
33         /** The backing plugin store. */
34         private final PluginStore pluginStore;
35
36         /**
37          * Creates a new configuration backend based on a plugin store.
38          *
39          * @param pluginStore
40          *            The backing plugin store
41          */
42         public PluginStoreConfigurationBackend(PluginStore pluginStore) {
43                 this.pluginStore = pluginStore;
44         }
45
46         /**
47          * {@inheritDoc}
48          */
49         @Override
50         public String getValue(String attribute) throws ConfigurationException {
51                 if (!pluginStore.strings.containsKey(attribute)) {
52                         throw new AttributeNotFoundException(attribute);
53                 }
54                 return pluginStore.strings.get(attribute);
55         }
56
57         /**
58          * {@inheritDoc}
59          */
60         @Override
61         public void putValue(String attribute, String value) throws ConfigurationException {
62                 pluginStore.strings.put(attribute, value);
63         }
64
65         /**
66          * {@inheritDoc}
67          */
68         @Override
69         public Boolean getBooleanValue(String attribute) throws ConfigurationException {
70                 if (!pluginStore.booleans.containsKey(attribute)) {
71                         throw new AttributeNotFoundException(attribute);
72                 }
73                 return pluginStore.booleans.get(attribute);
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         public void setBooleanValue(String attribute, Boolean value) throws ConfigurationException {
81                 pluginStore.booleans.put(attribute, value);
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         public Double getDoubleValue(String attribute) throws ConfigurationException {
89                 if (!pluginStore.strings.containsKey(attribute)) {
90                         throw new AttributeNotFoundException(attribute);
91                 }
92                 String stringValue = pluginStore.strings.get(attribute);
93                 if (stringValue == null) {
94                         return null;
95                 }
96                 try {
97                         return Double.valueOf(pluginStore.strings.get(attribute));
98                 } catch (NumberFormatException nfe1) {
99                         throw new ConfigurationException("Could not parse “" + stringValue + "”.", nfe1);
100                 }
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         @Override
107         public void setDoubleValue(String attribute, Double value) throws ConfigurationException {
108                 pluginStore.strings.put(attribute, String.valueOf(value));
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         @Override
115         public Integer getIntegerValue(String attribute) throws ConfigurationException {
116                 if (!pluginStore.integers.containsKey(attribute)) {
117                         throw new AttributeNotFoundException(attribute);
118                 }
119                 return pluginStore.integers.get(attribute);
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public void setIntegerValue(String attribute, Integer value) throws ConfigurationException {
127                 pluginStore.integers.put(attribute, value);
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         public Long getLongValue(String attribute) throws ConfigurationException {
135                 if (!pluginStore.longs.containsKey(attribute)) {
136                         throw new AttributeNotFoundException(attribute);
137                 }
138                 return pluginStore.longs.get(attribute);
139         }
140
141         /**
142          * {@inheritDoc}
143          */
144         @Override
145         public void setLongValue(String attribute, Long value) throws ConfigurationException {
146                 pluginStore.longs.put(attribute, value);
147         }
148
149 }