source: java/main/src/main/java/com/framsticks/core/Entity.java @ 85

Last change on this file since 85 was 85, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 2.3 KB
Line 
1package com.framsticks.core;
2
3import javax.annotation.OverridingMethodsMustInvokeSuper;
4
5import com.framsticks.params.FramsClass;
6import com.framsticks.util.dispatching.Thread;
7import com.framsticks.util.dispatching.Dispatcher;
8
9import org.apache.commons.configuration.Configuration;
10import org.apache.log4j.Logger;
11import com.framsticks.util.dispatching.RunAt;
12
13/**
14 * @author Piotr Sniegowski
15 */
16public 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}
Note: See TracBrowser for help on using the repository browser.