summaryrefslogtreecommitdiff
path: root/qpid/cpp/bindings/swig_java_helper.i
blob: a42f02b480bc5463bf9f8b597cbd63f8e8c58ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * ===================== Java Helper Methods ==========================
 * Defines helper methods in support of typemaps.
 * These methods are placed in the respective module.
 * ===================================================================
 */

%pragma(java) moduleimports=%{
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
%}

%pragma(java) modulecode=%{
  /** Checks if the buffer passed is a direct buffer
   *  This method could also convert a non direct buffer into a direct buffer.
   *  However the extra copying deafeats the purpose of the binding.
   */
  static protected java.nio.ByteBuffer isBufferDirect(java.nio.ByteBuffer buff)
  {
        if (buff.isDirect())
        {
            return buff;
        }
        else
        {
          throw new RuntimeException("The ByteBuffer passed is not allocated direct");
        }
  }

  static protected void checkVariantMapKey(Object key)
  {
        if (key == null)
        {
            throw new NullPointerException("Key cannot be null");
        }
        if (! (key instanceof String))
        {
            throw new ClassCastException("Key should be of type java.lang.String");
        }
  }

  static long getVariantMap(final Map<String,Object> m)
  {
      //optimizing for the null & empty map case.
      if (m == null || m.size() == 0) { return 0; }

      WriteOnlyVariantMapWrapper map = new WriteOnlyVariantMapWrapper();
      for (String key: m.keySet())
      {
          map.put(key,m.get(key));
      }
      return WriteOnlyVariantMapWrapper.getCPtr(map);
  }

  static Map<String, Object> getJavaMap(final ReadOnlyVariantMapWrapper map)
  {
        if (map == null)
        {
            return Collections.emptyMap();
        }

        return new Map<String, Object>()
        {
            @Override
            public int size()
            {
                return map.size();
            }

            @Override
            public boolean isEmpty()
            {
                return map.isEmpty();
            }

            @Override
            public boolean containsKey(Object key)
            {
                checkVariantMapKey(key);
                return map.containsKey((String)key);
            }

            @Override
            public boolean containsValue(Object value)
            {
                throw new UnsupportedOperationException("Unsupported at the native layer for efficiency");
            }

            @Override
            public Object get(Object key)
            {
                checkVariantMapKey(key);
                return map.get((String)key);
            }

            @Override
            public Object put(String key, Object value)
            {
                throw new UnsupportedOperationException("This map is read-only");
            }

            @Override
            public Object remove(Object key)
            {
                throw new UnsupportedOperationException("This map is read-only");
            }

            @Override
            public void putAll(Map<? extends String, ? extends Object> m)
            {
                throw new UnsupportedOperationException("This map is read-only");
            }

            @Override
            public void clear()
            {
                throw new UnsupportedOperationException("This map is read-only");
            }

            @Override
            public Set<String> keySet()
            {
                Set<String> keys = new HashSet<String>();
                for (String key:(String[])map.keys())
                {
                    keys.add(key);
                }

                return keys;
            }

            @Override
            public Collection<Object> values()
            {
                throw new UnsupportedOperationException("Unsupported at the native layer for efficiency");
            }

            @Override
            public Set<Entry<String, Object>> entrySet()
            {
                Set<Entry<String, Object>> entries = new HashSet<Entry<String, Object>>();
                for (final String key: keySet())
                {
                    final Object value = map.get(key);
                    entries.add(new Entry<String, Object>()
                    {
                        @Override
                        public String getKey()
                        {
                            return key;
                        }

                        @Override
                        public Object getValue()
                        {
                            return value;
                        }

                        @Override
                        public Object setValue(Object value)
                        {
                            throw new UnsupportedOperationException("This set is read-only");
                        }

                    });
                }
                return entries;
            }

        };
  }
%}