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 package org.apache.openoffice.comp.sdbc.dbtools.util;
23 
24 import java.util.Map;
25 
26 import com.sun.star.container.NoSuchElementException;
27 import com.sun.star.container.XNameAccess;
28 import com.sun.star.lang.WrappedTargetException;
29 import com.sun.star.lib.uno.helper.WeakBase;
30 import com.sun.star.uno.Type;
31 
32 public class MapToXNameAccessAdapter extends WeakBase implements XNameAccess {
33     protected final Map<String,Object> map;
34     protected final Object lock;
35     private final Type elementType;
36 
MapToXNameAccessAdapter(Map<String,Object> map, Object lock, Type elementType)37     public MapToXNameAccessAdapter(Map<String,Object> map, Object lock, Type elementType) {
38         this.map = map;
39         this.lock = lock;
40         this.elementType = elementType;
41     }
42 
43     // XNameAccess:
44 
45     @Override
getByName(String key)46     public Object getByName(String key)
47             throws NoSuchElementException, WrappedTargetException {
48         Object object;
49         synchronized (lock) {
50             object = map.get(key);
51         }
52         if (object == null) {
53             throw new NoSuchElementException();
54         }
55         return object;
56     }
57 
58     @Override
getElementNames()59     public String[] getElementNames() {
60         synchronized (lock) {
61             String[] names = new String[map.size()];
62             int next = 0;
63             for (Map.Entry<String,Object> entry : map.entrySet()) {
64                 names[next++] = entry.getKey().toString();
65             }
66             return names;
67         }
68     }
69 
70     @Override
hasByName(String key)71     public boolean hasByName(String key) {
72         synchronized (lock) {
73             return map.containsKey(key);
74         }
75     }
76 
77     // XElementAccess:
78 
79     @Override
getElementType()80     public Type getElementType() {
81         return elementType;
82     }
83 
84 
85     @Override
hasElements()86     public boolean hasElements() {
87         synchronized (lock) {
88             return !map.isEmpty();
89         }
90     }
91 }
92