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.portlet.dynamicdatamapping.model;
016    
017    import com.liferay.portal.kernel.util.LocaleUtil;
018    
019    import java.util.HashMap;
020    import java.util.Locale;
021    import java.util.Map;
022    import java.util.Set;
023    
024    /**
025     * @author Pablo Carvalho
026     */
027    public class LocalizedValue implements Value {
028    
029            public LocalizedValue() {
030                    this(LocaleUtil.getDefault());
031            }
032    
033            public LocalizedValue(Locale defaultLocale) {
034                    setDefaultLocale(defaultLocale);
035            }
036    
037            @Override
038            public void addString(Locale locale, String value) {
039                    _values.put(locale, value);
040            }
041    
042            @Override
043            public Set<Locale> getAvailableLocales() {
044                    return _values.keySet();
045            }
046    
047            @Override
048            public Locale getDefaultLocale() {
049                    return _defaultLocale;
050            }
051    
052            @Override
053            public String getString(Locale locale) {
054                    String value = _values.get(locale);
055    
056                    if (value == null) {
057                            value = _values.get(_defaultLocale);
058                    }
059    
060                    return value;
061            }
062    
063            @Override
064            public Map<Locale, String> getValues() {
065                    return _values;
066            }
067    
068            @Override
069            public boolean isLocalized() {
070                    return true;
071            }
072    
073            @Override
074            public void setDefaultLocale(Locale defaultLocale) {
075                    _defaultLocale = defaultLocale;
076            }
077    
078            private Locale _defaultLocale;
079            private final Map<Locale, String> _values = new HashMap<Locale, String>();
080    
081    }