KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > spi > retrieval > cummulative > CummulativeAnnotationsItem


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata.spi.retrieval.cummulative;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.jboss.metadata.spi.context.MetaDataContext;
30 import org.jboss.metadata.spi.retrieval.AnnotationItem;
31 import org.jboss.metadata.spi.retrieval.AnnotationsItem;
32 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
33 import org.jboss.metadata.spi.retrieval.simple.SimpleAnnotationsItem;
34
35 /**
36  * CummulativeAnnotationsItem.
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 57307 $
40  */

41 public class CummulativeAnnotationsItem extends SimpleAnnotationsItem
42 {
43    /** The context */
44    private MetaDataContext context;
45    
46    /** Whether to include the parent */
47    private boolean includeParent;
48    
49    /** The valid time */
50    private long validTime;
51    
52    /**
53     * Create a new CummulativeAnnotationsItem.
54     *
55     * @param context the context
56     * @param includeParent whether to include the parent
57     */

58    public CummulativeAnnotationsItem(MetaDataContext context, boolean includeParent)
59    {
60       if (context == null)
61          throw new IllegalArgumentException JavaDoc("Null context");
62
63       this.context = context;
64       this.includeParent = includeParent;
65       init(context.getValidTime().getValidTime());
66    }
67
68    public Annotation JavaDoc[] getValue()
69    {
70       checkValid();
71       return super.getValue();
72    }
73    
74    public AnnotationItem[] getAnnotations()
75    {
76       checkValid();
77       return super.getAnnotations();
78    }
79
80    public boolean isCachable()
81    {
82       return true;
83    }
84
85    public boolean isValid()
86    {
87       return true;
88    }
89
90    /**
91     * Check whether we are valid
92     */

93    protected void checkValid()
94    {
95       AnnotationItem[] items = super.getAnnotations();
96       boolean valid = (items != null);
97       
98       long newValidTime = context.getValidTime().getValidTime();
99       if (validTime < newValidTime)
100          valid = false;
101       
102       if (valid && items != null)
103       {
104          for (AnnotationItem item : items)
105          {
106             if (item.isValid() == false)
107                valid = false;
108          }
109       }
110       
111       if (valid == false)
112          init(newValidTime);
113    }
114
115    /**
116     * Initialise
117     *
118     * @param validTime the valid time
119     */

120    protected void init(long validTime)
121    {
122       Set JavaDoc<AnnotationItem> temp = null;
123
124       List JavaDoc<MetaDataRetrieval> retrievals;
125       
126       if (includeParent)
127          retrievals = context.getRetrievals();
128       else
129          retrievals = context.getLocalRetrievals();
130       
131       for (MetaDataRetrieval retrieval : retrievals)
132       {
133          AnnotationsItem item = retrieval.retrieveAnnotations();
134          if (item != null)
135          {
136             AnnotationItem[] items = item.getAnnotations();
137             for (AnnotationItem it : items)
138             {
139                if (temp == null)
140                   temp = new HashSet JavaDoc<AnnotationItem>();
141                temp.add(it);
142             }
143          }
144       }
145       
146       AnnotationItem[] items = NO_ANNOTATION_ITEMS;
147       if (temp != null)
148          items = temp.toArray(new AnnotationItem[temp.size()]);
149       setAnnotationItems(items);
150       this.validTime = validTime;
151    }
152 }
153
Popular Tags