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

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

HIGHLIGHTS:

  • FramsClass? and contained Param are now immutable classes (like String),

which allows to refer to them concurrently without synchronization
(which for example in turn simplifies GUI management)

  • also make Path immutable (which was earlier only assumed)
  • add global cache for FramsClasses? created solely and automatically

on base of Java classes.

representations basing on given FramsClass?

  • above changes greatly improved GUI responsivness during browsing
  • furtherly improve Param class hierarchy
  • allow to inject actions on state changes into MultiParamLoader?
  • add more tests

CHANGELOG:

Add StatusListener? to MultiParamLoader?.

Minor refactorization in MultiParamLoader?.

First step with auto append.

Add SchemaTest?.

Improve Registry.

Clean up in Registry.

Work out Registry.

Use annotations for Param.

Fix ListChange?.

Improve fluent interface of the FramsClassBuilder?.

Done caching of ReflectionAccess?.Backend

Fix hashCode of Pair.

A step on a way to cache ReflectionAccess?.Backend

Make SimpleAbstractAccess?.framsClass a final field.

Add static cache for FramsClasses? based on java.

Only classes created strictly and automatically
based on java classes are using this cache.

Make all Params immutable.

Many improvement to make Param immutable.

Make PrimitiveParam? generic type.

Several changes to make Param immutable.

Make FramsClass? immutable.

Another improvement to Path immutability.

Several improvements to Path.

Improve PathTest?.

Configurarable MutabilityDetector?.

File size: 3.9 KB
Line 
1package com.framsticks.params;
2
3import org.apache.log4j.Logger;
4
5import com.framsticks.params.annotations.FramsClassAnnotation;
6
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.Map;
11import java.util.Set;
12
13/**
14 * Author: Piotr Śniegowski
15 */
16public class Registry {
17        private static final Logger log = Logger.getLogger(Registry.class.getName());
18
19        public static class DoubleSet<K1, K2, V> {
20                protected final Map<K1, V> byId = new HashMap<>();
21                protected final Map<K2, V> byName = new HashMap<>();
22                protected final Set<V> values = new HashSet<>();
23
24                public int size() {
25                        return values.size();
26                }
27
28                public void put(K1 id, K2 name, V value) {
29                        values.add(value);
30                        if (id != null) {
31                                byId.put(id, value);
32                        }
33                        if (name != null) {
34                                byName.put(name, value);
35                        }
36                }
37
38                public boolean containsValue(V value) {
39                        return values.contains(value);
40
41                }
42
43                public boolean containsKey(String identifier) {
44                        return byId.containsKey(identifier) || byName.containsKey(identifier);
45                }
46
47                public V get(String identifier) {
48                        if (byId.containsKey(identifier)) {
49                                return byId.get(identifier);
50                        }
51                        if (byName.containsKey(identifier)) {
52                                return byName.get(identifier);
53                        }
54                        return null;
55                }
56
57                public Set<V> getValues() {
58                        return Collections.unmodifiableSet(values);
59                }
60        }
61
62        // protected void internalRegisterClass(FramsClass framsClass, @Nullable Class<?> javaClass) {
63        //      assert framsClass.getName() != null;
64        //      assert framsClass.getId() != null;
65        //      infoCacheByName.put(framsClass.getName(), framsClass);
66        //      infoCacheById.put(framsClass.getId(), framsClass);
67
68        //      if (javaClass != null) {
69        //              reflectedClasses.put(framsClass, javaClass);
70        //              infoCacheByJavaName.put(javaClass.getCanonicalName(), framsClass);
71        //      }
72        // }
73        protected final DoubleSet<String, String, Class<?>> javaClasses = new DoubleSet<>();
74        protected final DoubleSet<String, String, FramsClass> framsClasses = new DoubleSet<>();
75
76        public void registerReflectedClass(String name, String id, Class<?> reflectedClass) {
77                javaClasses.put(id, name, reflectedClass);
78        }
79
80        public Registry register(Class<?> reflectedClass) {
81                FramsClassAnnotation a = reflectedClass.getAnnotation(FramsClassAnnotation.class);
82                if (a == null) {
83                        log.error("class is not annotated: " + reflectedClass);
84                        return this;
85                }
86
87                registerReflectedClass(FramsClassBuilder.getName(a, reflectedClass), FramsClassBuilder.getId(a, reflectedClass), reflectedClass);
88
89                return this;
90        }
91
92        public AccessInterface createAccess(String name, FramsClass framsClass) throws ConstructionException {
93                assert framsClasses.containsValue(framsClass);
94                if (javaClasses.containsKey(name)) {
95                        return new ReflectionAccess(javaClasses.get(name), framsClass);
96                }
97                return new PropertiesAccess(framsClass);
98        }
99
100        public void putInfoIntoCache(FramsClass framsClass) {
101                log.debug("caching info for " + framsClass);
102                framsClasses.put(framsClass.getId(), framsClass.getName(), framsClass);
103        }
104
105        public FramsClass getInfoFromCache(String identifier) {
106                if (identifier == null) {
107                        return null;
108                }
109                return framsClasses.get(identifier);
110        }
111
112        public static AccessInterface wrapAccessWithListIfNeeded(CompositeParam param, AccessInterface access) {
113                if (access == null) {
114                        return null;
115                }
116                return param.prepareAccessInterface(access);
117        }
118
119        public AccessInterface prepareAccess(CompositeParam param) throws ConstructionException {
120                return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName()));
121        }
122
123        public AccessInterface createAccess(String name) throws ConstructionException {
124                if (name == null) {
125                        return null;
126                }
127                FramsClass framsClass = getInfoFromCache(name);
128                if (framsClass == null) {
129                        return null;
130                }
131
132                return createAccess(name, framsClass);
133        }
134
135        public Set<Class<?>> getReflectedClasses() {
136                return javaClasses.getValues();
137        }
138
139        public Set<FramsClass> getInfoCache() {
140                return framsClasses.getValues();
141        }
142
143}
Note: See TracBrowser for help on using the repository browser.