source: josm/trunk/src/org/openstreetmap/josm/data/preferences/ListListSetting.java@ 9798

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

refactor preferences settings classes

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