source: java/main/src/main/java/com/framsticks/params/Registry.java @ 86

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

HIGHLIGHTS:

  • use java annotations to mark classes and fields to be used when:
    • using java classes with ReflectionAccess? to represent remote objects with FramsClass? description found by "info ..." requests
    • to build up FramsClass? representation of objects not present at remote server
  • allow using primitive types (instead of wraping counterparts) in reflected classes
  • rework FramsClass? creation process (add FramsClassBuilder?)
  • add more tests

CHANGELOG:
Prepare model.World class.

Minor change.

Use primitive types for Genotype and Creature classes.

Use primitive types in model.Neuro* classes.

Use primitive types in model.Joint* classes.

Use primitive types in model.Part* classes.

Fix primitive values.

Extract FramsClassBuilder?.

Add tests of Model classes.

More fixes.

Refactorize out ParamCandidate?.

Several fixes.

Fix all regressions after introducing annotations.

Use annotations throughout the project.

Add exception classes.

Improve creation of FramsClass?.

More changes.

Many changes regarding annotations.

Annotate classes in com.framsticks.model package.

Remove manual FramsClass? constructor.

Construct FramsClass? for Creature. Add test.

Add default values to the ParamAnnotation?.

Add ParamBuilderTest? and ParamAnnotation?.

Add FramsClassAnnotation?.

File size: 3.0 KB
Line 
1package com.framsticks.params;
2
3import org.apache.log4j.Logger;
4
5import com.framsticks.params.annotations.FramsClassAnnotation;
6
7import java.util.HashMap;
8import java.util.Map;
9
10/**
11 * Author: Piotr Śniegowski
12 */
13public class Registry {
14        private static final Logger log = Logger.getLogger(Registry.class.getName());
15
16        protected final Map<String, Class<?>> reflectedClasses = new HashMap<String, Class<?>>();
17        protected final Map<String, String> invertedReflectedClasses = new HashMap<String, String>();
18        protected final Map<String, FramsClass> infoCache = new HashMap<String, FramsClass>();
19
20        public final void registerReflectedClass(String name, String id, String className) {
21                try {
22                        registerReflectedClass(name, id, Class.forName(className));
23                } catch (ClassNotFoundException e) {
24                        log.fatal("class not found during registration: " + e);
25                }
26        }
27
28        public void registerReflectedClass(String name, String id,
29                        Class<?> reflectedClass) {
30                if (name != null) {
31                        reflectedClasses.put(name, reflectedClass);
32                        invertedReflectedClasses.put(reflectedClass.getCanonicalName(), name);
33                }
34                if (id != null) {
35                        reflectedClasses.put(id, reflectedClass);
36                }
37        }
38
39        public Registry register(Class<?> reflectedClass) {
40                FramsClassAnnotation a = reflectedClass.getAnnotation(FramsClassAnnotation.class);
41                if (a == null) {
42                        log.error("class is not annotated: " + reflectedClass);
43                        return this;
44                }
45
46                registerReflectedClass(FramsClassBuilder.getName(a, reflectedClass), FramsClassBuilder.getId(a, reflectedClass), reflectedClass);
47
48                return this;
49        }
50
51        public AccessInterface createAccess(String name, FramsClass framsClass) throws ConstructionException {
52                if (reflectedClasses.containsKey(name)) {
53                        return new ReflectionAccess(reflectedClasses.get(name), framsClass);
54                }
55                return new PropertiesAccess(framsClass);
56        }
57
58        public void putInfoIntoCache(FramsClass framsClass) {
59                if (infoCache.containsKey(framsClass.getId())) {
60                        log.info("already cached " + framsClass);
61                        return;
62                }
63                log.debug("caching info for " + framsClass);
64                infoCache.put(framsClass.getId(), framsClass);
65                infoCache.put(framsClass.getName(), framsClass);
66        }
67
68        public FramsClass getInfoFromCache(String id) {
69                if (id == null) {
70                        return null;
71                }
72                if (infoCache.containsKey(id)) {
73                        return infoCache.get(id);
74                }
75                if (invertedReflectedClasses.containsKey(id)) {
76                        return getInfoFromCache(invertedReflectedClasses.get(id));
77                }
78                return null;
79        }
80
81        public static AccessInterface wrapAccessWithListIfNeeded(CompositeParam param, AccessInterface access) {
82                if (access == null) {
83                        return null;
84                }
85                return param.prepareAccessInterface(access);
86        }
87
88        public AccessInterface prepareAccess(CompositeParam param) throws ConstructionException {
89                return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName()));
90        }
91
92        public AccessInterface createAccess(String name) throws ConstructionException {
93                if (name == null) {
94                        return null;
95                }
96                FramsClass framsClass = getInfoFromCache(name);
97                if (framsClass == null) {
98                        return null;
99                }
100
101                return createAccess(name, framsClass);
102        }
103
104}
Note: See TracBrowser for help on using the repository browser.