001 package com.croftsoft.apps.chat.model.seri;
002
003 import java.util.*;
004
005 import com.croftsoft.core.animation.model.Model;
006 import com.croftsoft.core.animation.model.ModelId;
007 import com.croftsoft.core.animation.model.seri.SeriModelId;
008 import com.croftsoft.core.animation.model.seri.SeriWorld;
009 import com.croftsoft.core.lang.NullArgumentException;
010 import com.croftsoft.core.role.Server;
011 import com.croftsoft.core.util.consumer.Consumer;
012
013 import com.croftsoft.apps.chat.event.CreateModelEvent;
014 import com.croftsoft.apps.chat.event.RemoveModelEvent;
015 import com.croftsoft.apps.chat.model.ChatModel;
016 import com.croftsoft.apps.chat.model.ChatWorld;
017
018 /*********************************************************************
019 * A Serializable ChatWorld implementation.
020 *
021 * @version
022 * 2003-06-17
023 * @since
024 * 2003-06-05
025 * @author
026 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
027 *********************************************************************/
028
029 public final class SeriChatWorld
030 extends SeriWorld
031 implements ChatWorld
032 //////////////////////////////////////////////////////////////////////
033 //////////////////////////////////////////////////////////////////////
034 {
035
036 private static final long serialVersionUID = 0L;
037
038 //
039
040 private transient Consumer eventConsumer;
041
042 private transient Random random;
043
044 //////////////////////////////////////////////////////////////////////
045 //////////////////////////////////////////////////////////////////////
046
047 public SeriChatWorld ( Consumer eventConsumer )
048 //////////////////////////////////////////////////////////////////////
049 {
050 NullArgumentException.check ( this.eventConsumer = eventConsumer );
051
052 random = new Random ( );
053 }
054
055 //////////////////////////////////////////////////////////////////////
056 //////////////////////////////////////////////////////////////////////
057
058 public void addChatModel ( ChatModel chatModel )
059 //////////////////////////////////////////////////////////////////////
060 {
061 modelArrayKeeper.insert ( chatModel );
062
063 eventConsumer.consume ( new CreateModelEvent ( chatModel ) );
064 }
065
066 public ModelId createModel (
067 String avatarType,
068 double x,
069 double y )
070 //////////////////////////////////////////////////////////////////////
071 {
072 ModelId modelId = new SeriModelId ( random.nextLong ( ) );
073
074 addChatModel ( new SeriChatModel (
075 modelId, eventConsumer, this, avatarType, x, y ) );
076
077 return modelId;
078 }
079
080 public ChatModel getChatModel ( ModelId modelId )
081 //////////////////////////////////////////////////////////////////////
082 {
083 NullArgumentException.check ( modelId );
084
085 Model [ ] models = getModels ( );
086
087 for ( int i = 0; i < models.length; i++ )
088 {
089 Model model = models [ i ];
090
091 if ( model.getModelId ( ).equals ( modelId ) )
092 {
093 return ( ChatModel ) model;
094 }
095 }
096
097 return null;
098 }
099
100 public boolean removeModel ( ModelId modelId )
101 //////////////////////////////////////////////////////////////////////
102 {
103 NullArgumentException.check ( modelId );
104
105 ChatModel chatModel = getChatModel ( modelId );
106
107 if ( chatModel == null )
108 {
109 return false;
110 }
111
112 remove ( chatModel );
113
114 eventConsumer.consume ( new RemoveModelEvent ( modelId ) );
115
116 return true;
117 }
118
119 //////////////////////////////////////////////////////////////////////
120 //////////////////////////////////////////////////////////////////////
121 }