001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.spi.preferences;
003
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.LinkedHashMap;
007import java.util.List;
008import java.util.Map;
009import java.util.SortedMap;
010
011/**
012 * Setting containing a {@link List} of {@link Map}s of {@link String} values.
013 * @since 12881 (moved from package {@code org.openstreetmap.josm.data.preferences})
014 */
015public class MapListSetting extends AbstractSetting<List<Map<String, String>>> {
016
017    /**
018     * Constructs a new {@code MapListSetting} with the given value
019     * @param value The setting value
020     */
021    public MapListSetting(List<Map<String, String>> value) {
022        super(value);
023        consistencyTest();
024    }
025
026    @Override
027    public MapListSetting copy() {
028        if (value == null)
029            return new MapListSetting(null);
030        List<Map<String, String>> copy = new ArrayList<>(value.size());
031        for (Map<String, String> map : value) {
032            Map<String, String> mapCopy = new LinkedHashMap<>(map);
033            copy.add(Collections.unmodifiableMap(mapCopy));
034        }
035        return new MapListSetting(Collections.unmodifiableList(copy));
036    }
037
038    private void consistencyTest() {
039        if (value == null)
040            return;
041        if (value.contains(null))
042            throw new IllegalArgumentException("Error: Null as list element in preference setting");
043        for (Map<String, String> map : value) {
044            if (!(map instanceof SortedMap) && map.containsKey(null))
045                throw new IllegalArgumentException("Error: Null as map key in preference setting");
046            if (map.containsValue(null))
047                throw new IllegalArgumentException("Error: Null as map value in preference setting");
048        }
049    }
050
051    @Override
052    public void visit(SettingVisitor visitor) {
053        visitor.visit(this);
054    }
055
056    @Override
057    public MapListSetting getNullInstance() {
058        return new MapListSetting(null);
059    }
060}