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<Object,InterfaceContainer> map= new HashMap<Object,InterfaceContainer>();
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 
54             int countTypes= 0;
55             for (Map.Entry<Object,InterfaceContainer> entry : map.entrySet())
56             {
57                 Object key= entry.getKey();
58                 InterfaceContainer cont= entry.getValue();
59                 if (cont != null && cont.size() > 0)
60                 {
61                     if (key == null)
62                         arTypes[countTypes++]= new Type();
63                     else if (key instanceof Type)
64                         arTypes[countTypes++]= (Type) key;
65                     else if (key instanceof Class)
66                         arTypes[countTypes++]= new Type((Class<?>) key);
67                     else
68                         arTypes[countTypes++]= new Type(key.getClass());
69                 }
70             }
71 
72             if (countTypes != size)
73             {
74                 retVal= new Type[countTypes];
75                 System.arraycopy(arTypes, 0, retVal, 0, countTypes);
76             }
77             else
78                 retVal= arTypes;
79         }
80         if (retVal == null)
81             retVal= new Type[0];
82         return retVal;
83     }
84 
85     /** param key can be null */
getContainer(Object key)86     synchronized public InterfaceContainer getContainer(Object key)
87     {
88         return map.get(key);
89     }
90 
91 
addInterface(Object ckey, Object iface)92     synchronized public int addInterface(Object ckey, Object iface)
93     {
94         //If the key is a Type then it does not matter if the objects are different
95         // if they represent the same type. This is because Types overrides hashCode and
96         // equals. For example:
97         // Type a= new Type(XInterface.class);
98         // Type b= new Type(XInterface.class);
99         // Although a != b , the map interprets both as being the same.
100         InterfaceContainer cont= map.get(ckey);
101         if (cont != null)
102         {
103             cont.add(iface);
104         }
105         else
106         {
107             cont= new InterfaceContainer();
108             cont.add(iface);
109             map.put(ckey, cont);
110         }
111         return cont.size();
112     }
113 
114 
removeInterface(Object key, Object iface)115     synchronized public int removeInterface(Object key, Object iface)
116     {
117         int retVal= 0;
118         InterfaceContainer cont= map.get(key);
119         if (cont != null)
120         {
121             cont.remove(iface);
122             retVal= cont.size();
123         }
124         return retVal;
125     }
126 
disposeAndClear(EventObject evt)127     public void disposeAndClear(EventObject evt)
128     {
129         Iterator<InterfaceContainer> it= null;
130         synchronized(this)
131         {
132             it= map.values().iterator();
133         }
134         while (it.hasNext() )
135             it.next().disposeAndClear(evt);
136     }
137 
clear()138     synchronized public void clear()
139     {
140         Iterator<InterfaceContainer> it= map.values().iterator();
141         while (it.hasNext())
142             it.next().clear();
143     }
144 }
145