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

Last change on this file since 3715 was 3505, checked in by stoecker, 14 years ago

fix #5402 - creates different users for same name when no OSM id given

  • Property svn:eol-style set to native
File size: 5.3 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 return uidCounter.decrementAndGet();
30 }
31
32 /**
33 * Creates a local user with the given name
34 *
35 * @param name the name
36 */
37 public static User createLocalUser(String name) {
38 for(long i = -1; i >= uidCounter.get(); --i)
39 {
40 User olduser = getById(i);
41 if(olduser != null && olduser.hasName(name))
42 return olduser;
43 }
44 User user = new User(getNextLocalUid(), name);
45 userMap.put(user.getId(), user);
46 return user;
47 }
48
49 /**
50 * Creates a user known to the OSM server
51 *
52 * @param uid the user id
53 * @param name the name
54 */
55 public static User createOsmUser(long uid, String name) {
56 User user = userMap.get(uid);
57 if (user == null) {
58 user = new User(uid, name);
59 userMap.put(user.getId(), user);
60 }
61 user.addName(name);
62 return user;
63 }
64
65 /**
66 * clears the static map of user ids to user objects
67 *
68 */
69 public static void clearUserMap() {
70 userMap.clear();
71 }
72
73 /**
74 * Returns the user with user id <code>uid</code> or null if this user doesn't exist
75 *
76 * @param uid the user id
77 * @return the user; null, if there is no user with this id
78 */
79 public static User getById(long uid) {
80 return userMap.get(uid);
81 }
82
83 /**
84 * Returns the list of users with name <code>name</code> or the empty list if
85 * no such users exist
86 *
87 * @param name the user name
88 * @return the list of users with name <code>name</code> or the empty list if
89 * no such users exist
90 */
91 public static List<User> getByName(String name) {
92 if (name == null) {
93 name = "";
94 }
95 List<User> ret = new ArrayList<User>();
96 for (User user: userMap.values()) {
97 if (user.hasName(name)) {
98 ret.add(user);
99 }
100 }
101 return ret;
102 }
103
104 /** the user name */
105 private final HashSet<String> names = new HashSet<String>();
106 /** the user id */
107 private final long uid;
108
109 /**
110 * Replies the user name
111 *
112 * @return the user name. Never null, but may be the empty string
113 */
114 public String getName() {
115 StringBuilder sb = new StringBuilder();
116 for (String name: names) {
117 sb.append(name);
118 sb.append('/');
119 }
120 sb.deleteCharAt(sb.length() - 1);
121 return sb.toString();
122 }
123
124 /**
125 * Returns the list of user names
126 *
127 * @returns list of names
128 */
129 public ArrayList<String> getNames() {
130 return new ArrayList<String>(names);
131 }
132
133 /**
134 * Adds a user name to the list if it is not there, yet.
135 *
136 * @param name
137 */
138 public void addName(String name) {
139 names.add(name);
140 }
141
142 /**
143 * Returns true if the name is in the names list
144 *
145 * @param name
146 */
147 public boolean hasName(String name) {
148 return names.contains(name);
149 }
150
151 /**
152 * Replies the user id. If this user is known to the OSM server the positive user id
153 * from the server is replied. Otherwise, a negative local value is replied.
154 *
155 * A negative local is only unique during an editing session. It is lost when the
156 * application is closed and there is no guarantee that a negative local user id is
157 * always bound to a user with the same name.
158 *
159 */
160 public long getId() {
161 return uid;
162 }
163
164 /** private constructor, only called from get method. */
165 private User(long uid, String name) {
166 this.uid = uid;
167 if (name != null) {
168 addName(name);
169 }
170 }
171
172 public boolean isOsmUser() {
173 return uid > 0;
174 }
175
176 public boolean isLocalUser() {
177 return uid < 0;
178 }
179
180 @Override
181 public int hashCode() {
182 final int prime = 31;
183 int result = 1;
184 result = prime * result + getName().hashCode();
185 result = prime * result + (int) (uid ^ (uid >>> 32));
186 return result;
187 }
188
189 @Override
190 public boolean equals(Object obj) {
191 if (! (obj instanceof User))
192 return false;
193 User other = (User) obj;
194 if (uid != other.uid)
195 return false;
196 return true;
197 }
198
199 @Override
200 public String toString() {
201 StringBuffer s = new StringBuffer();
202 s.append("id:"+uid);
203 if (names.size() == 1) {
204 s.append(" name:"+getName());
205 }
206 else if (names.size() > 1) {
207 s.append(String.format(" %d names:%s", names.size(), getName()));
208 }
209 return s.toString();
210 }
211}
Note: See TracBrowser for help on using the repository browser.