source: josm/trunk/src/org/openstreetmap/josm/data/osm/User.java@ 6223

Last change on this file since 6223 was 6223, checked in by Don-vip, 11 years ago

Sonar - fix various violations

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.List;
10import java.util.concurrent.atomic.AtomicLong;
11
12import org.openstreetmap.josm.tools.Utils;
13
14/**
15 * A simple class to keep a list of user names.
16 *
17 * Instead of storing user names as strings with every OSM primitive, we store
18 * a reference to an user object, and make sure that for each username there
19 * is only one user object.
20 *
21 *
22 */
23public class User {
24
25 static private AtomicLong uidCounter = new AtomicLong();
26
27 /**
28 * the map of known users
29 */
30 private static HashMap<Long,User> userMap = new HashMap<Long,User>();
31 private final static User anonymous = createLocalUser(tr("<anonymous>"));
32
33 private static long getNextLocalUid() {
34 return uidCounter.decrementAndGet();
35 }
36
37 /**
38 * Creates a local user with the given name
39 *
40 * @param name the name
41 * @return a new local user with the given name
42 */
43 public static User createLocalUser(String name) {
44 for(long i = -1; i >= uidCounter.get(); --i)
45 {
46 User olduser = getById(i);
47 if(olduser != null && olduser.hasName(name))
48 return olduser;
49 }
50 User user = new User(getNextLocalUid(), name);
51 userMap.put(user.getId(), user);
52 return user;
53 }
54
55 /**
56 * Creates a user known to the OSM server
57 *
58 * @param uid the user id
59 * @param name the name
60 * @return a new OSM user with the given name and uid
61 */
62 public static User createOsmUser(long uid, String name) {
63 User user = userMap.get(uid);
64 if (user == null) {
65 user = new User(uid, name);
66 userMap.put(user.getId(), user);
67 }
68 if (name != null) user.addName(name);
69 return user;
70 }
71
72 /**
73 * clears the static map of user ids to user objects
74 *
75 */
76 public static void clearUserMap() {
77 userMap.clear();
78 }
79
80 /**
81 * Returns the user with user id <code>uid</code> or null if this user doesn't exist
82 *
83 * @param uid the user id
84 * @return the user; null, if there is no user with this id
85 */
86 public static User getById(long uid) {
87 return userMap.get(uid);
88 }
89
90 /**
91 * Returns the list of users with name <code>name</code> or the empty list if
92 * no such users exist
93 *
94 * @param name the user name
95 * @return the list of users with name <code>name</code> or the empty list if
96 * no such users exist
97 */
98 public static List<User> getByName(String name) {
99 if (name == null) {
100 name = "";
101 }
102 List<User> ret = new ArrayList<User>();
103 for (User user: userMap.values()) {
104 if (user.hasName(name)) {
105 ret.add(user);
106 }
107 }
108 return ret;
109 }
110
111 /**
112 * Replies the anonymous user
113 * @return The anonymous user
114 */
115 public static User getAnonymous() {
116 return anonymous;
117 }
118
119 /** the user name */
120 private final HashSet<String> names = new HashSet<String>();
121 /** the user id */
122 private final long uid;
123
124 /**
125 * Replies the user name
126 *
127 * @return the user name. Never <code>null</code>, but may be the empty string
128 */
129 public String getName() {
130 return Utils.join("/", names);
131 }
132
133 /**
134 * Returns the list of user names
135 *
136 * @return list of names
137 */
138 public ArrayList<String> getNames() {
139 return new ArrayList<String>(names);
140 }
141
142 /**
143 * Adds a user name to the list if it is not there, yet.
144 *
145 * @param name
146 */
147 public void addName(String name) {
148 names.add(name);
149 }
150
151 /**
152 * Returns true if the name is in the names list
153 *
154 * @param name
155 * @return <code>true</code> if the name is in the names list
156 */
157 public boolean hasName(String name) {
158 return names.contains(name);
159 }
160
161 /**
162 * Replies the user id. If this user is known to the OSM server the positive user id
163 * from the server is replied. Otherwise, a negative local value is replied.
164 *
165 * A negative local is only unique during an editing session. It is lost when the
166 * application is closed and there is no guarantee that a negative local user id is
167 * always bound to a user with the same name.
168 *
169 * @return the user id
170 */
171 public long getId() {
172 return uid;
173 }
174
175 /** private constructor, only called from get method. */
176 private User(long uid, String name) {
177 this.uid = uid;
178 if (name != null) {
179 addName(name);
180 }
181 }
182
183 /**
184 * Determines if this user is known to OSM
185 * @return {@code true} if this user is known to OSM, {@code false} otherwise
186 */
187 public boolean isOsmUser() {
188 return uid > 0;
189 }
190
191 /**
192 * Determines if this user is local
193 * @return {@code true} if this user is local, {@code false} otherwise
194 */
195 public boolean isLocalUser() {
196 return uid < 0;
197 }
198
199 @Override
200 public int hashCode() {
201 final int prime = 31;
202 int result = 1;
203 result = prime * result + getName().hashCode();
204 result = prime * result + (int) (uid ^ (uid >>> 32));
205 return result;
206 }
207
208 @Override
209 public boolean equals(Object obj) {
210 if (! (obj instanceof User))
211 return false;
212 User other = (User) obj;
213 if (uid != other.uid)
214 return false;
215 return true;
216 }
217
218 @Override
219 public String toString() {
220 StringBuffer s = new StringBuffer();
221 s.append("id:").append(uid);
222 if (names.size() == 1) {
223 s.append(" name:").append(getName());
224 }
225 else if (names.size() > 1) {
226 s.append(String.format(" %d names:%s", names.size(), getName()));
227 }
228 return s.toString();
229 }
230}
Note: See TracBrowser for help on using the repository browser.