1 /* 2 * hunt-proton: AMQP Protocol library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.proton.codec.impl.LongElement; 13 14 import hunt.collection.Map; 15 import hunt.io.ByteBuffer; 16 import std.conv; 17 import hunt.proton.codec.impl.AtomicElement; 18 import hunt.proton.codec.impl.Element; 19 import hunt.proton.codec.impl.ArrayElement; 20 import hunt.proton.codec.impl.AbstractElement; 21 22 import hunt.proton.codec.Data; 23 import hunt.Long; 24 25 class LongElement : AtomicElement!Long 26 { 27 28 private long _value; 29 30 this(IElement parent, IElement prev, long l) 31 { 32 super(parent, prev); 33 _value = l; 34 } 35 36 public int size() 37 { 38 if(isElementOfArray()) 39 { 40 ArrayElement parent = cast(ArrayElement) parent(); 41 42 if(parent.constructorType() == ArrayElement.SMALL) 43 { 44 if(-128 <= _value && _value <= 127) 45 { 46 return 1; 47 } 48 else 49 { 50 parent.setConstructorType(ArrayElement.LARGE); 51 } 52 } 53 54 return 8; 55 56 } 57 else 58 { 59 return (-128 <= _value && _value <= 127) ? 2 : 9; 60 } 61 62 } 63 64 public Object getValue() 65 { 66 return new Long (_value); 67 } 68 69 public Data.DataType getDataType() 70 { 71 return Data.DataType.LONG; 72 } 73 74 public int encode(ByteBuffer b) 75 { 76 int size = size(); 77 if(size > b.remaining()) 78 { 79 return 0; 80 } 81 switch(size) 82 { 83 case 2: 84 b.put(cast(byte)0x55); 85 goto case; 86 case 1: 87 b.put(cast(byte)_value); 88 break; 89 case 9: 90 b.put(cast(byte)0x81); 91 goto case; 92 case 8: 93 b.putLong(_value); 94 break; 95 default: 96 break; 97 98 } 99 return size; 100 } 101 }