KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > proxy > container > ContainerCacheUtil


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, 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.aop.proxy.container;
23
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Comparator JavaDoc;
27
28 /**
29  *
30  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
31  * @version $Revision: 44750 $
32  */

33 public class ContainerCacheUtil
34 {
35
36    /**
37     * Takes a Class[] containing interface classes, and returns an array of weak references to thos class
38     * objects, sorted alphabetically by name.
39     */

40    public static WeakReference JavaDoc[] getSortedWeakReferenceForInterfaces(Class JavaDoc[] ifaces)
41    {
42       if (ifaces == null)
43       {
44          return null;
45       }
46       
47       WeakReference JavaDoc[] interfaces = new WeakReference JavaDoc[ifaces.length];
48       
49       for (int i = 0 ; i < ifaces.length ; i++)
50       {
51          interfaces[i] = new WeakReference JavaDoc(ifaces[i]);
52       }
53       
54       Arrays.sort(interfaces, Alphabetical.singleton);
55       return interfaces;
56    }
57
58    public static boolean compareClassRefs(WeakReference JavaDoc my, WeakReference JavaDoc other)
59    {
60       Class JavaDoc myClass = (Class JavaDoc)my.get();
61       Class JavaDoc otherClass = (Class JavaDoc)other.get();
62       
63       if (myClass == null || otherClass == null)
64       {
65          return false;
66       }
67       
68       if (!myClass.equals(otherClass))
69       {
70          return false;
71       }
72       return true;
73    }
74    
75    public static boolean compareInterfaceRefs(WeakReference JavaDoc[] my, WeakReference JavaDoc[] other)
76    {
77       if ((my == null && other != null) ||
78             (my == null && other != null))
79       {
80          return false;
81       }
82       
83       if (my != null && my != null)
84       {
85          if (my.length != other.length)
86          {
87             return false;
88          }
89          
90          for (int i = 0 ; i < my.length ; i++)
91          {
92             Class JavaDoc myIf = (Class JavaDoc)my[i].get();
93             Class JavaDoc otherIf = (Class JavaDoc)other[i].get();
94             
95             if (!myIf.equals(otherIf))
96             {
97                return false;
98             }
99          }
100       }
101       
102       return true;
103    }
104
105    static class Alphabetical implements Comparator JavaDoc
106    {
107       static Alphabetical singleton = new Alphabetical();
108       
109       public int compare(Object JavaDoc o1, Object JavaDoc o2)
110       {
111          String JavaDoc name1 = ((Class JavaDoc)((WeakReference JavaDoc)o1).get()).getName();
112          String JavaDoc name2 = ((Class JavaDoc)((WeakReference JavaDoc)o2).get()).getName();
113          return (name1).compareTo(name2);
114       }
115    }
116    
117 }
118
Popular Tags