001: /*
002: * $Id: Room.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.chat;
022:
023: import java.util.ArrayList;
024: import java.util.Date;
025: import java.util.LinkedHashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: public class Room {
030:
031: private static final int MAX_CHAT_MESSAGES = 10;
032:
033: private String name;
034: private String description;
035: private Date creationDate;
036:
037: private List<ChatMessage> messages = new ArrayList<ChatMessage>();
038:
039: private Map<String, User> members = new LinkedHashMap<String, User>();
040:
041: public Room(String name, String description) {
042: this .name = name;
043: this .description = description;
044: this .creationDate = new Date(System.currentTimeMillis());
045: }
046:
047: // properties
048: public Date getCreationDate() {
049: return creationDate;
050: }
051:
052: public String getDescription() {
053: return description;
054: }
055:
056: public String getName() {
057: return name;
058: }
059:
060: // (behaviour) members
061: public List<User> getMembers() {
062: return new ArrayList<User>(members.values());
063: }
064:
065: public User findMember(String name) {
066: assert (name != null);
067: return members.get(name);
068: }
069:
070: public boolean hasMember(String name) {
071: assert (name != null);
072: return members.containsKey(name);
073: }
074:
075: public void memberEnter(User member) {
076: assert (member != null);
077: if (!hasMember(member.getName())) {
078: members.put(member.getName(), member);
079: }
080: }
081:
082: public void memberExit(String memberName) {
083: assert (memberName != null);
084: assert (memberName.trim().length() > 0);
085: members.remove(memberName);
086: }
087:
088: // (behaviour) chat messags
089: public void addMessage(ChatMessage chatMessage) {
090: if (messages.size() > MAX_CHAT_MESSAGES) {
091: // messages.remove(messages.size() - 1);
092: messages.remove(0);
093: }
094: messages.add(chatMessage);
095: }
096:
097: public List<ChatMessage> getChatMessages() {
098: return new ArrayList<ChatMessage>(messages);
099: }
100:
101: }
|