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.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     * @author Iv??n Zaera
023     */
024    public abstract class BaseSettings implements Settings {
025    
026            public BaseSettings() {
027            }
028    
029            public BaseSettings(Settings parentSettings) {
030                    this.parentSettings = parentSettings;
031            }
032    
033            @Override
034            public ModifiableSettings getModifiableSettings() {
035                    if (this instanceof ModifiableSettings) {
036                            return (ModifiableSettings)this;
037                    }
038                    else if (parentSettings == null) {
039                            return null;
040                    }
041                    else {
042                            return parentSettings.getModifiableSettings();
043                    }
044            }
045    
046            @Override
047            public Settings getParentSettings() {
048                    return parentSettings;
049            }
050    
051            @Override
052            public String getValue(String key, String defaultValue) {
053                    if (key == null) {
054                            throw new IllegalArgumentException("Key is null");
055                    }
056    
057                    String value = doGetValue(key);
058    
059                    if ((value == null) && (parentSettings != null)) {
060                            value = parentSettings.getValue(key, defaultValue);
061                    }
062    
063                    if (Validator.isNull(value)) {
064                            value = defaultValue;
065                    }
066    
067                    return value;
068            }
069    
070            @Override
071            public String[] getValues(String key, String[] defaultValue) {
072                    if (key == null) {
073                            throw new IllegalArgumentException("Key is null");
074                    }
075    
076                    String[] values = doGetValues(key);
077    
078                    if (ArrayUtil.isEmpty(values) && (parentSettings != null)) {
079                            values = parentSettings.getValues(key, defaultValue);
080                    }
081    
082                    if (ArrayUtil.isEmpty(values)) {
083                            values = defaultValue;
084                    }
085    
086                    return values;
087            }
088    
089            protected abstract String doGetValue(String key);
090    
091            protected abstract String[] doGetValues(String key);
092    
093            protected Settings parentSettings;
094    
095    }