1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.uno.helper;
25 import com.sun.star.uno.Type;
26 import com.sun.star.lang.EventObject;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Iterator;
30 
31 public class MultiTypeInterfaceContainer
32 {
33 
34     private Map map= new HashMap();
35 
36     /** Creates a new instance of MultiTypeInterfaceContainer */
MultiTypeInterfaceContainer()37     public MultiTypeInterfaceContainer()
38     {
39     }
40 
41     /** only returns types which have at least one value in InterfaceContainer
42      *  return value can contain an element null, if someone called
43      *  addInterface (null, interf)
44      */
getContainedTypes()45     synchronized public Type[] getContainedTypes()
46     {
47         int size;
48         Type[] retVal= null;
49 
50         if ( (size=map.size()) > 0)
51         {
52             Type [] arTypes= new Type[size];
53             Iterator it= map.keySet().iterator();
54 
55             int countTypes= 0;
56             while (it.hasNext())
57             {
58                 Object key= it.next();
59                 InterfaceContainer cont= (InterfaceContainer) map.get(key);
60                 if (cont != null && cont.size() > 0)
61                 {
62                     if (key == null)
63                         arTypes[countTypes++]= new Type();
64                     else if (key instanceof Type)
65                         arTypes[countTypes++]= (Type) key;
66                     else if (key instanceof Class)
67                         arTypes[countTypes++]= new Type((Class) key);
68                     else
69                         arTypes[countTypes++]= new Type(key.getClass());
70                 }
71             }
72 
73             if (countTypes != size)
74             {
75                 retVal= new Type[countTypes];
76                 System.arraycopy(arTypes, 0, retVal, 0, countTypes);
77             }
78             else
79                 retVal= arTypes;
80         }
81         if (retVal == null)
82             retVal= new Type[0];
83         return retVal;
84     }
85 
86     /** param key can be null */
getContainer(Object key)87     synchronized public InterfaceContainer getContainer(Object key)
88     {
89         InterfaceContainer retVal= null;
90         Iterator it= map.keySet().iterator();
91         while (it.hasNext())
92         {
93             Object obj= it.next();
94             if (obj == null && key == null)
95             {
96                 retVal= (InterfaceContainer) map.get(null);
97                 break;
98             }
99             else if( obj != null && obj.equals(key))
100             {
101                 retVal= (InterfaceContainer) map.get(obj);
102                 break;
103             }
104         }
105         return retVal;
106     }
107 
108 
addInterface(Object ckey, Object iface)109     synchronized public int addInterface(Object ckey, Object iface)
110     {
111         //If the key is a Type then it does not matter if the objects are different
112         // if they represent the same type. This is because Types overrides hashCode and
113         // equals. For example:
114         // Type a= new Type(XInterface.class);
115         // Type b= new Type(XInterface.class);
116         // Allthough a != b , the map interprets both as being the same.
117         InterfaceContainer cont= (InterfaceContainer) map.get(ckey);
118         if (cont != null)
119         {
120             cont.add(iface);
121         }
122         else
123         {
124             cont= new InterfaceContainer();
125             cont.add(iface);
126             map.put(ckey, cont);
127         }
128         return cont.size();
129     }
130 
131 
removeInterface(Object key, Object iface)132     synchronized public int removeInterface(Object key, Object iface)
133     {
134         int retVal= 0;
135         InterfaceContainer cont= (InterfaceContainer) map.get(key);
136         if (cont != null)
137         {
138             cont.remove(iface);
139             retVal= cont.size();
140         }
141         return retVal;
142     }
143 
disposeAndClear(EventObject evt)144     public void disposeAndClear(EventObject evt)
145     {
146         Iterator it= null;
147         synchronized(this)
148         {
149             it= map.values().iterator();
150         }
151         while (it.hasNext() )
152             ((InterfaceContainer) it.next()).disposeAndClear(evt);
153     }
154 
clear()155     synchronized public void clear()
156     {
157         Iterator it= map.values().iterator();
158         while (it.hasNext())
159             ((InterfaceContainer) it.next()).clear();
160     }
161 }
162