Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Collections;
- import java.util.Date;
- import java.util.Set;
- /**
- * Write a description of class Server here.
- *
- * A simple server, containing the most important information
- *
- *
- * @author (Peter De Wolf)
- * @version (22.04.2012)
- */
- public class Server
- {
- // A list with all the registered users.
- private ArrayList<User> users;
- // This field keeps track of the user who is logged in at the moment.
- private User currentUser;
- // This is a built in counter that keeps track of the unique id's for tweets, retweets and directmessages.
- private long uniqueID;
- // This is a list that stores all the tweets and retweets.
- private ArrayList<Message> tweetsAndRetweets;
- // This field has the maximum length of a tweet, by adjusting this number in the constructor, the changes will apply generally.
- private int maxTweetLength;
- /**
- * Construct 2 new arraylists, 1 userobject, a unique ID and the maxtweetlength.
- */
- public Server()
- {
- users = new ArrayList<User>();
- currentUser = null;
- tweetsAndRetweets = new ArrayList<Message>();
- uniqueID = 1;
- maxTweetLength = 140;
- }
- /**
- * This method returns an integer. The integer returned is the maximum tweetlength that is allowed.
- */
- public int getMaxTweetLength()
- {
- return maxTweetLength;
- }
- /**
- * This method adds a user-object to the user-list
- * @param userName: this is the username
- * @param fullName: this is the full name of the user
- * @param password: this is the password that the user will use to log in
- * If the username is not already taken, a new user will be registered.
- * @ return this method returns a boolean that will tell you if the user was registered succesfully.
- */
- public boolean addUser(String userName, String fullName,String password)
- {
- User newUser = new User(userName, fullName, password);
- boolean uniqueNickName = true;
- for( User user : users)
- if( newUser.getNickName().equals( user.getNickName()))
- {
- uniqueNickName = false;
- }
- if( uniqueNickName)
- {
- users.add(newUser);
- newUser.addFollowing(newUser);
- newUser.addFollower(newUser);
- }
- return uniqueNickName;
- }
- /**
- * This method is used to log in.
- * @param userName: This is the username of the user that wants to log in.
- * @param password: This is the password of the user that wants to log in.
- * If password and username match, the user will be logged in.
- * @ return this method returns a boolean that will tell you if the user succesfully logged in.
- */
- public boolean logIn(String userName, String password)
- {
- boolean logInSuccesFull = false;
- boolean userExists = false;
- if ( currentUser == null )
- {
- for(User user: users)
- if( user.getNickName().equals(userName) && password.equals(user.getPassWord()))
- {
- currentUser = user;
- logInSuccesFull = true;
- userExists = true;
- }
- }
- return logInSuccesFull;
- }
- /**
- * This method is used to set the currentUser to null, then there is nobody logged in.
- */
- public void logOut()
- {
- currentUser = null;
- }
- /**
- * This method is used to find all the users in the userdatabase which fullname or username contains a cetrain searchstring.
- * @param searchstring: This is the string that must be present in the username or fullname
- * @param numberOfResults: This integer is used to indicate the number of userNames that this method will return
- */
- public List<String> searchUsers( String searchString, int numberOfResults)
- {
- List<String> searchResult = new ArrayList<String>();
- int index = 0;
- for( User user: users)
- {
- if( user.getNickName().contains(searchString) || user.getFullName().contains(searchString) && index < numberOfResults )
- {
- index++;
- searchResult.add(user.getNickName());
- }
- }
- return searchResult;
- }
- /**
- * This method will return the usernames of all the people the currentuser is following.
- * @return A list with strings of people the current user is following.
- */
- public List<String> getFriends()
- {
- List<String> friends = currentUser.getFriends();
- friends.remove(0);
- return friends;
- }
- /**
- * This method will return the usernames of all the people that are following the current user.
- * @return A list with strings of the people that are following the current user.
- */
- public List<String> getFollowers()
- {
- List<String> followers = currentUser.getFollowers();
- followers.remove(0);
- return followers;
- }
- /**
- * This method will remove a user from the list of users the currentuser is following.
- * @ param the username of the user that must be removed from the list;
- * @ return this method returns a boolean that will tell u if the action was succesfull.
- */
- public boolean unfollow(String userName)
- {
- User personIWantToStopFollowing = null;
- boolean actionSuccesFull = false;
- for(User user: users)
- {
- if(user.getNickName().equals(userName) && userName != currentUser.getNickName())
- {
- personIWantToStopFollowing = user;
- }
- }
- if( personIWantToStopFollowing != null)
- {
- actionSuccesFull = currentUser.unfollow(personIWantToStopFollowing);
- if(actionSuccesFull)
- {
- personIWantToStopFollowing.removeFollower(currentUser);
- }
- }
- return actionSuccesFull;
- }
- /**
- * This method will add the user with a certain username to the list of the people the current user is following.
- * @ param the username of the user that must be added to the list of friends.
- * @ return this method will return a boolean that will tell u if u started following the user
- */
- public boolean follow(String userName)
- {
- boolean following= false;
- ArrayList<String> friends = currentUser.getFriends();
- for(String nickName: friends)
- {
- if( nickName.equals(userName))
- {
- following = true;
- }
- }
- if(following == false)
- {
- for(User user: users)
- {
- if( user.getNickName().equals(userName))
- {
- currentUser.addFollowing(user);
- user.addFollower(currentUser);
- following = true;
- }
- }
- }
- return following;
- }
- /**
- * This method is used to check the current user
- * @ return if there is no current user this method will return a string saying it, else, it will return a string containing the username of the current user.
- */
- public String getCurrentUser()
- {
- if( currentUser != null)
- {
- return currentUser.getNickName();
- }
- else
- {
- return null;
- }
- }
- /**
- * This method will send a directmessage to one certain user.
- * @ param receiverusername is the name of the user that will get this private message.
- * @ param message is the content of the message that will be sent to the user
- * @ return this will return the messageID of the directmessage if the sending was succesfull, or 0 when the sending was not succesfull.
- */
- public long sendDirectMessage(String recieverUserName, String message)
- {
- User reciever = null;
- boolean recieverFollowsSender = false;
- boolean senderFollowsReciever = false;
- for (User user: users)
- {
- if(user.getNickName().equals(recieverUserName))
- {
- reciever = user;
- }
- }
- if( reciever != null)
- {
- for(String userName: reciever.getFollowers())
- {
- if (userName.equals(currentUser.getNickName()))
- {
- recieverFollowsSender = true;
- }
- }
- }
- for(String userName: currentUser.getFollowers())
- {
- if(userName.equals(recieverUserName))
- {
- senderFollowsReciever = true;
- }
- }
- if( recieverFollowsSender && senderFollowsReciever )
- {
- DirectMessage DM = new DirectMessage(uniqueID,message, currentUser.getNickName());
- reciever.addDirectMessage(DM);
- uniqueID++;
- return uniqueID-1;
- }
- else
- {
- return 0;
- }
- }
- /**
- * This method will return a userdescription of a user, including the username, fullname, amount of tweets, followers and friends.
- * @ param username is the name of the user we want the userdescription from.
- * @ return a string containing the userdescription of a user, including the username, fullname, amount of tweets, followers and friends.
- */
- public String getUserDescription(String userName)
- {
- boolean userFound = false;
- String description = null;
- for(User user: users)
- {
- if(user.getNickName().equals(userName))
- {
- userFound = true;
- description = user.getDescription();
- }
- }
- return description;
- }
- /**
- * This method gets the directmessagedescription
- * @ param messageID is the unique ID of the directmessage that is requested.
- * @ return this is a string containing all the important information of 1 directmessage.
- */
- public String getDirectMessageDescription(long messageID)
- {
- return currentUser.getDirectMessageDescription(messageID);
- }
- /**
- * This method will return a tweetdescription.
- * @ param tweetID is the unique ID of the tweet
- * @ return this is a string containing all the important information of 1 tweet.
- */
- public String getTweetDescription(long tweetID)
- {
- String description= "The tweet could not be found.";
- for( Message message : tweetsAndRetweets)
- {
- if(message.getMessageID() == tweetID)
- {
- description = message.getMessageDescription();
- }
- }
- return description;
- }
- /**
- * This method will create a retweet, getting the original tweedID if the tweet you want to retweet is already a tweet, or returning the normal ID if you try to retweet is a tweet.
- * @param tweetID is the unique ID of the tweet you want to retweet
- * @return it returns a long, the unique tweetID of the
- */
- public long createRetweet(long tweetID)
- {
- String content = null;
- long referenceID = 0;
- String originalAuthor = null;
- boolean retweetSuccesfull = false;
- Date originalTimeOfCreation = null;
- for(Message message: tweetsAndRetweets)
- {
- if( message.getMessageID() == tweetID)
- {
- content = message.getMessageContent();
- uniqueID++;;
- originalAuthor = message.getOriginalAuthor();
- referenceID = message.getReferenceID();
- originalTimeOfCreation = message.getOriginalMomentOfCreation();
- retweetSuccesfull = true;
- }
- }
- if(retweetSuccesfull == true)
- {
- Retweet newRetweet = new Retweet((uniqueID-1), content, currentUser.getNickName(), referenceID, originalAuthor, originalTimeOfCreation);
- currentUser.incrementAmountOfTweets();
- tweetsAndRetweets.add(newRetweet);
- return uniqueID-1;
- }
- else
- {
- return 0;
- }
- }
- /**
- * This method will create a new tweet.
- * @param message is the string that will be displayed in the tweet, the method automatically gets the username from the currentuser.
- * @return this method returns the uniqueID of the created tweet
- */
- public long createNewTweet(String message)
- {
- if( message.length() <= maxTweetLength )
- {
- Tweet newTweet = new Tweet(uniqueID, message, currentUser.getNickName());
- tweetsAndRetweets.add(newTweet);
- uniqueID++;
- currentUser.incrementAmountOfTweets();
- return uniqueID-1;
- }
- else
- {
- return 0;
- }
- }
- // vanaf hier: alle methodes die aanroep doen op sorteeralgorithme en het sorteeralgorithme
- /**
- * This method give all the new directmessages in inversed chornological order, it does not need any parameters, but assumes that there is a user logged on.
- * @return This method will return a list with longs, the messageIDs of the directmessages.
- */
- public List<Long> getDirectMessages()
- {
- ArrayList<Message> sortingList = currentUser.getDirectMessages();
- int amountOfNewDirectMessages = sortingList.size();
- return sortTheList(sortingList, amountOfNewDirectMessages);
- }
- /**
- * This method will collect all the tweets 1 user created and will return them in an inverse choronological order. The amount of tweets is returned to a certain
- * amount of results.
- * @ param userName: This is the username of the user from whom we want to get the tweets.
- * @ param numberOfResults: This is the number of tweets that will finally be displayed.
- * @ return This returns a list with all the IDs of the tweets that will be printed.
- */
- public List<Long> getTweetsOfUser(String userName, int numberOfResults)
- {
- ArrayList<Message> listToBeSorted = new ArrayList<Message>();
- for(Message message : tweetsAndRetweets)
- {
- if(message.getCreator().equals(userName))
- {
- listToBeSorted.add(message);
- }
- }
- return sortTheList(listToBeSorted, numberOfResults);
- }
- /**
- * This method will search tweets that contain a certain searchstring
- * @param searchString: is the string that will be looked for in the tweets.
- * @param numberOfResults: is an integer that indicates how many searchresults maximum will be returned.
- * @return This method returns a list of uniqueIDs of the tweets that contain the searchstring, the length of the list is limited to the numberOfResults
- */
- public List<Long> searchTweets(String searchString, int numberOfResults)
- {
- ArrayList<Message> listToBeSorted = new ArrayList<Message>();
- for(Message message: tweetsAndRetweets)
- {
- if( message.getMessageContent().contains(searchString))
- {
- listToBeSorted.add(message);
- }
- }
- return sortTheList(listToBeSorted, numberOfResults);
- }
- /**
- * This method gets all tweets of the users you are following, sorts them invers chronological and then return a list of longs.
- * @ param numberOfResults: This parameter indicates how many results will be returned
- * @ return a list of unique IDs.
- */
- public List<Long> getTimeLine(int numberOfResults)
- {
- ArrayList<Message> tweetsToBeSorted = new ArrayList<>();
- List<String> friends = currentUser.getFriends();
- for(Message message: tweetsAndRetweets)
- {
- for(String name: friends)
- {
- if(name.equals(message.getCreator()))
- {
- tweetsToBeSorted.add(message);
- }
- }
- }
- return sortTheList(tweetsToBeSorted, numberOfResults);
- }
- /**
- * This method will sort any list it gets invers chronoligcally. It takes the list of tweets, calculates the timelapse, gives the 2 arraylists with timelapse and messageID's
- * to quicksort.
- * @ param listToBeSorted: this is a list of messages that will be sorted.
- * @ param numberOfResults: This integer determines how many results will be returned maximum.
- */
- private ArrayList<Long> sortTheList( ArrayList<Message> listToBeSorted, int numberOfResults)
- {
- Date momentOfRequest = new Date();
- ArrayList<Long> idList = new ArrayList<>();
- ArrayList<Long> timelapseList = new ArrayList<>();
- ArrayList<Long> sortedList = new ArrayList<>();
- int index = 0;
- for( Message message : listToBeSorted)
- {
- long timelapse = momentOfRequest.getTime() - message.getMomentOfCreation().getTime();
- idList.add(message.getMessageID());
- timelapseList.add(timelapse);
- }
- sortedList = quickSort(timelapseList, idList, 0, (timelapseList.size()-1));
- int listSize = sortedList.size();
- while(index < listSize)
- {
- if( index >= numberOfResults)
- {
- sortedList.remove(numberOfResults);
- }
- index++;
- }
- return sortedList;
- }
- // quicksort + 3way partitition
- /**
- * This method will sort the list it gets invers chronological. The list with timelapses gives the difference in time in milliseconds, so the smaller the timelapse, the
- * newer the tweet, and so it will be put first in the arraylist.
- * @param tijdslijst is a list containing the timelapses
- * @param idlijst is a list containing the id's of the tweets that must be sorted invers chronological
- * @param low is the integer that indicates from which point the list must be sorted
- * @param high is the integer that indicates untill which point the list must be sorted
- * @return This method returns the sorted list of id's.
- */
- private static ArrayList<Long> quickSort(ArrayList<Long> tijdlijst, ArrayList<Long> idlijst, int low, int high)
- {
- if (low < high)
- {
- int j = partition( tijdlijst, idlijst, low, high);
- quickSort(tijdlijst, idlijst, low, j-1);
- quickSort(tijdlijst, idlijst, j+1, high);
- }
- return idlijst;
- }
- /**
- * The partition method of quicksort, this method will in the arraylist compare elements and swap them accordingly, until the arraylist is divided into a pivot, a left part of the
- * arralist, containing smaller elements than the pivot, and a right part of the arraylist, containing elements bigger than the pivot.
- * @param tijdlijst is the arraylist with timestamps that will be sorted from small to big
- * @param idlijst is a list with ID's that will be swapped together with the timestamps, so the timestamps and id's from the 2 lists still belong together
- * @param low is the integer that indicates from which point the arraylist will be divided into a left, right part and a pivot.
- * @param high is the integer that indicates untill which point in the arraylist the elements will be compared and swapped.
- * @return this method returns an integer, i, that indicates at which point in the arraylist the pivot is at the moment, so the quicksort can recursif recall itself and sort
- * the arraylist from the beginning till 1 position under the pivot, and from 1 position above the pivot till the end of the list.
- */
- private static int partition(ArrayList<Long> tijdlijst, ArrayList<Long> idlijst, int low, int high)
- {
- Long x = tijdlijst.get(high);
- int i = low;
- for( int j = low; j <= high-1; j++)
- {
- if(tijdlijst.get(j) <= x)
- {
- Collections.swap(tijdlijst, i, j);
- Collections.swap(idlijst, i, j);
- i++;
- }
- }
- Collections.swap(tijdlijst, i, high);
- Collections.swap(idlijst, i, high);
- return i;
- }
- // extras
- /**
- * This method will add a favourite tweet to the favourites list of the current user
- * @param id is the tweetID of the tweet that must be added to the favourites
- * @return succesFull is the boolean that will be returned, it indicates whether the action was succesfull or not.
- */
- public boolean addFavourite(Long id)
- {
- boolean succesFull = false;
- for(Message message: tweetsAndRetweets)
- {
- if(message.getMessageID() == id)
- {
- succesFull = true;
- currentUser.addFavourite(message);
- }
- }
- return succesFull;
- }
- /**
- * This method will return a list of ID, containing the id's of the favourites of a user.
- * @param userName is the username of the user whose favourites we try to get.
- * @param numberOfResults is the amount of favourites we want to see from the user.
- * @return A list containing the tweetID's of the favourites of the user.
- */
- public List<Long> getFavouritesOfUser(String userName, int numberOfResults)
- {
- User wantedUser = null;
- for(User user: users)
- {
- if(user.getNickName().equals(userName))
- {
- wantedUser = user;
- }
- }
- return sortTheList(wantedUser.getFavourites(), numberOfResults);
- }
- }
Add Comment
Please, Sign In to add comment