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

Last change on this file since 2455 was 2455, checked in by jttt, 14 years ago

Do not create new instance of User for every OsmPrimitive, share instances for the same user

  • Property svn:eol-style set to native
File size: 4.2 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.List;
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 static private long uidCounter = 0;
21 /**
22 * the map of known users
23 */
24 private static HashMap<Long,User> userMap = new HashMap<Long,User>();
25
26
27 private static long getNextLocalUid() {
28 synchronized(User.class) {
29 uidCounter--;
30 return uidCounter;
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 if (!user.getName().equals(name))
58 throw new DataIntegrityProblemException(tr("User with the same uid but different name found"));
59 return user;
60 }
61
62
63 /**
64 * Returns the user with user id <code>uid</code> or null if this user doesn't exist
65 *
66 * @param uid the user id
67 * @return the user; null, if there is no user with this id
68 */
69 public static User getById(long uid) {
70 return userMap.get(uid);
71 }
72
73 /**
74 * Returns the list of users with name <code>name</code> or the empty list if
75 * no such users exist
76 *
77 * @param name the user name
78 * @return the list of users with name <code>name</code> or the empty list if
79 * no such users exist
80 */
81 public static List<User> getByName(String name) {
82 if (name == null) {
83 name = "";
84 }
85 List<User> ret = new ArrayList<User>();
86 for (User user: userMap.values()) {
87 if (user.getName().equals(name)) {
88 ret.add(user);
89 }
90 }
91 return ret;
92 }
93
94 /** the user name */
95 private final String name;
96 /** the user id */
97 private final long uid;
98
99 /**
100 * Replies the user name
101 *
102 * @return the user name. Never null, but may be the empty string
103 */
104 public String getName() {
105 return name;
106 }
107
108 /**
109 * Replies the user id. If this user is known to the OSM server the positive user id
110 * from the server is replied. Otherwise, a negative local value is replied.
111 *
112 * A negative local is only unique during an editing session. It is lost when the
113 * application is closed and there is no guarantee that a negative local user id is
114 * always bound to a user with the same name.
115 *
116 */
117 public long getId() {
118 return uid;
119 }
120
121 /** private constructor, only called from get method. */
122 private User(long uid, String name) {
123 this.uid = uid;
124 if (name == null) {
125 this.name = "";
126 } else {
127 this.name = name;
128 }
129 }
130
131 public boolean isOsmUser() {
132 return uid > 0;
133 }
134
135 public boolean isLocalUser() {
136 return uid < 0;
137 }
138
139 @Override
140 public int hashCode() {
141 final int prime = 31;
142 int result = 1;
143 result = prime * result + name.hashCode();
144 result = prime * result + (int) (uid ^ (uid >>> 32));
145 return result;
146 }
147
148 @Override
149 public boolean equals(Object obj) {
150 if (obj instanceof User) {
151 User other = (User) obj;
152 if (!name.equals(other.name))
153 return false;
154 if (uid != other.uid)
155 return false;
156 return true;
157 } else
158 return false;
159 }
160}
Note: See TracBrowser for help on using the repository browser.