View Javadoc

1   package nl.tudelft.goal.ut2004.messages;
2   
3   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
4   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObject;
5   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
6   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
7   import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
8   import cz.cuni.amis.pogamut.ut2004.agent.module.sensor.AgentInfo;
9   import cz.cuni.amis.utils.NullCheck;
10  
11  public class UnrealIdOrLocation {
12  
13  	@Override
14  	public String toString() {
15  		return isLocation() ? location.toString() : id.toString();
16  	}
17  
18  	private boolean isLocation() {
19  		return location != null;
20  	}
21  
22  	private final UnrealId id;
23  	private final Location location;
24  
25  	public UnrealIdOrLocation(Location location) {
26  		super();
27  		NullCheck.check(location, "location");
28  		this.location = location;
29  		this.id = null;
30  	}
31  
32  	public UnrealIdOrLocation(UnrealId id) {
33  		super();
34  		NullCheck.check(id, "id");
35  		this.location = null;
36  		this.id = id;
37  	}
38  
39  	public UnrealId getId() {
40  		return id;
41  	}
42  
43  	/**
44  	 * Transforms an UnrealIdOrLocation to an ILocated. When this is a location
45  	 * it directly returned. Otherwise the UnrealId Is resolved against the bots
46  	 * own Id and those of objects in the world view.
47  	 * 
48  	 * When the UnrealId can not be resolved this method returns null.
49  	 * 
50  	 * @param world
51  	 * @param info
52  	 * @return
53  	 */
54  	public ILocated toILocated(IWorldView world, AgentInfo info) {
55  
56  		if (isLocation()) {
57  			return location;
58  		}
59  
60  		// ID could refer to this bot.
61  		if (id.equals(info.getId())) {
62  			return info;
63  		}
64  
65  		return toILocated(world);
66  	}
67  
68  	/**
69  	 * Transforms an UnrealIdOrLocation to an ILocated. When this is a location
70  	 * it directly returned. Otherwise the UnrealId Is resolved against objects
71  	 * in the world view.
72  	 * 
73  	 * When the UnrealId can not be resolved this method returns null.
74  	 * 
75  	 * @param world
76  	 * @param info
77  	 * @return
78  	 */
79  
80  	public ILocated toILocated(IWorldView world) {
81  		// Try to resolve id to an ILocated
82  		IWorldObject object = world.get(id);
83  
84  		if (object instanceof ILocated) {
85  			return (ILocated) object;
86  		}
87  
88  		// Not found
89  		return null;
90  	}
91  
92  }