001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.spi.preferences;
003
004import java.util.Collections;
005import java.util.LinkedHashMap;
006import java.util.List;
007import java.util.Map;
008import java.util.SortedMap;
009
010import org.openstreetmap.josm.tools.StreamUtils;
011
012/**
013 * Setting containing a {@link List} of {@link Map}s of {@link String} values.
014 * @since 12881 (moved from package {@code org.openstreetmap.josm.data.preferences})
015 */
016public class MapListSetting extends AbstractSetting<List<Map<String, String>>> {
017
018    /**
019     * Constructs a new {@code MapListSetting} with the given value
020     * @param value The setting value
021     */
022    public MapListSetting(List<Map<String, String>> value) {
023        super(value);
024        consistencyTest();
025    }
026
027    @Override
028    public MapListSetting copy() {
029        if (value == null)
030            return new MapListSetting(null);
031        List<Map<String, String>> copy = value.stream()
032                .map(LinkedHashMap::new)
033                .map(Collections::unmodifiableMap)
034                .collect(StreamUtils.toUnmodifiableList());
035        return new MapListSetting(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}