4 * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
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.ArrayList;
29 import java.util.Arrays;
30 import java.util.Date;
31 import java.util.TreeSet;
32 import java.util.logging.Level;
35 * Contains personal data about a member.
37 * _userID contains a key that uniquely identifies this member. This should be used by other
38 * classes and friend references to ensure data is not duplicated unnecessarily.
40 * The object can be serialized over a stream to a File or Network connection and added to a sorted collection.
41 * It can be added to ordered Collections and provides its own compareTo() using userName.
43 * It is supported by 2 Comparators:
45 * WSYD_Member_Comparator_UserID supports sorted Maps with userID as the sort key
46 * WSYD_Member_Comparator_UserName supports sorted Sets and Collections with userName as the sort key
48 * @author Eddie Berrisford-Lynch <dev@fun2be.me>
50 public class WSYD_Member implements java.io.Serializable, java.lang.Comparable<WSYD_Member> {
53 * Choices for _interests
55 public static String[] Interests = {"Computers","Driving","DIY","Gardening","Games","Gym","Music","Reading" };
58 public String _userName;
59 public String _password;
60 public String _currentLocation;
62 public Date _birthDate;
63 public TreeSet<String> _interests;
64 public TreeSet<Long> _friends;
65 public TreeSet<Long> _friendsRequestsSent;
66 public TreeSet<Long> _friendsRequestsReceived;
68 public static enum Fields {
69 USER_ID, USER_NAME, PASSWORD, CURRENT_LOCATION, BIO, BIRTH_DATE, INTERESTS, FRIENDS, FRIENDS_REQUESTS_SENT, FRIENDS_REQUESTS_RECEIVED
75 public WSYD_Member() {}
78 * Construct a complete member
80 * @param userID unique key
81 * @param userName user's preferred name
82 * @param password password for authentication
83 * @param currentLocation Geographic area
84 * @param bio Personal biography
85 * @param birthDate date of birth
86 * @param interests titles of personal interests
87 * @param friends IDs of all accepted friends
88 * @param friendsRequestsSent IDs of pending friends requests
89 * @param friendsRequestsReceived IDs of pending friends requests sent
91 public WSYD_Member(long userID, String userName, String password, String currentLocation, String bio,
93 TreeSet<String> interests,
94 TreeSet<Long> friends,
95 TreeSet<Long> friendsRequestsSent,
96 TreeSet<Long> friendsRequestsReceived
100 _userName = userName;
101 _password = password;
102 _currentLocation = currentLocation;
104 _birthDate = birthDate;
105 _interests = interests;
107 _friendsRequestsSent = friendsRequestsSent;
108 _friendsRequestsReceived = friendsRequestsReceived;
111 public static WSYD_Member createWSYD_Member(String line) throws IllegalArgumentException {
112 WSYD_Member result = null;
114 if (line.startsWith("#")) {
115 return result; // ignore comment lines
117 result = new WSYD_Member();
118 // split line into array of fields
119 String[] fields = line.split(",");
120 if (fields.length != 10)
121 throw new IllegalArgumentException();
123 // convert each field from a String to the correct type using the Fields enum
124 result._userID = new Long(fields[WSYD_Member.Fields.USER_ID.ordinal()]);
125 result._userName = fields[WSYD_Member.Fields.USER_NAME.ordinal()];
126 result._password = fields[WSYD_Member.Fields.PASSWORD.ordinal()];
127 result._currentLocation = fields[WSYD_Member.Fields.CURRENT_LOCATION.ordinal()];
128 result._bio = fields[WSYD_Member.Fields.BIO.ordinal()];
129 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
131 result._birthDate = df.parse(fields[WSYD_Member.Fields.BIRTH_DATE.ordinal()]);
132 } catch (ParseException e) {
133 result._birthDate = new Date();
135 // read the multi-element entries. "~" is used as the element separator
136 result._interests = new TreeSet<>();
137 String[] items = fields[WSYD_Member.Fields.INTERESTS.ordinal()].split("~");
138 result._interests.addAll(Arrays.asList(items));
139 result._friends = new TreeSet<>();
140 items = fields[WSYD_Member.Fields.FRIENDS.ordinal()].split("~");
141 for (String item : items) {
142 result._friends.add(new java.lang.Long(item));
144 result._friendsRequestsSent = new TreeSet<>();
145 items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_SENT.ordinal()].split("~");
146 for (String item : items) {
147 result._friendsRequestsSent.add(new Long(item));
149 result._friendsRequestsReceived = new TreeSet<>();
150 items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_RECEIVED.ordinal()].split("~");
151 for (String item : items) {
152 result._friendsRequestsReceived.add(new Long(item));
159 * implement the java.lang.Comparable interface
161 * @param m the member to compare with
162 * @return Returns a negative integer, zero, or a positive integer as this member is less than, equal to, or greater than the provided member
165 public int compareTo(WSYD_Member m) throws IllegalArgumentException {
167 throw new IllegalArgumentException("Reference cannot be null");
168 return this._userName.compareTo(m._userName);
172 * Convert to a String suitable for display and export to CSV
174 * Fields containing collections write collection members separated by "~"
179 public String toString() {
180 String interests = new String();
181 for (String i: _interests) {
182 interests = interests.concat((interests.length() > 0 ? "~" : "") + i);
184 String friends = new String();
185 for (Long l: _friends) {
186 friends = friends.concat((friends.length() > 0 ? "~" : "") + l.toString());
188 String friendsRequestsSent = new String();
189 for (Long l: _friendsRequestsSent) {
190 friendsRequestsSent = friendsRequestsSent.concat((friendsRequestsSent.length() > 0 ? "~" : "") + l.toString());
192 String friendsRequestsReceived = new String();
193 for (Long l: _friendsRequestsReceived) {
194 friendsRequestsReceived = friendsRequestsReceived.concat((friendsRequestsReceived.length() > 0 ? "~" : "") + l.toString());
196 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
198 String record = Long.toString(_userID) + ","
201 + _currentLocation + ","
203 + df.format(_birthDate) + ","
206 + friendsRequestsSent + ","
207 + friendsRequestsReceived;