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;
016    
017    import com.liferay.portal.kernel.util.HashUtil;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.util.HashMap;
022    import java.util.Locale;
023    import java.util.Map;
024    import java.util.Set;
025    
026    /**
027     * @author Pablo Carvalho
028     */
029    public class LocalizedValue implements Value {
030    
031            public LocalizedValue() {
032                    this(LocaleUtil.getDefault());
033            }
034    
035            public LocalizedValue(Locale defaultLocale) {
036                    setDefaultLocale(defaultLocale);
037            }
038    
039            public LocalizedValue(LocalizedValue localizedValue) {
040                    _defaultLocale = localizedValue._defaultLocale;
041    
042                    Map<Locale, String> values = localizedValue._values;
043    
044                    for (Map.Entry<Locale, String> entry : values.entrySet()) {
045                            addString(entry.getKey(), entry.getValue());
046                    }
047            }
048    
049            @Override
050            public void addString(Locale locale, String value) {
051                    _values.put(locale, value);
052            }
053    
054            @Override
055            public boolean equals(Object obj) {
056                    if (this == obj) {
057                            return true;
058                    }
059    
060                    if (!(obj instanceof LocalizedValue)) {
061                            return false;
062                    }
063    
064                    LocalizedValue localizedValue = (LocalizedValue)obj;
065    
066                    if (Validator.equals(_defaultLocale, localizedValue._defaultLocale) &&
067                            Validator.equals(_values, localizedValue._values)) {
068    
069                            return true;
070                    }
071    
072                    return false;
073            }
074    
075            @Override
076            public Set<Locale> getAvailableLocales() {
077                    return _values.keySet();
078            }
079    
080            @Override
081            public Locale getDefaultLocale() {
082                    return _defaultLocale;
083            }
084    
085            @Override
086            public String getString(Locale locale) {
087                    String value = _values.get(locale);
088    
089                    if (value == null) {
090                            value = _values.get(_defaultLocale);
091                    }
092    
093                    return value;
094            }
095    
096            @Override
097            public Map<Locale, String> getValues() {
098                    return _values;
099            }
100    
101            @Override
102            public int hashCode() {
103                    int hash = HashUtil.hash(0, _defaultLocale);
104    
105                    return HashUtil.hash(hash, _values);
106            }
107    
108            @Override
109            public boolean isLocalized() {
110                    return true;
111            }
112    
113            @Override
114            public void setDefaultLocale(Locale defaultLocale) {
115                    _defaultLocale = defaultLocale;
116            }
117    
118            private Locale _defaultLocale;
119            private final Map<Locale, String> _values = new HashMap<>();
120    
121    }