001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.tagging;
003
004 import java.util.ArrayList;
005 import java.util.List;
006
007 public class TagModel {
008
009 /** the name of the tag */
010 private String name = null;
011
012 /** the list of values */
013 private ArrayList<String> values = null;
014
015 /**
016 * constructor
017 */
018 public TagModel() {
019 values = new ArrayList<String>();
020 setName("");
021 setValue("");
022 }
023
024 /**
025 * constructor
026 * @param name the tag name
027 */
028 public TagModel(String name) {
029 this();
030 setName(name);
031 }
032
033 /**
034 * constructor
035 *
036 * @param name the tag name
037 * @param value the tag value
038 */
039 public TagModel(String name, String value) {
040 this();
041 setName(name);
042 setValue(value);
043 }
044
045 /**
046 * sets the name. Converts name to "" if null.
047 * @param name the tag name
048 */
049 public void setName(String name) {
050 name = (name == null) ? "" : name;
051 this.name = name;
052 }
053
054 /**
055 * @return the tag name
056 */
057 public String getName(){
058 return name;
059 }
060
061 /**
062 * removes all values from the list of values
063 */
064 public void clearValues() {
065 this.values.clear();
066 }
067
068 /**
069 * sets a unique value for this tag. Converts value to "", if null.
070 * @param value the value.
071 */
072 public void setValue(String value) {
073 value = (value == null) ? "" : value;
074 clearValues();
075 this.values.add(value);
076 }
077
078 /**
079 *
080 * @param value the value to be checked; converted to "" if null
081 * @return true, if the values of this tag include <code>value</code>; false otherwise
082 */
083 public boolean hasValue(String value) {
084 value = (value == null) ? "" : value;
085 return values.contains(value);
086 }
087
088 public void addValue(String value) {
089 value = (value == null) ? "" : value;
090 if (hasValue(value)) {
091 return;
092 }
093 values.add(value);
094 }
095
096 /**
097 * removes a value from the list of values. Converts value to "" if null
098 * @param value the value
099 */
100 public void removeValue(String value){
101 value = (value == null) ? "" : value;
102 values.remove(value);
103 }
104
105 public List<String> getValues() {
106 return values;
107 }
108
109 public String getValue() {
110 if (getValueCount() == 0) {
111 return "";
112 } else if (getValueCount() == 1) {
113 return values.get(0);
114 } else {
115 StringBuilder sb = new StringBuilder();
116 for (int i =0; i < values.size(); i++) {
117 sb.append(values.get(i));
118 if (i + 1 < values.size()) {
119 sb.append(";");
120 }
121 }
122 return sb.toString();
123 }
124 }
125
126 public int getValueCount() {
127 return values.size();
128 }
129 }