001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.test;
018
019import java.util.ArrayList;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Properties;
023import java.util.Set;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027
028/**
029 * Properties implementation detecting duplicate keys, that does *not* handle properly removal of items.
030 *
031 * @since 7.3
032 */
033public class TranslationProperties extends Properties {
034
035    private static final long serialVersionUID = 1L;
036
037    private static final Log log = LogFactory.getLog(TranslationProperties.class);
038
039    protected Properties duplicates;
040
041    protected Set<String> singleLabels;
042
043    public TranslationProperties() {
044        super();
045        duplicates = new Properties();
046        singleLabels = new HashSet<String>();
047    }
048
049    /**
050     * Override to detect duplicate keys
051     */
052    @SuppressWarnings("unchecked")
053    @Override
054    public synchronized Object put(Object key, Object value) {
055        if (key instanceof String && !((String) key).contains(".")) {
056            singleLabels.add((String) key);
057        }
058        if (containsKey(key)) {
059            List<Object> values;
060            if (duplicates.containsKey(key)) {
061                values = (List<Object>) duplicates.get(key);
062            } else {
063                values = new ArrayList<>();
064                duplicates.put(key, values);
065                values.add(get(key));
066            }
067            values.add(value);
068            if (log.isDebugEnabled()) {
069                log.debug(String.format("Detected duplicate key '%s', values=%s", key, values));
070            }
071        }
072        return super.put(key, value);
073    }
074
075    public Set<String> getDuplicatePropertyNames() {
076        return duplicates.stringPropertyNames();
077    }
078
079    public Properties getDuplicates() {
080        return duplicates;
081    }
082
083    public Set<String> getSingleLabels() {
084        return singleLabels;
085    }
086
087    @Override
088    public synchronized void clear() {
089        super.clear();
090        duplicates.clear();
091    }
092
093    // other removal methods not handled
094
095}