Permalink
Browse files

Refactor, cleanup and optimization of UserCollection

  • Loading branch information...
1 parent bfe1c01 commit 47c6d95813aa3d8fe1b8b4dd042213bb2f3df402 @mollerse mollerse committed Jul 8, 2012
@@ -14,12 +14,12 @@
* Declare in order of rank, this way values() will naturally
* return the different modes based on rank
*/
- OWNER ("Owner","q", "(s)", R.drawable.irc_operator),
- ADMIN ("Admin","a", "(s)", R.drawable.irc_operator),
- OPERATOR ("Operator","o", "(s)", R.drawable.irc_operator),
- HALF_OPERATOR ("Half-Op","h", "(s)", R.drawable.irc_voice),
+ OWNER ("Owner","q", "s", R.drawable.irc_operator),
+ ADMIN ("Admin","a", "s", R.drawable.irc_operator),
+ OPERATOR ("Operator","o", "s", R.drawable.irc_operator),
+ HALF_OPERATOR ("Half-Op","h", "s", R.drawable.irc_voice),
VOICE ("Voiced","v", "", R.drawable.irc_voice),
- USER ("User","", "(s)", R.drawable.im_user); //This will work as a catch-all for unknown modes
+ USER ("User","", "s", R.drawable.im_user); //This will work as a catch-all for unknown modes
public final String modeName;
public final String shortModeName;
@@ -171,7 +171,7 @@ public void onUserQuit(String nick) {
if(user.nick.equals(nick)) {
for(Buffer buffer : buffers.getRawBufferList()) {
if(user.channels.contains(buffer.getInfo().name)) {
- buffer.getUsers().removeUserWithNick(nick);
+ buffer.getUsers().removeUserByNick(nick);
}
}
userList.remove(user);
@@ -191,7 +191,7 @@ public void onUserParted(String nick, String bufferName) {
}
for(Buffer buffer : buffers.getRawBufferList()) {
if(buffer.getInfo().name.equalsIgnoreCase(bufferName)) {
- buffer.getUsers().removeUserWithNick(nick);
+ buffer.getUsers().removeUserByNick(nick);
if(nick.equalsIgnoreCase(getNick())) {
buffer.setActive(false);
}
@@ -20,102 +20,187 @@ public UserCollection() {
}
}
- public void removeUserWithNick(String nick) {
+ public void addUser(IrcUser user, String modes) {
+ for(IrcMode mode: IrcMode.values()){
+ if(modes.contains(mode.shortModeName)){
+ try{
+ addUserToModeList(mode, user);
+ //Log.e(TAG, "Mode "+mode.modeName+" added to user "+user.nick+".");
+ } catch (IllegalArgumentException e){
+ Log.e(TAG, e.getMessage());
+ }
+ }
+ }
+ updateUniqueUsersSortedByMode();
+ notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
+ user.addObserver(this);
+
+ }
+
+ public void addUsers(ArrayList<Pair<IrcUser, String>> usersWithModes) {
+ for(Pair<IrcUser, String> user: usersWithModes) {
+ for(IrcMode mode: IrcMode.values()){
+ if(user.second.contains(mode.shortModeName)){
+ try{
+ addUserToModeList(mode, user.first);
+ //Log.e(TAG, "Mode "+mode.modeName+" added to user "+user.first.nick+".");
+ } catch (IllegalArgumentException e){
+ //Log.e(TAG, e.getMessage());
+ }
+ }
+ }
+ user.first.addObserver(this);
+ }
+ updateUniqueUsersSortedByMode();
+ notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
+ }
+
+ private void addUserToModeList(IrcMode mode, IrcUser user) {
+ if(users.get(mode).contains(user)){
+ throw new IllegalArgumentException("User "+user.nick+" is already in this list.");
+ }else{
+ users.get(mode).add(user);
+ Collections.sort(users.get(mode));
+ this.setChanged();
+ }
+ }
+
+ public void removeUser(IrcUser user) {
for(IrcMode mode: IrcMode.values()){
try{
- removeUserWithNickFromModeList(mode, nick);
+ removeUserFromModeList(mode, user);
//Log.e(TAG, "Mode "+mode.modeName+" was removed from user "+nick+".");
} catch(IllegalArgumentException e) {
//Log.e(TAG, e.getMessage());
}
}
- findUniqueUsersSortedByMode();
notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
- }
+ }
- private void removeUserWithNickFromModeList(IrcMode mode, String nick) {
- boolean found = false;
- IrcUser userToRemove = null;
- for(IrcUser user : users.get(mode)) {
- if(user.nick.equals(nick) && users.get(mode).contains(user)) {
- found = true;
- userToRemove = user;
- break;
+ public void removeUsers(ArrayList<IrcUser> users) {
+ for(IrcUser user: users){
+ for(IrcMode mode: IrcMode.values()){
+ try{
+ removeUserFromModeList(mode, user);
+ //Log.e(TAG, "Mode "+mode.modeName+" was removed from user "+nick+".");
+ } catch(IllegalArgumentException e) {
+ //Log.e(TAG, e.getMessage());
+ }
}
}
- if(found) {
- users.get(mode).remove(userToRemove);
- uniqueUsers.get(mode).remove(userToRemove);
- this.setChanged();
+ notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
+ }
+
+ public void removeUserByNick(String nick) {
+ for(IrcMode mode: IrcMode.values()){
+ try{
+ for(IrcUser user: users.get(mode)){
+ if(user.nick.equals(nick)){
+ removeUserFromModeList(mode, user);
+ //Log.e(TAG, "Mode "+mode.modeName+" was removed from user "+nick+".");
+ break;
+ }
+ }
+ } catch(IllegalArgumentException e) {
+ //Log.e(TAG, e.getMessage());
+ }
}
+ notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
}
- private void removeUserFromModeList(List<IrcUser> list, IrcUser user){
- if(list.remove(user)){
+ public void removeUsersByNick(ArrayList<String> nicks) {
+ for(String nick: nicks){
+ for(IrcMode mode: IrcMode.values()){
+ try{
+ for(IrcUser user: users.get(mode)){
+ if(user.nick.equals(nick)){
+ removeUserFromModeList(mode, user);
+ //Log.e(TAG, "Mode "+mode.modeName+" was removed from user "+nick+".");
+ break;
+ }
+ }
+ } catch(IllegalArgumentException e) {
+ //Log.e(TAG, e.getMessage());
+ }
+ }
+ }
+ notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
+
+ }
+
+ private void removeUserFromModeList(IrcMode mode, IrcUser user){
+ if(users.get(mode).remove(user)){
+ uniqueUsers.get(mode).remove(user);
this.setChanged();
}else{
throw new IllegalArgumentException("User "+user.nick+" was not found.");
}
}
- public void addUser(IrcUser user, String modes) {
- for(IrcMode mode: IrcMode.values()){
- if(modes.contains(mode.shortModeName)){
+
+ public void addModeToUser(IrcUser user, String mode) {
+ for(IrcMode ircMode: IrcMode.values()){
+ if(mode.equals(ircMode.shortModeName)){
try{
- addUserToModeList(users.get(mode), user);
- //Log.e(TAG, "Mode "+mode.modeName+" added to user "+user.nick+".");
+ addUserToModeList(ircMode, user);
+ //Log.e(TAG, "Mode " + ircMode.modeName + " added to user " + user.nick);
+ break;
} catch (IllegalArgumentException e){
- Log.e(TAG, e.getMessage());
+ //Log.e(TAG, e.getMessage());
}
}
}
- findUniqueUsersSortedByMode();
+ updateUniqueUsersSortedByMode();
notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
- user.addObserver(this);
-
}
- public void addUsers(ArrayList<Pair<IrcUser, String>> usersToAdd) {
- for(Pair<IrcUser, String> user: usersToAdd) {
- for(IrcMode mode: IrcMode.values()){
- if(user.second.contains(mode.shortModeName)){
- try{
- addUserToModeList(users.get(mode), user.first);
- //Log.e(TAG, "Mode "+mode.modeName+" added to user "+user.first.nick+".");
- } catch (IllegalArgumentException e){
- Log.e(TAG, e.getMessage());
- }
+
+ public void removeModeFromUser(IrcUser user, String mode) {
+ if(mode.equals("")){
+ throw new IllegalArgumentException("Cannot remove empty mode from user.");
+ }
+ for(IrcMode ircMode: IrcMode.values()){
+ if(mode.equals(ircMode.shortModeName)){
+ try{
+ removeUserFromModeList(ircMode,user);
+ //Log.e(TAG, "Mode " + ircMode.modeName + " removed from user " + user.nick+".");
+ break;
+
+ } catch (IllegalArgumentException e){
+ //Log.e(TAG, e.getMessage());
}
}
- user.first.addObserver(this);
}
- findUniqueUsersSortedByMode();
+ updateUniqueUsersSortedByMode();
notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
}
- private void addUserToModeList(List<IrcUser> list, IrcUser user) {
- if(list.contains(user)){
- throw new IllegalArgumentException("User "+user.nick+" is already in this list.");
- }else{
- list.add(user);
- Collections.sort(list);
- this.setChanged();
+ public ArrayList<IrcUser> getUniqueUsers(){
+ /*
+ * Because IrcMode.values() starts at the first declaration and moves down,
+ * we can be sure that users get added to the list with the highest ranking mode first.
+ */
+ ArrayList<IrcUser> uniqueUsers = new ArrayList<IrcUser>();
+ for(IrcMode mode: IrcMode.values()){
+ for(IrcUser user: users.get(mode)){
+ if(!uniqueUsers.contains(user)){
+ uniqueUsers.add(user);
+ }
+ }
}
+ return uniqueUsers;
}
- public Map<IrcMode,ArrayList<IrcUser>> getUsers() {
- return users;
- }
-
- public ArrayList<IrcUser> getUsersWithMode(IrcMode mode){
- return users.get(mode);
+ public ArrayList<IrcUser> getUniqueUsersWithMode(IrcMode mode) {
+ return uniqueUsers.get(mode);
}
- private void findUniqueUsersSortedByMode(){
+ private void updateUniqueUsersSortedByMode(){
/*
* Because IrcMode.values() starts at the first declaration and moves down,
* we can be sure that users get added to the list with the highest ranking mode first.
*/
for(IrcMode mode: IrcMode.values()){
for(IrcUser user: users.get(mode)){
+ //Log.e(TAG, "Checking user "+user.nick+" for mode "+mode.modeName+".");
if(!isIrcUserAlreadyAddedWithAHigherRankingMode(mode, user)){
//Log.e(TAG, "Adding unique user "+user.nick+" with mode "+mode.modeName+".");
uniqueUsers.get(mode).add(user);
@@ -133,9 +218,13 @@ private boolean isIrcUserAlreadyAddedWithAHigherRankingMode(IrcMode currentMode,
found = true;
break;
}
+ if(mode==currentMode){
+ break;
+ }
}
return found;
}
+
private void removeUserFromLowerRankingMode(IrcMode hasMode, IrcUser user) {
boolean lowerRank = false;
for(IrcMode mode: IrcMode.values()) {
@@ -148,78 +237,16 @@ private void removeUserFromLowerRankingMode(IrcMode hasMode, IrcUser user) {
}
}
- public ArrayList<IrcUser> getUniqueUsers(){
- /*
- * Because IrcMode.values() starts at the first declaration and moves down,
- * we can be sure that users get added to the list with the highest ranking mode first.
- */
- ArrayList<IrcUser> uniqueUsers = new ArrayList<IrcUser>();
- for(IrcMode mode: IrcMode.values()){
- for(IrcUser user: users.get(mode)){
- if(!uniqueUsers.contains(user)){
- uniqueUsers.add(user);
- }
- }
- }
- return uniqueUsers;
- }
-
- public ArrayList<IrcUser> getUniqueUsersWithMode(IrcMode mode) {
- return uniqueUsers.get(mode);
- }
-
- public int getUserCount() {
- //All users have the mode IrcMode.USER
- return users.get(IrcMode.USER).size();
- }
-
@Override
- public void update(Observable observable, Object data) {
+ public void update(Observable observable, Object data) {
for(IrcMode mode: IrcMode.values()){
if(users.get(mode).contains(observable)){
Collections.sort(users.get(mode));
+ if(uniqueUsers.get(mode).contains(observable))Collections.sort(uniqueUsers.get(mode));
this.setChanged();
}
}
- findUniqueUsersSortedByMode();
notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
- }
-
- public void addUserMode(IrcUser user, String mode) {
- for(IrcMode ircMode: IrcMode.values()){
- if(mode.equals(ircMode.shortModeName)){
- try{
- addUserToModeList(users.get(ircMode), user);
- //Log.e(TAG, "Mode " + ircMode.modeName + " added to user " + user.nick);
- break;
- } catch (IllegalArgumentException e){
- Log.e(TAG, e.getMessage());
- }
- }
- }
- findUniqueUsersSortedByMode();
- notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
- }
-
- public void removeModeFromUser(IrcUser user, String mode) {
- if(mode.equals("")){
- throw new IllegalArgumentException("Cannot remove empty mode from user.");
- }
- for(IrcMode ircMode: IrcMode.values()){
- if(mode.equals(ircMode.shortModeName)){
- try{
- removeUserFromModeList(users.get(ircMode),user);
- uniqueUsers.get(ircMode).remove(user);
- //Log.e(TAG, "Mode " + ircMode.modeName + " removed from user " + user.nick+".");
- break;
-
- } catch (IllegalArgumentException e){
- //Log.e(TAG, e.getMessage());
- }
- }
- }
- findUniqueUsersSortedByMode();
- notifyObservers(R.id.BUFFERUPDATE_USERSCHANGED);
- }
+ }
}
@@ -794,7 +794,7 @@ public void handleMessage(Message msg) {
user = networks.getNetworkById(msg.arg1).getUserByNick(bundle.getString("nick"));
for(Buffer buf : networks.getNetworkById(msg.arg1).getBuffers().getRawBufferList()) {
if(buf.getInfo().name.equals(bufferName)) {
- buf.getUsers().addUserMode(user, bundle.getString("mode"));
+ buf.getUsers().addModeToUser(user, bundle.getString("mode"));
break;
}
}

0 comments on commit 47c6d95

Please sign in to comment.