Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/codec/impl/DataImpl.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.amqp.*;
28 import org.apache.qpid.proton.codec.Data;
29
30
31 public class DataImpl implements Data
32 {
33
34 private Element _first;
35 private Element _current;
36 private Element _parent;
37
38
39 public DataImpl()
40 {
41 }
42
43 @Override
44 public void free()
45 {
46 _first = null;
47 _current = null;
48
49 }
50
51 @Override
52 public void clear()
53 {
54 _first=null;
55 _current=null;
56 _parent=null;
57 }
58
59 @Override
60 public long size()
61 {
62 return _first == null ? 0 : _first.size();
63 }
64
65 @Override
66 public void rewind()
67 {
68 _current = null;
69 _parent = null;
70 }
71
72 @Override
73 public DataType next()
74 {
75 Element next = _current == null ? (_parent == null ? _first : _parent.child()) : _current.next();
76
77 if(next != null)
78 {
79 _current = next;
80 }
81 return next == null ? null : next.getDataType();
82 }
83
84 @Override
85 public DataType prev()
86 {
87 Element prev = _current == null ? null : _current.prev();
88
89 _current = prev;
90 return prev == null ? null : prev.getDataType();
91 }
92
93 @Override
94 public boolean enter()
95 {
96 if(_current != null && _current.canEnter())
97 {
98
99 _parent = _current;
100 _current = null;
101 return true;
102 }
103 return false;
104 }
105
106 @Override
107 public boolean exit()
108 {
109 if(_parent != null)
110 {
111 Element parent = _parent;
112 _current = parent;
113 _parent = _current.parent();
114 return true;
115
116 }
117 return false;
118 }
119
120 @Override
121 public DataType type()
122 {
123 return _current == null ? null : _current.getDataType();
124 }
125
126 @Override
127 public long encodedSize()
128 {
129 int size = 0;
130 Element elt = _first;
131 while(elt != null)
132 {
133 size += elt.size();
134 elt = elt.next();
135 }
136 return size;
137 }
138
139 @Override
140 public Binary encode()
141 {
142 byte[] data = new byte[(int)encodedSize()];
143 ByteBuffer buf = ByteBuffer.wrap(data);
144 encode(buf);
145
146 return new Binary(data);
147 }
148
149 @Override
150 public long encode(ByteBuffer buf)
151 {
152 Element elt = _first;
153 int size = 0;
154 while(elt != null )
155 {
156 final int eltSize = elt.size();
157 if(eltSize <= buf.remaining())
158 {
159 size += elt.encode(buf);
160 }
161 else
162 {
163 size+= eltSize;
164 }
165 elt = elt.next();
166 }
167 return size;
168 }
169
170 @Override
171 public long decode(ByteBuffer buf)
172 {
173 return DataDecoder.decode(buf, this);
174 }
175
176
177 private void putElement(Element element)
178 {
179 if(_first == null)
180 {
181 _first = element;
182 }
183 else
184 {
185 if(_current == null)
186 {
187 if (_parent == null) {
188 _first = _first.replaceWith(element);
189 element = _first;
190 } else {
191 element = _parent.addChild(element);
192 }
193 }
194 else
195 {
196 if(_parent!=null)
197 {
198 element = _parent.checkChild(element);
199 }
200 _current.setNext(element);
201 }
202 }
203
204 _current = element;
205 }
206
207 @Override
208 public void putList()
209 {
210 putElement(new ListElement(_parent, _current));
211 }
212
213 @Override
214 public void putMap()
215 {
216 putElement(new MapElement(_parent, _current));
217 }
218
219 @Override
220 public void putArray(boolean described, DataType type)
221 {
222 putElement(new ArrayElement(_parent, _current, described, type));
223
224 }
225
226 @Override
227 public void putDescribed()
228 {
229 putElement(new DescribedTypeElement(_parent, _current));
230 }
231
232 @Override
233 public void putNull()
234 {
235 putElement(new NullElement(_parent, _current));
236
237 }
238
239 @Override
240 public void putBoolean(boolean b)
241 {
242 putElement(new BooleanElement(_parent, _current, b));
243 }
244
245 @Override
246 public void putUnsignedByte(UnsignedByte ub)
247 {
248 putElement(new UnsignedByteElement(_parent, _current, ub));
249
250 }
251
252 @Override
253 public void putByte(byte b)
254 {
255 putElement(new ByteElement(_parent, _current, b));
256 }
257
258 @Override
259 public void putUnsignedShort(UnsignedShort us)
260 {
261 putElement(new UnsignedShortElement(_parent, _current, us));
262
263 }
264
265 @Override
266 public void putShort(short s)
267 {
268 putElement(new ShortElement(_parent, _current, s));
269 }
270
271 @Override
272 public void putUnsignedInteger(UnsignedInteger ui)
273 {
274 putElement(new UnsignedIntegerElement(_parent, _current, ui));
275 }
276
277 @Override
278 public void putInt(int i)
279 {
280 putElement(new IntegerElement(_parent, _current, i));
281 }
282
283 @Override
284 public void putChar(int c)
285 {
286 putElement(new CharElement(_parent, _current, c));
287 }
288
289 @Override
290 public void putUnsignedLong(UnsignedLong ul)
291 {
292 putElement(new UnsignedLongElement(_parent, _current, ul));
293 }
294
295 @Override
296 public void putLong(long l)
297 {
298 putElement(new LongElement(_parent, _current, l));
299 }
300
301 @Override
302 public void putTimestamp(Date t)
303 {
304 putElement(new TimestampElement(_parent,_current,t));
305 }
306
307 @Override
308 public void putFloat(float f)
309 {
310 putElement(new FloatElement(_parent,_current,f));
311 }
312
313 @Override
314 public void putDouble(double d)
315 {
316 putElement(new DoubleElement(_parent,_current,d));
317 }
318
319 @Override
320 public void putDecimal32(Decimal32 d)
321 {
322 putElement(new Decimal32Element(_parent,_current,d));
323 }
324
325 @Override
326 public void putDecimal64(Decimal64 d)
327 {
328 putElement(new Decimal64Element(_parent,_current,d));
329 }
330
331 @Override
332 public void putDecimal128(Decimal128 d)
333 {
334 putElement(new Decimal128Element(_parent,_current,d));
335 }
336
337 @Override
338 public void putUUID(UUID u)
339 {
340 putElement(new UUIDElement(_parent,_current,u));
341 }
342
343 @Override
344 public void putBinary(Binary bytes)
345 {
346 putElement(new BinaryElement(_parent, _current, bytes));
347 }
348
349 @Override
350 public void putBinary(byte[] bytes)
351 {
352 putBinary(new Binary(bytes));
353 }
354
355 @Override
356 public void putString(String string)
357 {
358 putElement(new StringElement(_parent,_current,string));
359 }
360
361 @Override
362 public void putSymbol(Symbol symbol)
363 {
364 putElement(new SymbolElement(_parent,_current,symbol));
365 }
366
367 @Override
368 public void putObject(Object o)
369 {
370 if(o == null)
371 {
372 putNull();
373 }
374 else if(o instanceof Boolean)
375 {
376 putBoolean((Boolean) o);
377 }
378 else if(o instanceof UnsignedByte)
379 {
380 putUnsignedByte((UnsignedByte)o);
381 }
382 else if(o instanceof Byte)
383 {
384 putByte((Byte)o);
385 }
386 else if(o instanceof UnsignedShort)
387 {
388 putUnsignedShort((UnsignedShort)o);
389 }
390 else if(o instanceof Short)
391 {
392 putShort((Short)o);
393 }
394 else if(o instanceof UnsignedInteger)
395 {
396 putUnsignedInteger((UnsignedInteger)o);
397 }
398 else if(o instanceof Integer)
399 {
400 putInt((Integer)o);
401 }
402 else if(o instanceof Character)
403 {
404 putChar((Character)o);
405 }
406 else if(o instanceof UnsignedLong)
407 {
408 putUnsignedLong((UnsignedLong)o);
409 }
410 else if(o instanceof Long)
411 {
412 putLong((Long)o);
413 }
414 else if(o instanceof Date)
415 {
416 putTimestamp((Date)o);
417 }
418 else if(o instanceof Float)
419 {
420 putFloat((Float)o);
421 }
422 else if(o instanceof Double)
423 {
424 putDouble((Double)o);
425 }
426 else if(o instanceof Decimal32)
427 {
428 putDecimal32((Decimal32)o);
429 }
430 else if(o instanceof Decimal64)
431 {
432 putDecimal64((Decimal64)o);
433 }
434 else if(o instanceof Decimal128)
435 {
436 putDecimal128((Decimal128)o);
437 }
438 else if(o instanceof UUID)
439 {
440 putUUID((UUID)o);
441 }
442 else if(o instanceof Binary)
443 {
444 putBinary((Binary)o);
445 }
446 else if(o instanceof String)
447 {
448 putString((String)o);
449 }
450 else if(o instanceof Symbol)
451 {
452 putSymbol((Symbol)o);
453 }
454 else if(o instanceof DescribedType)
455 {
456 putDescribedType((DescribedType)o);
457 }
458 else if(o instanceof Symbol[])
459 {
460 putArray(false, Data.DataType.SYMBOL);
461 enter();
462 for(Symbol s : (Symbol[]) o)
463 {
464 putSymbol(s);
465 }
466 exit();
467 }
468 else if(o instanceof Object[])
469 {
470 throw new IllegalArgumentException("Unsupported array type");
471 }
472 else if(o instanceof List)
473 {
474 putJavaList((List)o);
475 }
476 else if(o instanceof Map)
477 {
478 putJavaMap((Map)o);
479 }
480 else
481 {
482 throw new IllegalArgumentException("Unknown type " + o.getClass().getSimpleName());
483 }
484 }
485
486 @Override
487 public void putJavaMap(Map<Object, Object> map)
488 {
489 putMap();
490 enter();
491 for(Map.Entry<Object, Object> entry : map.entrySet())
492 {
493 putObject(entry.getKey());
494 putObject(entry.getValue());
495 }
496 exit();
497
498 }
499
500 @Override
501 public void putJavaList(List<Object> list)
502 {
503 putList();
504 enter();
505 for(Object o : list)
506 {
507 putObject(o);
508 }
509 exit();
510 }
511
512 @Override
513 public void putDescribedType(DescribedType dt)
514 {
515 putElement(new DescribedTypeElement(_parent,_current));
516 enter();
517 putObject(dt.getDescriptor());
518 putObject(dt.getDescribed());
519 exit();
520 }
521
522 @Override
523 public long getList()
524 {
525 if(_current instanceof ListElement)
526 {
527 return ((ListElement)_current).count();
528 }
529 throw new IllegalStateException("Current value not list");
530 }
531
532 @Override
533 public long getMap()
534 {
535 if(_current instanceof MapElement)
536 {
537 return ((MapElement)_current).count();
538 }
539 throw new IllegalStateException("Current value not map");
540 }
541
542 @Override
543 public long getArray()
544 {
545 if(_current instanceof ArrayElement)
546 {
547 return ((ArrayElement)_current).count();
548 }
549 throw new IllegalStateException("Current value not array");
550 }
551
552 @Override
553 public boolean isArrayDescribed()
554 {
555 if(_current instanceof ArrayElement)
556 {
557 return ((ArrayElement)_current).isDescribed();
558 }
559 throw new IllegalStateException("Current value not array");
560 }
561
562 @Override
563 public DataType getArrayType()
564 {
565 if(_current instanceof ArrayElement)
566 {
567 return ((ArrayElement)_current).getArrayDataType();
568 }
569 throw new IllegalStateException("Current value not array");
570 }
571
572 @Override
573 public boolean isDescribed()
574 {
575 return _current != null && _current.getDataType() == DataType.DESCRIBED;
576 }
577
578 @Override
579 public boolean isNull()
580 {
581 return _current != null && _current.getDataType() == DataType.NULL;
582 }
583
584 @Override
585 public boolean getBoolean()
586 {
587 if(_current instanceof BooleanElement)
588 {
589 return ((BooleanElement)_current).getValue();
590 }
591 throw new IllegalStateException("Current value not boolean");
592 }
593
594 @Override
595 public UnsignedByte getUnsignedByte()
596 {
597
598 if(_current instanceof UnsignedByteElement)
599 {
600 return ((UnsignedByteElement)_current).getValue();
601 }
602 throw new IllegalStateException("Current value not unsigned byte");
603 }
604
605 @Override
606 public byte getByte()
607 {
608 if(_current instanceof ByteElement)
609 {
610 return ((ByteElement)_current).getValue();
611 }
612 throw new IllegalStateException("Current value not byte");
613 }
614
615 @Override
616 public UnsignedShort getUnsignedShort()
617 {
618 if(_current instanceof UnsignedShortElement)
619 {
620 return ((UnsignedShortElement)_current).getValue();
621 }
622 throw new IllegalStateException("Current value not unsigned short");
623 }
624
625 @Override
626 public short getShort()
627 {
628 if(_current instanceof ShortElement)
629 {
630 return ((ShortElement)_current).getValue();
631 }
632 throw new IllegalStateException("Current value not short");
633 }
634
635 @Override
636 public UnsignedInteger getUnsignedInteger()
637 {
638 if(_current instanceof UnsignedIntegerElement)
639 {
640 return ((UnsignedIntegerElement)_current).getValue();
641 }
642 throw new IllegalStateException("Current value not unsigned integer");
643 }
644
645 @Override
646 public int getInt()
647 {
648 if(_current instanceof IntegerElement)
649 {
650 return ((IntegerElement)_current).getValue();
651 }
652 throw new IllegalStateException("Current value not integer");
653 }
654
655 @Override
656 public int getChar()
657 {
658 if(_current instanceof CharElement)
659 {
660 return ((CharElement)_current).getValue();
661 }
662 throw new IllegalStateException("Current value not char");
663 }
664
665 @Override
666 public UnsignedLong getUnsignedLong()
667 {
668 if(_current instanceof UnsignedLongElement)
669 {
670 return ((UnsignedLongElement)_current).getValue();
671 }
672 throw new IllegalStateException("Current value not unsigned long");
673 }
674
675 @Override
676 public long getLong()
677 {
678 if(_current instanceof LongElement)
679 {
680 return ((LongElement)_current).getValue();
681 }
682 throw new IllegalStateException("Current value not long");
683 }
684
685 @Override
686 public Date getTimestamp()
687 {
688 if(_current instanceof TimestampElement)
689 {
690 return ((TimestampElement)_current).getValue();
691 }
692 throw new IllegalStateException("Current value not timestamp");
693 }
694
695 @Override
696 public float getFloat()
697 {
698 if(_current instanceof FloatElement)
699 {
700 return ((FloatElement)_current).getValue();
701 }
702 throw new IllegalStateException("Current value not float");
703 }
704
705 @Override
706 public double getDouble()
707 {
708 if(_current instanceof DoubleElement)
709 {
710 return ((DoubleElement)_current).getValue();
711 }
712 throw new IllegalStateException("Current value not double");
713 }
714
715 @Override
716 public Decimal32 getDecimal32()
717 {
718 if(_current instanceof Decimal32Element)
719 {
720 return ((Decimal32Element)_current).getValue();
721 }
722 throw new IllegalStateException("Current value not decimal32");
723 }
724
725 @Override
726 public Decimal64 getDecimal64()
727 {
728 if(_current instanceof Decimal64Element)
729 {
730 return ((Decimal64Element)_current).getValue();
731 }
732 throw new IllegalStateException("Current value not decimal32");
733 }
734
735 @Override
736 public Decimal128 getDecimal128()
737 {
738 if(_current instanceof Decimal128Element)
739 {
740 return ((Decimal128Element)_current).getValue();
741 }
742 throw new IllegalStateException("Current value not decimal32");
743 }
744
745 @Override
746 public UUID getUUID()
747 {
748 if(_current instanceof UUIDElement)
749 {
750 return ((UUIDElement)_current).getValue();
751 }
752 throw new IllegalStateException("Current value not uuid");
753 }
754
755 @Override
756 public Binary getBinary()
757 {
758 if(_current instanceof BinaryElement)
759 {
760 return ((BinaryElement)_current).getValue();
761 }
762 throw new IllegalStateException("Current value not binary");
763 }
764
765 @Override
766 public String getString()
767 {
768 if (_current instanceof StringElement)
769 {
770 return ((StringElement) _current).getValue();
771 }
772 throw new IllegalStateException("Current value not string");
773 }
774
775 @Override
776 public Symbol getSymbol()
777 {
778 if(_current instanceof SymbolElement)
779 {
780 return ((SymbolElement)_current).getValue();
781 }
782 throw new IllegalStateException("Current value not symbol");
783 }
784
785 @Override
786 public Object getObject()
787 {
788 return _current == null ? null : _current.getValue();
789 }
790
791 @Override
792 public Map<Object, Object> getJavaMap()
793 {
794 if(_current instanceof MapElement)
795 {
796 return ((MapElement)_current).getValue();
797 }
798 throw new IllegalStateException("Current value not map");
799 }
800
801 @Override
802 public List<Object> getJavaList()
803 {
804 if(_current instanceof ListElement)
805 {
806 return ((ListElement)_current).getValue();
807 }
808 throw new IllegalStateException("Current value not list");
809 }
810
811 @Override
812 public Object[] getJavaArray()
813 {
814 if(_current instanceof ArrayElement)
815 {
816 return ((ArrayElement)_current).getValue();
817 }
818 throw new IllegalStateException("Current value not array");
819 }
820
821 @Override
822 public DescribedType getDescribedType()
823 {
824 if(_current instanceof DescribedTypeElement)
825 {
826 return ((DescribedTypeElement)_current).getValue();
827 }
828 throw new IllegalStateException("Current value not described type");
829 }
830
831 @Override
832 public String format()
833 {
834 StringBuilder sb = new StringBuilder();
835 Element el = _first;
836 boolean first = true;
837 while (el != null) {
838 if (first) {
839 first = false;
840 } else {
841 sb.append(", ");
842 }
843 el.render(sb);
844 el = el.next();
845 }
846
847 return sb.toString();
848 }
849
850 private void render(StringBuilder sb, Element el)
851 {
852 if (el == null) return;
853 sb.append(" ").append(el).append("\n");
854 if (el.canEnter()) {
855 render(sb, el.child());
856 }
857 render(sb, el.next());
858 }
859
860 @Override
861 public String toString()
862 {
863 StringBuilder sb = new StringBuilder();
864 render(sb, _first);
865 return String.format("Data[current=%h, parent=%h]{%n%s}",
866 System.identityHashCode(_current),
867 System.identityHashCode(_parent),
868 sb);
869 }
870 }