Back to project page android_lucidchat.
The source code is released under:
Apache License
If you think the Android project android_lucidchat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Copyright 2013 Harri Smatt// w w w . j a va 2 s .co m Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package fi.harism.lucidchat.api; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Vector; /** * Conversation holder class. */ public class Conversation { public static final int TYPE_CHANNEL = 1; public static final int TYPE_LOG = 2; public static final int TYPE_USER = 3; private Vector<User> mMembers = new Vector<User>(); private Vector<Message> mMessages = new Vector<Message>(); private Vector<Observer> mObservers = new Vector<Observer>(); private boolean mSortMembers; private int mType; private Object mWith; /** * Default constructor. Object with ought to be either Channel or User * depending on which type of conversation this is. */ public Conversation(int type, Object with) { mType = type; mWith = with; } /** * Add member to conversation. */ public void addMember(User user) { if (!mMembers.contains(user)) { mMembers.add(user); mSortMembers = true; for (Observer observer : mObservers) { observer.onConversationMemberAdded(this, user); } } } /** * Add new message to conversation. */ public void addMessage(Message message) { mMessages.add(message); for (Observer observer : mObservers) { observer.onConversationMessageAdded(this, message); } } /** * Add an observer for this conversation. */ public void addObserver(Observer observer) { if (!mObservers.contains(observer)) { mObservers.add(observer); } } /** * Getter for caption of conversation. In case of user conversation this is * user's, conversation happens with, real name, in channel case channel * topic. */ public String getCaption() { if (mWith instanceof User) { return ((User) mWith).getRealName(); } else if (mWith instanceof Channel) { return ((Channel) mWith).getTopic(); } return "___ERROR___"; } /** * Getter for conversation members. */ public List<User> getMembers() { if (mSortMembers) { Collections.sort(mMembers, new Comparator<User>() { @Override public int compare(User lhs, User rhs) { return lhs.getName().compareToIgnoreCase(rhs.getName()); } }); mSortMembers = false; } return mMembers; } /** * Getter for conversation messages. */ public List<Message> getMessages() { return mMessages; } /** * Getter for conversation title. In case of user conversation it's user's, * conversation happens with, nick name. In channel case channel's name. */ public String getTitle() { if (mWith instanceof User) { return ((User) mWith).getName(); } else if (mWith instanceof Channel) { return ((Channel) mWith).getName(); } return "___ERROR___"; } /** * Getter for conversation type. */ public int getType() { return mType; } /** * Protected method for retrieving object conversation happens with. */ protected Object getWith() { return mWith; } /** * Remove member from conversation based on user id. */ public void removeMember(String userId) { for (int i = mMembers.size() - 1; i >= 0; --i) { if (mMembers.get(i).getId().equals(userId)) { User user = mMembers.remove(i); for (Observer observer : mObservers) { observer.onConversationMemberRemoved(this, user); } } } } /** * Remove user object from member list. */ public void removeMember(User user) { if (mMembers.remove(user)) { for (Observer observer : mObservers) { observer.onConversationMemberRemoved(this, user); } } } /** * Remove observer from this conversation. */ public void removeObserver(Observer observer) { mObservers.remove(observer); } /** * Protected setter for changing with object this conversation happens with. */ protected void setWith(Object with) { mWith = with; for (Observer observer : mObservers) { observer.onConversationInfoChanged(this); } } /** * Update member information with new user object. Comparison happens with * user id. */ public void updateMember(User user) { if (mWith.equals(user)) { mWith = user; for (Observer observer : mObservers) { observer.onConversationInfoChanged(this); } } for (int i = 0; i < mMembers.size(); ++i) { if (mMembers.get(i).equals(user)) { mMembers.set(i, user); mSortMembers = true; for (Observer observer : mObservers) { observer.onConversationMemberUpdated(this, user); } } } } /** * Conversation observer interface. */ public interface Observer { /** * Sent once conversation title and/or caption changes. */ public void onConversationInfoChanged(Conversation conversation); /** * Sent once new member has been added to conversation. */ public void onConversationMemberAdded(Conversation conversation, User user); /** * Sent once conversation member has been removed. */ public void onConversationMemberRemoved(Conversation conversation, User user); /** * Sent once conversation member has been updated. */ public void onConversationMemberUpdated(Conversation conversation, User user); /** * Sent once new message has been added to conversation. */ public void onConversationMessageAdded(Conversation conversation, Message message); } }