From 9d0c533167518aac56c53d29655725c5ce7b77c6 Mon Sep 17 00:00:00 2001
From: Michael Zangl <michael.zangl@student.kit.edu>
Date: Fri, 27 Mar 2015 22:22:46 +0100
Subject: [PATCH] Made user map synchronized.
---
src/org/openstreetmap/josm/data/osm/User.java | 32 +++++++++++++++------------
1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/src/org/openstreetmap/josm/data/osm/User.java b/src/org/openstreetmap/josm/data/osm/User.java
index b87df75..481dd22 100644
|
a
|
b
|
import java.util.HashSet;
|
| 9 | 9 | import java.util.List; |
| 10 | 10 | import java.util.Map; |
| 11 | 11 | import java.util.Set; |
| 12 | | import java.util.concurrent.atomic.AtomicLong; |
| 13 | 12 | |
| 14 | 13 | import org.openstreetmap.josm.tools.Utils; |
| 15 | 14 | |
| … |
… |
import org.openstreetmap.josm.tools.Utils;
|
| 24 | 23 | */ |
| 25 | 24 | public final class User { |
| 26 | 25 | |
| 27 | | private static AtomicLong uidCounter = new AtomicLong(); |
| | 26 | private static long uidCounter = 0; |
| 28 | 27 | |
| 29 | 28 | /** |
| 30 | 29 | * the map of known users |
| 31 | 30 | */ |
| 32 | 31 | private static Map<Long,User> userMap = new HashMap<>(); |
| | 32 | |
| | 33 | /** |
| | 34 | * The anonymous user is a local user used in places where no user is known. |
| | 35 | * @see #getAnonymous() |
| | 36 | */ |
| 33 | 37 | private static final User anonymous = createLocalUser(tr("<anonymous>")); |
| 34 | 38 | |
| 35 | 39 | private static long getNextLocalUid() { |
| 36 | | return uidCounter.decrementAndGet(); |
| | 40 | uidCounter--; |
| | 41 | return uidCounter; |
| 37 | 42 | } |
| 38 | 43 | |
| 39 | 44 | /** |
| … |
… |
public final class User {
|
| 42 | 47 | * @param name the name |
| 43 | 48 | * @return a new local user with the given name |
| 44 | 49 | */ |
| 45 | | public static User createLocalUser(String name) { |
| 46 | | for(long i = -1; i >= uidCounter.get(); --i) |
| | 50 | public static synchronized User createLocalUser(String name) { |
| | 51 | for(long i = -1; i >= uidCounter; --i) |
| 47 | 52 | { |
| 48 | | User olduser = getById(i); |
| 49 | | if(olduser != null && olduser.hasName(name)) |
| 50 | | return olduser; |
| | 53 | User olduser = getById(i); |
| | 54 | if(olduser != null && olduser.hasName(name)) |
| | 55 | return olduser; |
| 51 | 56 | } |
| 52 | 57 | User user = new User(getNextLocalUid(), name); |
| 53 | 58 | userMap.put(user.getId(), user); |
| … |
… |
public final class User {
|
| 61 | 66 | * @param name the name |
| 62 | 67 | * @return a new OSM user with the given name and uid |
| 63 | 68 | */ |
| 64 | | public static User createOsmUser(long uid, String name) { |
| | 69 | public static synchronized User createOsmUser(long uid, String name) { |
| 65 | 70 | User user = userMap.get(uid); |
| 66 | 71 | if (user == null) { |
| 67 | 72 | user = new User(uid, name); |
| … |
… |
public final class User {
|
| 73 | 78 | |
| 74 | 79 | /** |
| 75 | 80 | * clears the static map of user ids to user objects |
| 76 | | * |
| 77 | 81 | */ |
| 78 | | public static void clearUserMap() { |
| | 82 | public static synchronized void clearUserMap() { |
| 79 | 83 | userMap.clear(); |
| 80 | 84 | } |
| 81 | 85 | |
| … |
… |
public final class User {
|
| 85 | 89 | * @param uid the user id |
| 86 | 90 | * @return the user; null, if there is no user with this id |
| 87 | 91 | */ |
| 88 | | public static User getById(long uid) { |
| 89 | | return userMap.get(uid); |
| | 92 | public static synchronized User getById(long uid) { |
| | 93 | return userMap.get(uid); |
| 90 | 94 | } |
| 91 | 95 | |
| 92 | 96 | /** |
| … |
… |
public final class User {
|
| 97 | 101 | * @return the list of users with name <code>name</code> or the empty list if |
| 98 | 102 | * no such users exist |
| 99 | 103 | */ |
| 100 | | public static List<User> getByName(String name) { |
| | 104 | public static synchronized List<User> getByName(String name) { |
| 101 | 105 | if (name == null) { |
| 102 | 106 | name = ""; |
| 103 | 107 | } |