source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/TagModel.java@ 10755

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

add more unit tests, javadoc, fix code style issues

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging;
3
4import java.util.ArrayList;
5import java.util.List;
6
7/**
8 * Tag model.
9 * @since 1762
10 */
11public class TagModel {
12
13 /** the name of the tag */
14 private String name;
15
16 /** the list of values */
17 private List<String> values;
18
19 /**
20 * constructor
21 */
22 public TagModel() {
23 values = new ArrayList<>();
24 setName("");
25 setValue("");
26 }
27
28 /**
29 * constructor
30 * @param name the tag name
31 */
32 public TagModel(String name) {
33 this();
34 setName(name);
35 }
36
37 /**
38 * constructor
39 *
40 * @param name the tag name
41 * @param value the tag value
42 */
43 public TagModel(String name, String value) {
44 this();
45 setName(name);
46 setValue(value);
47 }
48
49 /**
50 * sets the name. Converts name to "" if null.
51 * @param name the tag name
52 */
53 public final void setName(String name) {
54 this.name = (name == null) ? "" : name;
55 }
56
57 /**
58 * returns the tag name (key).
59 * @return the tag name
60 */
61 public String getName() {
62 return name;
63 }
64
65 /**
66 * removes all values from the list of values
67 */
68 public void clearValues() {
69 this.values.clear();
70 }
71
72 /**
73 * sets a unique value for this tag. Converts value to "", if null.
74 * @param value the value.
75 */
76 public final void setValue(String value) {
77 clearValues();
78 this.values.add((value == null) ? "" : value);
79 }
80
81 /**
82 * determines if this tag model has a specific value
83 * @param value the value to be checked; converted to "" if null
84 * @return true, if the values of this tag include <code>value</code>; false otherwise
85 */
86 public boolean hasValue(String value) {
87 return values.contains((value == null) ? "" : value);
88 }
89
90 /**
91 * adds a tag value
92 * @param value the value to add; converted to "" if null
93 */
94 public void addValue(String value) {
95 String val = (value == null) ? "" : value;
96 if (hasValue(val)) {
97 return;
98 }
99 values.add(val);
100 }
101
102 /**
103 * removes a value from the list of values. Converts value to "" if null
104 * @param value the value
105 */
106 public void removeValue(String value) {
107 values.remove((value == null) ? "" : value);
108 }
109
110 /**
111 * returns the list of values
112 * @return the list of values
113 */
114 public List<String> getValues() {
115 return values;
116 }
117
118 /**
119 * returns the value(s) as string
120 * @return the value(s) as string, joined with a semicolon (;) if multiple values
121 */
122 public String getValue() {
123 if (getValueCount() == 0) {
124 return "";
125 } else if (getValueCount() == 1) {
126 return values.get(0);
127 } else {
128 StringBuilder sb = new StringBuilder();
129 for (int i = 0; i < values.size(); i++) {
130 sb.append(values.get(i));
131 if (i + 1 < values.size()) {
132 sb.append(';');
133 }
134 }
135 return sb.toString();
136 }
137 }
138
139 /**
140 * returns the number of values
141 * @return the number of values
142 */
143 public int getValueCount() {
144 return values.size();
145 }
146}
Note: See TracBrowser for help on using the repository browser.