View Javadoc

1   /***
2    * Copyright 2006 Joseph M. Ferner
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *    
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fernsroth.easyio;
17  
18  import java.lang.annotation.Annotation;
19  import java.lang.reflect.Field;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import com.fernsroth.easyio.field.FieldINT64;
24  import com.fernsroth.easyio.field.FieldUINT16;
25  import com.fernsroth.easyio.field.FieldUINT32;
26  import com.fernsroth.easyio.field.FieldUINT64;
27  import com.fernsroth.easyio.field.FieldUINT8;
28  import com.fernsroth.easyio.field.handler.FieldINT64Handler;
29  import com.fernsroth.easyio.field.handler.FieldUINT16Handler;
30  import com.fernsroth.easyio.field.handler.FieldUINT32Handler;
31  import com.fernsroth.easyio.field.handler.FieldUINT64Handler;
32  import com.fernsroth.easyio.field.handler.FieldUINT8Handler;
33  
34  /***
35   * 
36   * @author Joseph M. Ferner (Near Infinity Corporation)
37   */
38  public class DefaultFieldHandlerRegistry implements FieldHandlerRegistry {
39  
40      /***
41       * the instance.
42       */
43      private static DefaultFieldHandlerRegistry instance;
44  
45      /***
46       * the registry.
47       */
48      private Map<Class<? extends Annotation>, FieldHandler> registry = new HashMap<Class<? extends Annotation>, FieldHandler>();
49  
50      /***
51       * gets an instance of the this class.
52       * @return the instance.
53       */
54      public static FieldHandlerRegistry getInstance() {
55          if (instance == null) {
56              instance = new DefaultFieldHandlerRegistry();
57              instance.register(FieldUINT8.class, new FieldUINT8Handler());
58              instance.register(FieldUINT16.class, new FieldUINT16Handler());
59              instance.register(FieldUINT32.class, new FieldUINT32Handler());
60              instance.register(FieldINT64.class, new FieldINT64Handler());
61              instance.register(FieldUINT64.class, new FieldUINT64Handler());
62          }
63          return instance;
64      }
65  
66      /***
67       * @param an the annotation to register.
68       * @param handler the handler.
69       */
70      public void register(Class<? extends Annotation> an, FieldHandler handler) {
71          this.registry.put(an, handler);
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      public FieldHandler lookup(Object obj, Field field) {
78          for (Annotation an : field.getDeclaredAnnotations()) {
79              Class<? extends Annotation> anType = an.annotationType();
80              FieldHandler handler = this.registry.get(anType);
81              if (handler != null) {
82                  return handler;
83              }
84          }
85  
86          for (Annotation an : field.getAnnotations()) {
87              Class<? extends Annotation> anType = an.annotationType();
88              FieldHandler handler = this.registry.get(anType);
89              if (handler != null) {
90                  return handler;
91              }
92          }
93          return null;
94      }
95  }