root / trunk / JavaChecker / jsrc / ua / gradsoft / javachecker / models / JavaClassFormalParameterModel.java @ 15107

1
/*
2
 * JavaClassFormalParameterModel.java
3
 *
4
 */
5
6
package ua.gradsoft.javachecker.models;
7
8
import java.lang.annotation.Annotation;
9
import java.lang.annotation.ElementType;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.TreeMap;
13
import ua.gradsoft.javachecker.util.CachedMap;
14
import ua.gradsoft.javachecker.util.Function;
15
import ua.gradsoft.javachecker.util.FunctionMap;
16
import ua.gradsoft.javachecker.util.ImmutableMappedCollection;
17
import ua.gradsoft.javachecker.util.ImmutableMappedList;
18
import ua.gradsoft.javachecker.util.IntegerOrderList;
19
20
/**
21
 *
22
 * @author rssh
23
 */
24
public class JavaClassFormalParameterModel extends JavaFormalParameterModel
25
{
26
    
27
    /** Creates a new instance of JavaClassFormalParameterModel */
28
    public JavaClassFormalParameterModel(
29
            String name,
30
            Annotation[] annotations,
31
            int          modifiers,            
32
            JavaTypeModel type,
33
            JavaClassTopLevelBlockOwnerModel owner,
34
            int index
35
            ) {
36
        name_=name;       
37
        annotations_=annotations;
38
        type_=type;
39
        owner_=owner;
40
        index_=index;
41
        modifiers_=new JavaClassModifiersModel(getAnnotationsList(),modifiers);
42
    }
43
    
44
    public JavaModifiersModel  getModifiers()
45
    { return modifiers_; }
46
    
47
    public String getName()
48
    { return name_; }
49
    
50
    public JavaTypeModel getType()
51
    { return type_; }
52
    
53
    public JavaTopLevelBlockOwnerModel getTopLevelBlockOwner()
54
    { return owner_; }
55
    
56
    public Map<String,JavaAnnotationInstanceModel> getAnnotationsMap()
57
    {
58
        final int size=owner_.getParameterAnnotations()[index_].length;
59
        if (annotationsCache_==null) {
60
           annotationsCache_=new TreeMap<String,JavaAnnotationInstanceModel>();
61
        }
62
        return new CachedMap<String,JavaAnnotationInstanceModel>(
63
                annotationsCache_,
64
                new FunctionMap<String,JavaAnnotationInstanceModel>(
65
                  new ImmutableMappedCollection<Integer,String>(
66
                    new IntegerOrderList(size),
67
                    new Function<Integer,String>(){
68
                      public String function(Integer x){
69
                         return owner_.getParameterAnnotations()[index_][x].annotationType().getName();
70
                      }
71
                    }
72
                  ),
73
                  new Function<String,JavaAnnotationInstanceModel>(){
74
                    public JavaAnnotationInstanceModel function(String s)  {
75
                      for(int i=0; i<owner_.getParameterAnnotations()[index_].length; ++i) {
76
                          if (owner_.getParameterAnnotations()[index_][i].annotationType().getName().equals(s)){
77
                             return new JavaClassAnnotationInstanceModel(ElementType.PARAMETER,owner_.getParameterAnnotations()[index_][i],JavaClassFormalParameterModel.this);
78
                          }
79
                      }
80
                      return null;
81
                    }
82
                  }
83
                )
84
              );            
85
    }
86
    
87
    
88
    public List<JavaAnnotationInstanceModel> getAnnotationsList()
89
    {
90
        return new ImmutableMappedList<Integer,JavaAnnotationInstanceModel>(
91
                new IntegerOrderList(annotations_.length),
92
                new Function<Integer,JavaAnnotationInstanceModel>(){
93
            public JavaAnnotationInstanceModel function(Integer i)
94
            {
95
                return new JavaClassAnnotationInstanceModel(ElementType.PARAMETER,annotations_[i],JavaClassFormalParameterModel.this);
96
            }
97
        }
98
                );                           
99
    }
100
101
    public boolean isConstant()
102
    { return false; }
103
    
104
    public int getIndex()
105
    { return index_; }
106
    
107
    private TreeMap<String,JavaAnnotationInstanceModel> annotationsCache_=null;
108
    
109
    private String name_;
110
    private JavaModifiersModel modifiers_; 
111
    private JavaTypeModel      type_;
112
    private JavaClassTopLevelBlockOwnerModel owner_;
113
    private Annotation[]  annotations_;
114
    int     index_;
115
    
116
}