source: josm/trunk/src/org/openstreetmap/josm/data/preferences/MapListSetting.java@ 11266

Last change on this file since 11266 was 10235, checked in by Don-vip, 8 years ago

sonar - squid:S00112 - Generic exceptions should never be thrown

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences;
3
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Iterator;
7import java.util.LinkedHashMap;
8import java.util.List;
9import java.util.Map;
10import java.util.Map.Entry;
11import java.util.Objects;
12
13/**
14 * Setting containing a {@link List} of {@link Map}s of {@link String} values.
15 * @since 9759
16 */
17public class MapListSetting extends AbstractSetting<List<Map<String, String>>> {
18
19 /**
20 * Constructs a new {@code MapListSetting} with the given value
21 * @param value The setting value
22 */
23 public MapListSetting(List<Map<String, String>> value) {
24 super(value);
25 consistencyTest();
26 }
27
28 @Override
29 public boolean equalVal(List<Map<String, String>> otherVal) {
30 if (value == null)
31 return otherVal == null;
32 if (otherVal == null)
33 return false;
34 if (value.size() != otherVal.size())
35 return false;
36 Iterator<Map<String, String>> itA = value.iterator();
37 Iterator<Map<String, String>> itB = otherVal.iterator();
38 while (itA.hasNext()) {
39 if (!equalMap(itA.next(), itB.next()))
40 return false;
41 }
42 return true;
43 }
44
45 private static boolean equalMap(Map<String, String> a, Map<String, String> b) {
46 if (a == null)
47 return b == null;
48 if (b == null)
49 return false;
50 if (a.size() != b.size())
51 return false;
52 for (Entry<String, String> e : a.entrySet()) {
53 if (!Objects.equals(e.getValue(), b.get(e.getKey())))
54 return false;
55 }
56 return true;
57 }
58
59 @Override
60 public MapListSetting copy() {
61 if (value == null)
62 return new MapListSetting(null);
63 List<Map<String, String>> copy = new ArrayList<>(value.size());
64 for (Map<String, String> map : value) {
65 Map<String, String> mapCopy = new LinkedHashMap<>(map);
66 copy.add(Collections.unmodifiableMap(mapCopy));
67 }
68 return new MapListSetting(Collections.unmodifiableList(copy));
69 }
70
71 private void consistencyTest() {
72 if (value == null)
73 return;
74 if (value.contains(null))
75 throw new IllegalArgumentException("Error: Null as list element in preference setting");
76 for (Map<String, String> map : value) {
77 if (map.keySet().contains(null))
78 throw new IllegalArgumentException("Error: Null as map key in preference setting");
79 if (map.values().contains(null))
80 throw new IllegalArgumentException("Error: Null as map value in preference setting");
81 }
82 }
83
84 @Override
85 public void visit(SettingVisitor visitor) {
86 visitor.visit(this);
87 }
88
89 @Override
90 public MapListSetting getNullInstance() {
91 return new MapListSetting(null);
92 }
93
94 @Override
95 public boolean equals(Object other) {
96 if (!(other instanceof MapListSetting))
97 return false;
98 return equalVal(((MapListSetting) other).getValue());
99 }
100}
Note: See TracBrowser for help on using the repository browser.