Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/codec/impl/MapElement.java

raw
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.qpid.proton.codec.impl;
23
24 import java.nio.ByteBuffer;
25 import java.util.*;
26
27 import org.apache.qpid.proton.codec.Data;
28
29 class MapElement extends AbstractElement<Map<Object,Object>>
30 {
31 private Element _first;
32
33 MapElement(Element parent, Element prev)
34 {
35 super(parent, prev);
36 }
37
38
39 public int count()
40 {
41 int count = 0;
42 Element elt = _first;
43 while(elt != null)
44 {
45 count++;
46 elt = elt.next();
47 }
48 return count;
49 }
50
51 @Override
52 public int size()
53 {
54 int count = 0;
55 int size = 0;
56 Element elt = _first;
57 while(elt != null)
58 {
59 count++;
60 size += elt.size();
61 elt = elt.next();
62 }
63 if(isElementOfArray())
64 {
65 ArrayElement parent = (ArrayElement) parent();
66
67 if(parent.constructorType() == ArrayElement.SMALL)
68 {
69 if(count > 255 || size > 254)
70 {
71 parent.setConstructorType(ArrayElement.ConstructorType.LARGE);
72 size += 8;
73 }
74 else
75 {
76 size += 2;
77 }
78 }
79 else
80 {
81 size += 8;
82 }
83 }
84 else
85 {
86 if(count <= 255 && size <= 254)
87 {
88 size += 3;
89 }
90 else
91 {
92 size+=9;
93 }
94 }
95
96 return size;
97 }
98
99 @Override
100 public Map<Object,Object> getValue()
101 {
102 LinkedHashMap<Object,Object> map = new LinkedHashMap<Object,Object>();
103 Element elt = _first;
104 while(elt != null)
105 {
106 Object key = elt.getValue();
107 Object value;
108 elt = elt.next();
109 if(elt != null)
110 {
111 value = elt.getValue();
112 elt = elt.next();
113 }
114 else
115 {
116 value = null;
117 }
118 map.put(key,value);
119 }
120
121 return Collections.unmodifiableMap(map);
122 }
123
124 @Override
125 public Data.DataType getDataType()
126 {
127 return Data.DataType.MAP;
128 }
129
130 @Override
131 public int encode(ByteBuffer b)
132 {
133 int encodedSize = size();
134
135 int count = 0;
136 int size = 0;
137 Element elt = _first;
138 while(elt != null)
139 {
140 count++;
141 size += elt.size();
142 elt = elt.next();
143 }
144
145 if(encodedSize > b.remaining())
146 {
147 return 0;
148 }
149 else
150 {
151 if(isElementOfArray())
152 {
153 switch(((ArrayElement)parent()).constructorType())
154 {
155 case SMALL:
156 b.put((byte)(size+1));
157 b.put((byte)count);
158 break;
159 case LARGE:
160 b.putInt((size+4));
161 b.putInt(count);
162 }
163 }
164 else
165 {
166 if(size <= 254 && count <=255)
167 {
168 b.put((byte)0xc1);
169 b.put((byte)(size+1));
170 b.put((byte)count);
171 }
172 else
173 {
174 b.put((byte)0xd1);
175 b.putInt((size+4));
176 b.putInt(count);
177 }
178
179 }
180
181 elt = _first;
182 while(elt != null)
183 {
184 elt.encode(b);
185 elt = elt.next();
186 }
187
188 return encodedSize;
189 }
190 }
191
192 @Override
193 public boolean canEnter()
194 {
195 return true;
196 }
197
198 @Override
199 public Element child()
200 {
201 return _first;
202 }
203
204 @Override
205 public void setChild(Element elt)
206 {
207 _first = elt;
208 }
209
210 @Override
211 public Element checkChild(Element element)
212 {
213 return element;
214 }
215
216 @Override
217 public Element addChild(Element element)
218 {
219 _first = element;
220 return element;
221 }
222
223 @Override
224 String startSymbol() {
225 return "{";
226 }
227
228 @Override
229 String stopSymbol() {
230 return "}";
231 }
232
233 }