001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.tagging; 003 004import java.util.ArrayList; 005import java.util.List; 006 007/** 008 * Tag model. 009 * @since 1762 010 */ 011public class TagModel { 012 013 /** the name of the tag */ 014 private String name; 015 016 /** the list of values */ 017 private List<String> values; 018 019 /** 020 * constructor 021 */ 022 public TagModel() { 023 values = new ArrayList<>(); 024 setName(""); 025 setValue(""); 026 } 027 028 /** 029 * constructor 030 * @param name the tag name 031 */ 032 public TagModel(String name) { 033 this(); 034 setName(name); 035 } 036 037 /** 038 * constructor 039 * 040 * @param name the tag name 041 * @param value the tag value 042 */ 043 public TagModel(String name, String value) { 044 this(); 045 setName(name); 046 setValue(value); 047 } 048 049 /** 050 * sets the name. Converts name to "" if null. 051 * @param name the tag name 052 */ 053 public final void setName(String name) { 054 this.name = (name == null) ? "" : name; 055 } 056 057 /** 058 * returns the tag name (key). 059 * @return the tag name 060 */ 061 public String getName() { 062 return name; 063 } 064 065 /** 066 * removes all values from the list of values 067 */ 068 public void clearValues() { 069 this.values.clear(); 070 } 071 072 /** 073 * sets a unique value for this tag. Converts value to "", if null. 074 * @param value the value. 075 */ 076 public final void setValue(String value) { 077 clearValues(); 078 this.values.add((value == null) ? "" : value); 079 } 080 081 /** 082 * determines if this tag model has a specific value 083 * @param value the value to be checked; converted to "" if null 084 * @return true, if the values of this tag include <code>value</code>; false otherwise 085 */ 086 public boolean hasValue(String value) { 087 return values.contains((value == null) ? "" : value); 088 } 089 090 /** 091 * adds a tag value 092 * @param value the value to add; converted to "" if null 093 */ 094 public void addValue(String value) { 095 String val = (value == null) ? "" : value; 096 if (hasValue(val)) { 097 return; 098 } 099 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}