View Javadoc

1   package cz.cuni.amis.pogamut.base.agent.navigation.impl;
2   
3   import java.util.List;
4   import java.util.concurrent.TimeUnit;
5   
6   import cz.cuni.amis.pogamut.base.agent.navigation.IPathFuture;
7   import cz.cuni.amis.utils.SyncLazy;
8   import cz.cuni.amis.utils.future.FutureStatus;
9   import cz.cuni.amis.utils.future.IFutureListener;
10  import cz.cuni.amis.utils.listener.Listeners;
11  
12  /**
13   * Serves as a {@link IPathFuture} that contains pre-set result, i.e., you do not need the future, you just want to pass down
14   * some value...
15   * 
16   * @author Jimmy
17   *
18   * @param <PATH_ELEMENT>
19   */
20  public class PrecomputedPathFuture<PATH_ELEMENT> implements IPathFuture<PATH_ELEMENT> {
21  
22  	private PATH_ELEMENT pathFrom;
23  	private PATH_ELEMENT pathTo;
24  	private List<PATH_ELEMENT> result;
25  	private SyncLazy<Listeners<IFutureListener>> listeners = new SyncLazy<Listeners<IFutureListener>>() {
26  		@Override
27  		protected Listeners<IFutureListener> create() {
28  			return new Listeners<IFutureListener>();
29  		}
30  	};
31  
32  	public PrecomputedPathFuture(PATH_ELEMENT from, PATH_ELEMENT to, List<PATH_ELEMENT> path) {
33  		this.pathFrom = from;
34  		this.pathTo = to;
35  		this.result = path;
36  	}
37  
38  	@Override
39  	public PATH_ELEMENT getPathFrom() {
40  		return pathFrom;
41  	}
42  
43  	@Override
44  	public PATH_ELEMENT getPathTo() {
45  		return pathTo;
46  	}
47  	
48  	@Override
49  	public void addFutureListener(IFutureListener<List<PATH_ELEMENT>> listener) {
50  		listeners.get().addWeakListener(listener);
51  	}
52  	
53  	@Override
54  	public void removeFutureListener(IFutureListener<List<PATH_ELEMENT>> listener) {
55  		listeners.get().removeListener(listener);
56  	}
57  
58  	@Override
59  	public List<PATH_ELEMENT> get() {
60  		return result;
61  	}
62  
63  	@Override
64  	public List<PATH_ELEMENT> get(long timeout, TimeUnit unit) {
65  		return result;
66  	}
67  
68  	@Override
69  	public FutureStatus getStatus() {
70  		return FutureStatus.FUTURE_IS_READY;
71  	}
72  
73  	@Override
74  	public boolean isListening(IFutureListener<List<PATH_ELEMENT>> listener) {
75  		return listeners.get().isListening(listener);
76  	}
77  
78  	@Override
79  	public boolean cancel(boolean mayInterruptIfRunning) {
80  		return false;
81  	}
82  
83  	@Override
84  	public boolean isCancelled() {
85  		return false;
86  	}
87  
88  	@Override
89  	public boolean isDone() {
90  		return true;
91  	}
92  
93  }