1 | package com.framsticks.core; |
---|
2 | |
---|
3 | import javax.annotation.OverridingMethodsMustInvokeSuper; |
---|
4 | |
---|
5 | import com.framsticks.params.FramsClass; |
---|
6 | import com.framsticks.util.dispatching.Thread; |
---|
7 | import com.framsticks.util.dispatching.Dispatcher; |
---|
8 | |
---|
9 | import org.apache.commons.configuration.Configuration; |
---|
10 | import org.apache.log4j.Logger; |
---|
11 | import com.framsticks.util.dispatching.RunAt; |
---|
12 | |
---|
13 | /** |
---|
14 | * @author Piotr Sniegowski |
---|
15 | */ |
---|
16 | public abstract class Entity implements Dispatcher<Entity> { |
---|
17 | |
---|
18 | private final static Logger log = Logger.getLogger(Entity.class.getName()); |
---|
19 | |
---|
20 | protected String name = "entity"; |
---|
21 | protected EntityOwner owner; |
---|
22 | protected Dispatcher<Entity> dispatcher; |
---|
23 | |
---|
24 | public Entity() { |
---|
25 | } |
---|
26 | |
---|
27 | public final String getName() { |
---|
28 | return name; |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * @param name the name to set |
---|
33 | */ |
---|
34 | public final void setName(String name) { |
---|
35 | this.name = name; |
---|
36 | if (dispatcher instanceof Thread) { |
---|
37 | ((Thread<Entity>) dispatcher).setName(name); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * @return the owner |
---|
43 | */ |
---|
44 | public EntityOwner getOwner() { |
---|
45 | return owner; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * @param owner the owner to set |
---|
50 | */ |
---|
51 | public void setOwner(EntityOwner owner) { |
---|
52 | this.owner = owner; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public final boolean isActive() { |
---|
57 | return dispatcher == null || dispatcher.isActive(); |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public final void invokeLater(RunAt<? extends Entity> runnable) { |
---|
62 | assert dispatcher != null; |
---|
63 | dispatcher.invokeLater(runnable); |
---|
64 | } |
---|
65 | |
---|
66 | public Dispatcher<Entity> createDefaultDispatcher() { |
---|
67 | return new Thread<Entity>(name).start(); |
---|
68 | } |
---|
69 | |
---|
70 | @OverridingMethodsMustInvokeSuper |
---|
71 | protected void run() { |
---|
72 | assert isActive(); |
---|
73 | log.info("running: " + this); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | public void configure(Configuration config) { |
---|
78 | } |
---|
79 | |
---|
80 | public Dispatcher<Entity> getDispatcher() { |
---|
81 | return dispatcher; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * @param dispatcher the dispatcher to set |
---|
86 | */ |
---|
87 | public void setDispatcher(Dispatcher<Entity> dispatcher) { |
---|
88 | this.dispatcher = dispatcher; |
---|
89 | } |
---|
90 | |
---|
91 | public final void start() { |
---|
92 | if (dispatcher == null) { |
---|
93 | log.debug("no dispatcher set for " + this + ", creating default one"); |
---|
94 | setDispatcher(createDefaultDispatcher()); |
---|
95 | } |
---|
96 | invokeLater(new RunAt<Entity>() { |
---|
97 | @Override |
---|
98 | public void run() { |
---|
99 | Entity.this.run(); |
---|
100 | } |
---|
101 | }); |
---|
102 | } |
---|
103 | |
---|
104 | public final void done() { |
---|
105 | log.info("stopping entity"); |
---|
106 | if (owner != null) { |
---|
107 | owner.onDone(); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | public static void constructFramsClass(FramsClass.Constructor constructor) { |
---|
112 | constructor.method("getName"); |
---|
113 | } |
---|
114 | } |
---|