View Javadoc

1   package nl.tudelft.goal.unreal.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  	public boolean isLocation() {
19  		return location != null;
20  	}
21  	
22  	public boolean isUnrealId(){
23  		return id != null;
24  	}
25  	
26  	public Location getLocation(){
27  		return location;
28  	}
29  
30  	private final UnrealId id;
31  	private final Location location;
32  
33  	public UnrealIdOrLocation(Location location) {
34  		super();
35  		NullCheck.check(location, "location");
36  		this.location = location;
37  		this.id = null;
38  	}
39  
40  	public UnrealIdOrLocation(UnrealId id) {
41  		super();
42  		NullCheck.check(id, "id");
43  		this.location = null;
44  		this.id = id;
45  	}
46  
47  	public UnrealId getId() {
48  		return id;
49  	}
50  
51  	/**
52  	 * Transforms an UnrealIdOrLocation to an ILocated. When this is a location
53  	 * it directly returned. Otherwise the UnrealId Is resolved against the bots
54  	 * own Id and those of objects in the world view.
55  	 * 
56  	 * When the UnrealId can not be resolved this method returns null.
57  	 * 
58  	 * @param world
59  	 * @param info
60  	 * @return
61  	 */
62  	public ILocated toILocated(IWorldView world, AgentInfo info) {
63  
64  		if (isLocation()) {
65  			return location;
66  		}
67  
68  		// ID could refer to this bot.
69  		if (id.equals(info.getId())) {
70  			return info;
71  		}
72  
73  		return toILocated(world);
74  	}
75  
76  	/**
77  	 * Transforms an UnrealIdOrLocation to an ILocated. When this is a location
78  	 * it directly returned. Otherwise the UnrealId Is resolved against objects
79  	 * in the world view.
80  	 * 
81  	 * When the UnrealId can not be resolved this method returns null.
82  	 * 
83  	 * @param world
84  	 * @param info
85  	 * @return
86  	 */
87  
88  	public ILocated toILocated(IWorldView world) {
89  		// Try to resolve id to an ILocated
90  		IWorldObject object = world.get(id);
91  
92  		if (object instanceof ILocated) {
93  			return (ILocated) object;
94  		}
95  
96  		// Not found
97  		return null;
98  	}
99  
100 }