4 * Copyright 2015 TJ <hacker@iam.tj>.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package uk.ac.ntu.n0521366.wsyd.libs;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.Arrays;
29 import java.util.Date;
30 import java.util.TreeSet;
31 import java.util.logging.Level;
34 * Contains personal data about a member.
36 * _userID contains a key that uniquely identifies this member. This should be used by other
37 * classes and friend references to ensure data is not duplicated unnecessarily.
39 * The object can be serialized over a stream to a File or Network connection and added to a sorted collection.
40 * It can be added to ordered Collections and provides its own compareTo() using userName.
42 * It is supported by 2 Comparators:
44 * WSYD_Member_Comparator_UserID supports sorted Maps with userID as the sort key
45 * WSYD_Member_Comparator_UserName supports sorted Sets and Collections with userName as the sort key
47 * @author TJ <hacker@iam.tj>
49 public class WSYD_Member implements java.io.Serializable, java.lang.Comparable<WSYD_Member> {
51 public String _userName;
52 public String _password;
53 public String _currentLocation;
55 public Date _birthDate;
56 public TreeSet<String> _interests;
57 public TreeSet<Long> _friends;
58 public TreeSet<Long> _friendsRequestsSent;
59 public TreeSet<Long> _friendsRequestsReceived;
61 public static enum Fields {
62 USER_ID, USER_NAME, PASSWORD, CURRENT_LOCATION, BIO, BIRTH_DATE, INTERESTS, FRIENDS, FRIENDS_REQUESTS_SENT, FRIENDS_REQUESTS_RECEIVED
68 public WSYD_Member() {}
71 * Construct a complete member
73 * @param userID unique key
74 * @param userName user's preferred name
75 * @param password password for authentication
76 * @param currentLocation Geographic area
77 * @param bio Personal biography
78 * @param birthDate date of birth
79 * @param interests titles of personal interests
80 * @param friends IDs of all accepted friends
81 * @param friendsRequestsSent IDs of pending friends requests
82 * @param friendsRequestsReceived IDs of pending friends requests sent
84 public WSYD_Member(long userID, String userName, String password, String currentLocation, String bio,
86 TreeSet<String> interests,
87 TreeSet<Long> friends,
88 TreeSet<Long> friendsRequestsSent,
89 TreeSet<Long> friendsRequestsReceived
95 _currentLocation = currentLocation;
97 _birthDate = birthDate;
98 _interests = interests;
100 _friendsRequestsSent = friendsRequestsSent;
101 _friendsRequestsReceived = friendsRequestsReceived;
104 public static WSYD_Member createWSYD_Member(String line) throws IllegalArgumentException {
105 WSYD_Member result = null;
107 if (line.startsWith("#")) {
108 return result; // ignore comment lines
110 result = new WSYD_Member();
111 // split line into array of fields
112 String[] fields = line.split(",");
113 if (fields.length != 10)
114 throw new IllegalArgumentException();
116 // convert each field from a String to the correct type using the Fields enum
117 result._userID = new Long(fields[WSYD_Member.Fields.USER_ID.ordinal()]);
118 result._userName = fields[WSYD_Member.Fields.USER_NAME.ordinal()];
119 result._password = fields[WSYD_Member.Fields.PASSWORD.ordinal()];
120 result._currentLocation = fields[WSYD_Member.Fields.CURRENT_LOCATION.ordinal()];
121 result._bio = fields[WSYD_Member.Fields.BIO.ordinal()];
122 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
124 result._birthDate = df.parse(fields[WSYD_Member.Fields.BIRTH_DATE.ordinal()]);
125 } catch (ParseException e) {
126 result._birthDate = new Date();
128 // read the multi-element entries. "~" is used as the element separator
129 result._interests = new TreeSet<>();
130 String[] items = fields[WSYD_Member.Fields.INTERESTS.ordinal()].split("~");
131 result._interests.addAll(Arrays.asList(items));
132 result._friends = new TreeSet<>();
133 items = fields[WSYD_Member.Fields.FRIENDS.ordinal()].split("~");
134 for (String item : items) {
135 result._friends.add(new java.lang.Long(item));
137 result._friendsRequestsSent = new TreeSet<>();
138 items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_SENT.ordinal()].split("~");
139 for (String item : items) {
140 result._friendsRequestsSent.add(new Long(item));
142 result._friendsRequestsReceived = new TreeSet<>();
143 items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_RECEIVED.ordinal()].split("~");
144 for (String item : items) {
145 result._friendsRequestsReceived.add(new Long(item));
152 * implement the java.lang.Comparable interface
154 * @param m the member to compare with
155 * @return Returns a negative integer, zero, or a positive integer as this member is less than, equal to, or greater than the provided member
158 public int compareTo(WSYD_Member m) throws IllegalArgumentException {
160 throw new IllegalArgumentException("Reference cannot be null");
161 return this._userName.compareTo(m._userName);
165 * Convert to a String suitable for display and export to CSV
167 * Fields containing collections write collection members separated by "~"
172 public String toString() {
173 String interests = new String();
174 for (String i: _interests) {
175 interests = interests.concat((interests.length() > 0 ? "~" : "") + i);
177 String friends = new String();
178 for (Long l: _friends) {
179 friends = friends.concat((friends.length() > 0 ? "~" : "") + l.toString());
181 String friendsRequestsSent = new String();
182 for (Long l: _friendsRequestsSent) {
183 friendsRequestsSent = friendsRequestsSent.concat((friendsRequestsSent.length() > 0 ? "~" : "") + l.toString());
185 String friendsRequestsReceived = new String();
186 for (Long l: _friendsRequestsReceived) {
187 friendsRequestsReceived = friendsRequestsReceived.concat((friendsRequestsReceived.length() > 0 ? "~" : "") + l.toString());
189 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
191 String record = Long.toString(_userID) + ","
194 + _currentLocation + ","
196 + df.format(_birthDate) + ","
199 + friendsRequestsSent + ","
200 + friendsRequestsReceived;