View Javadoc

1   package cz.cuni.amis.pogamut.ut2004multi.communication.worldview.objects;
2   
3   import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdateResult;
4   import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdateResult.Result;
5   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObject;
6   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
7   import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
8   import cz.cuni.amis.pogamut.multi.communication.translator.event.ILocalWorldObjectUpdatedEvent;
9   import cz.cuni.amis.pogamut.multi.communication.worldview.object.ILocalWorldObject;
10  import cz.cuni.amis.utils.exception.PogamutException;
11  
12  public abstract class TestLocalViewableObject implements ILocalWorldObject, ILocalGBViewable{
13  
14  	protected WorldObjectId id;
15  	protected long simTime;
16  	
17  	public abstract String getLocalString();
18  	public abstract long getLocalLong();
19  	
20  	protected TestLocalViewableObject( WorldObjectId id, long simTime)
21  	{
22  		this.id = id;
23  		this.simTime = simTime;
24  	}
25  	
26  	public abstract TestLocalViewableObject clone();
27  	
28  	@Override
29  	public WorldObjectId getId() {
30  		return this.id;
31  	}
32  
33  	@Override
34  	public long getSimTime() {
35  		return this.simTime;
36  	}
37  
38  	@Override
39  	public Class getCompositeClass() {
40  		return TestCompositeViewableObject.class;
41  	}
42  	
43  	@Override
44  	public String toString()
45  	{
46  		return ("TestLocalObject["+id+"] : long="+getLocalLong()+" ; string=" + getLocalString() + " ;");
47  	}
48  	
49  	public TestLocalViewableObjectUpdatedEvent createUpdateEvent( long simTime )
50  	{
51  		return new TestLocalViewableObjectUpdatedEvent(this, simTime);
52  	}
53  	
54  	public static class TestLocalViewableObjectUpdatedEvent implements ILocalWorldObjectUpdatedEvent, IWorldObjectEvent<IWorldObject>
55  	{
56  		private TestLocalViewableObject data;
57  		private long simTime;
58  		
59  		public TestLocalViewableObjectUpdatedEvent(TestLocalViewableObject data, long simTime)
60  		{
61  			this.data = data;
62  			this.simTime = simTime;
63  		}
64  		
65  		@Override
66  		public long getSimTime() {
67  			return simTime;
68  		}
69  
70  		@Override
71  		public WorldObjectId getId() {
72  			return data.getId();
73  		}
74  		
75  		
76  
77  		@Override
78  		public IWorldObjectUpdateResult<ILocalWorldObject> update(
79  				ILocalWorldObject object) 
80  		{
81  			if ( object == null)
82  			{
83  				TestLocalViewableObject ret = new TestLocalViewableObjectImpl(data);
84  				data = ret;
85  				return new IWorldObjectUpdateResult.WorldObjectUpdateResult<ILocalWorldObject>( Result.CREATED, data);
86  			}
87  			if ( ! ( object instanceof TestLocalViewableObjectImpl) )
88  			{
89  				throw new PogamutException("Wrong object class provided for update, expected TestLocalObjectImpl", this);
90  			}
91  			else
92  			{
93  				TestLocalViewableObjectImpl toUpdate = (TestLocalViewableObjectImpl)object;
94  				boolean updated = false;
95  				if ( toUpdate.longVal != data.getLocalLong() )
96  				{
97  					toUpdate.longVal = data.getLocalLong();
98  					updated = true;
99  				}
100 				if ( !toUpdate.stringVal.equals( data.getLocalString() ) )
101 				{
102 					toUpdate.stringVal = data.getLocalString();
103 					updated = true;
104 				}
105 				if ( toUpdate.visible != data.isVisible())
106 				{
107 					toUpdate.visible = data.isVisible();
108 					updated = true;
109 					
110 				}
111 				toUpdate.simTime = this.simTime;
112 				if ( updated )
113 				{
114 					return new IWorldObjectUpdateResult.WorldObjectUpdateResult<ILocalWorldObject>(Result.UPDATED, toUpdate);
115 				}
116 				else
117 				{
118 					return new IWorldObjectUpdateResult.WorldObjectUpdateResult<ILocalWorldObject>(Result.SAME, toUpdate);
119 				}
120 			}
121 			
122 		}
123 
124 		@Override
125 		public IWorldObject getObject() {
126 			return data;
127 		}
128 		
129 	}
130 
131 	
132 }