Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/codec/impl/IntegerElement.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
26 import org.apache.qpid.proton.codec.Data;
27
28 class IntegerElement extends AtomicElement<Integer>
29 {
30
31 private final int _value;
32
33 IntegerElement(Element parent, Element prev, int i)
34 {
35 super(parent, prev);
36 _value = i;
37 }
38
39 @Override
40 public int size()
41 {
42 if(isElementOfArray())
43 {
44 final ArrayElement parent = (ArrayElement) parent();
45 if(parent.constructorType() == ArrayElement.SMALL)
46 {
47 if(-128 <= _value && _value <= 127)
48 {
49 return 1;
50 }
51 else
52 {
53 parent.setConstructorType(ArrayElement.LARGE);
54 return 4;
55 }
56 }
57 else
58 {
59 return 4;
60 }
61 }
62 else
63 {
64 return (-128 <= _value && _value <= 127) ? 2 : 5;
65 }
66
67 }
68
69 @Override
70 public Integer getValue()
71 {
72 return _value;
73 }
74
75 @Override
76 public Data.DataType getDataType()
77 {
78 return Data.DataType.INT;
79 }
80
81 @Override
82 public int encode(ByteBuffer b)
83 {
84 int size = size();
85 if(size <= b.remaining())
86 {
87 switch(size)
88 {
89 case 2:
90 b.put((byte)0x54);
91 case 1:
92 b.put((byte)_value);
93 break;
94
95 case 5:
96 b.put((byte)0x71);
97 case 4:
98 b.putInt(_value);
99
100 }
101
102 return size;
103 }
104 return 0;
105 }
106 }