001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.settings;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.LocalizationUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.util.Collection;
024    import java.util.Locale;
025    
026    /**
027     * @author Iv??n Zaera
028     * @author Jorge Ferrer
029     */
030    public class TypedSettings {
031    
032            public TypedSettings(Settings settings) {
033                    this(settings, LanguageUtil.getAvailableLocales());
034            }
035    
036            public TypedSettings(
037                    Settings settings, Collection<Locale> availableLocales) {
038    
039                    _settings = settings;
040                    _availableLocales = availableLocales;
041            }
042    
043            public boolean getBooleanValue(String key) {
044                    return getBooleanValue(key, false);
045            }
046    
047            public boolean getBooleanValue(String key, boolean defaultValue) {
048                    String value = getValue(key, null);
049    
050                    return GetterUtil.getBoolean(value, defaultValue);
051            }
052    
053            public double getDoubleValue(String key) {
054                    return getDoubleValue(key, 0);
055            }
056    
057            public double getDoubleValue(String key, double defaultValue) {
058                    String value = getValue(key, null);
059    
060                    return GetterUtil.getDouble(value, defaultValue);
061            }
062    
063            public float getFloatValue(String key) {
064                    return getFloatValue(key, 0);
065            }
066    
067            public float getFloatValue(String key, float defaultValue) {
068                    String value = getValue(key, null);
069    
070                    return GetterUtil.getFloat(value, defaultValue);
071            }
072    
073            public int getIntegerValue(String key) {
074                    return getIntegerValue(key, 0);
075            }
076    
077            public int getIntegerValue(String key, int defaultValue) {
078                    String value = getValue(key, null);
079    
080                    return GetterUtil.getInteger(value, defaultValue);
081            }
082    
083            public LocalizedValuesMap getLocalizedValuesMap(String key) {
084                    LocalizedValuesMap localizedValuesMap = new LocalizedValuesMap(
085                            getValue(key, null));
086    
087                    for (Locale locale : _availableLocales) {
088                            String localizedPreference = LocalizationUtil.getLocalizedName(
089                                    key, LocaleUtil.toLanguageId(locale));
090    
091                            localizedValuesMap.put(locale, getValue(localizedPreference, null));
092                    }
093    
094                    return localizedValuesMap;
095            }
096    
097            public long getLongValue(String key) {
098                    return getLongValue(key, 0);
099            }
100    
101            public long getLongValue(String key, long defaultValue) {
102                    String value = getValue(key, null);
103    
104                    return GetterUtil.getLong(value, defaultValue);
105            }
106    
107            public String getValue(String key) {
108                    return getValue(key, StringPool.BLANK);
109            }
110    
111            public String getValue(String key, String defaultValue) {
112                    return _settings.getValue(key, defaultValue);
113            }
114    
115            public String[] getValues(String key) {
116                    return getValues(key, StringPool.EMPTY_ARRAY);
117            }
118    
119            public String[] getValues(String key, String[] defaultValue) {
120                    return _settings.getValues(key, defaultValue);
121            }
122    
123            public Settings getWrappedSettings() {
124                    return _settings;
125            }
126    
127            public void reset(String key) {
128                    ModifiableSettings modifiableSettings =
129                            _settings.getModifiableSettings();
130    
131                    modifiableSettings.reset(key);
132            }
133    
134            public void setBooleanValue(String key, boolean value) {
135                    setValue(key, String.valueOf(value));
136            }
137    
138            public void setIntegerValue(String key, int value) {
139                    setValue(key, String.valueOf(value));
140            }
141    
142            public void setLongValue(String key, long value) {
143                    setValue(key, String.valueOf(value));
144            }
145    
146            public void setValue(String key, String value) {
147                    ModifiableSettings modifiableSettings =
148                            _settings.getModifiableSettings();
149    
150                    modifiableSettings.setValue(key, value);
151            }
152    
153            public void setValues(String key, String[] values) {
154                    ModifiableSettings modifiableSettings =
155                            _settings.getModifiableSettings();
156    
157                    modifiableSettings.setValues(key, values);
158            }
159    
160            private final Collection<Locale> _availableLocales;
161            private final Settings _settings;
162    
163    }