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

Last change on this file since 2667 was 2638, checked in by mjulius, 14 years ago

fixes #4175 - Exception when user with existing ID but different name is encountered
User now keeps a list of all names for the uid and users are considered equal if the uids are equal. User.getName() now returns all names joined by '/'.

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