• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / datasketches-java / #306

30 Apr 2024 10:01PM UTC coverage: 97.645% (-0.5%) from 98.139%
#306

push

web-flow
Merge pull request #555 from apache/fix_pom_xml_header

Fix pom xml header

26865 of 27513 relevant lines covered (97.64%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.93
/src/main/java/org/apache/datasketches/common/ArrayOfNumbersSerDe.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.datasketches.common;
21

22
import static org.apache.datasketches.common.ByteArrayUtil.copyBytes;
23
import static org.apache.datasketches.common.ByteArrayUtil.putDoubleLE;
24
import static org.apache.datasketches.common.ByteArrayUtil.putFloatLE;
25
import static org.apache.datasketches.common.ByteArrayUtil.putIntLE;
26
import static org.apache.datasketches.common.ByteArrayUtil.putLongLE;
27
import static org.apache.datasketches.common.ByteArrayUtil.putShortLE;
28

29
import java.util.Objects;
30

31
import org.apache.datasketches.memory.Memory;
32

33
/**
34
 * Methods of serializing and deserializing arrays of the object version of primitive types of
35
 * Number. The array can be a mix of primitive object types.
36
 *
37
 * <p>This class serializes numbers with a leading byte (ASCII character) indicating the type.
38
 * The class keeps the values byte aligned, even though only 3 bits are strictly necessary to
39
 * encode one of the 6 different primitives with object types that extend Number.</p>
40
 *
41
 * <p>Classes handled are: <code>Long</code>, <code>Integer</code>, <code>Short</code>,
42
 * <code>Byte</code>, <code>Double</code>, and <code>Float</code>.</p>
43
 *
44
 * @author Jon Malkin
45
 */
46
public class ArrayOfNumbersSerDe extends ArrayOfItemsSerDe<Number> {
1✔
47

48
  // values selected to enable backwards compatibility
49
  private static final byte LONG_INDICATOR    = 12;
50
  private static final byte INTEGER_INDICATOR = 9;
51
  private static final byte SHORT_INDICATOR   = 3;
52
  private static final byte BYTE_INDICATOR    = 2;
53
  private static final byte DOUBLE_INDICATOR  = 4;
54
  private static final byte FLOAT_INDICATOR   = 6;
55

56
  @Override
57
  public byte[] serializeToByteArray(final Number item) {
58
    Objects.requireNonNull(item, "Item must not be null");
1✔
59
    final byte[] byteArr;
60
    if (item instanceof Long) {
1✔
61
      byteArr = new byte[Long.BYTES + 1];
1✔
62
      byteArr[0] = LONG_INDICATOR;
1✔
63
      putLongLE(byteArr, 1, (Long)item);
1✔
64
    } else if (item instanceof Integer) {
1✔
65
      byteArr = new byte[Integer.BYTES + 1];
1✔
66
      byteArr[0] = INTEGER_INDICATOR;
1✔
67
      putIntLE(byteArr, 1, (Integer)item);
1✔
68
    } else if (item instanceof Short) {
1✔
69
      byteArr = new byte[Short.BYTES + 1];
1✔
70
      byteArr[0] = SHORT_INDICATOR;
1✔
71
      putShortLE(byteArr, 1, (Short)item);
1✔
72
    } else if (item instanceof Byte) {
1✔
73
      byteArr = new byte[Byte.BYTES + 1];
1✔
74
      byteArr[0] = BYTE_INDICATOR;
1✔
75
      byteArr[1] = (byte)item;
1✔
76
    } else if (item instanceof Double) {
1✔
77
      byteArr = new byte[Double.BYTES + 1];
1✔
78
      byteArr[0] = DOUBLE_INDICATOR;
1✔
79
      putDoubleLE(byteArr, 1, (Double)item);
1✔
80
    } else if (item instanceof Float) {
1✔
81
      byteArr = new byte[Float.BYTES + 1];
1✔
82
      byteArr[0] = FLOAT_INDICATOR;
1✔
83
      putFloatLE(byteArr, 1, (Float)item);
1✔
84
    } else {
85
      throw new SketchesArgumentException(
1✔
86
          "Item must be one of: Long, Integer, Short, Byte, Double, Float. "
87
          + "item: " + item.toString());
1✔
88
    }
89
    return byteArr;
1✔
90
  }
91

92
  @Override
93
  public byte[] serializeToByteArray(final Number[] items) {
94
    Objects.requireNonNull(items, "Items must not be null");
1✔
95
    final int numItems = items.length;
1✔
96
    int totalBytes = 0;
1✔
97
    final byte[][] serialized2DArray = new byte[numItems][];
1✔
98
    for (int i = 0; i < numItems; i++) {
1✔
99
      serialized2DArray[i] = serializeToByteArray(items[i]);
1✔
100
      totalBytes += serialized2DArray[i].length;
1✔
101
    }
102
    final byte[] out = new byte[totalBytes];
1✔
103
    int offset = 0;
1✔
104
    for (int i = 0; i < numItems; i++) {
1✔
105
      final int itemLen = serialized2DArray[i].length;
1✔
106
      copyBytes(serialized2DArray[i], 0, out, offset, itemLen);
1✔
107
      offset += itemLen;
1✔
108
    }
109
    return out;
1✔
110
  }
111

112
  @Override
113
  public Number[] deserializeFromMemory(final Memory mem, final int numItems) {
114
    return deserializeFromMemory(mem, 0, numItems);
×
115
  }
116

117
  @Override
118
  public Number[] deserializeFromMemory(final Memory mem, final long offsetBytes, final int numItems) {
119
    Objects.requireNonNull(mem, "Memory must not be null");
1✔
120
    if (numItems <= 0) { return new Number[0]; }
1✔
121
    final Number[] array = new Number[numItems];
1✔
122
    long offset = offsetBytes;
1✔
123
    for (int i = 0; i < numItems; i++) {
1✔
124
      Util.checkBounds(offset, Byte.BYTES, mem.getCapacity());
1✔
125
      final byte typeId = mem.getByte(offset);
1✔
126
      offset += Byte.BYTES;
1✔
127

128
      switch (typeId) {
1✔
129
        case LONG_INDICATOR:
130
          Util.checkBounds(offset, Long.BYTES, mem.getCapacity());
1✔
131
          array[i] = mem.getLong(offset);
1✔
132
          offset += Long.BYTES;
1✔
133
          break;
1✔
134
        case INTEGER_INDICATOR:
135
          Util.checkBounds(offset, Integer.BYTES, mem.getCapacity());
1✔
136
          array[i] = mem.getInt(offset);
1✔
137
          offset += Integer.BYTES;
1✔
138
          break;
1✔
139
        case SHORT_INDICATOR:
140
          Util.checkBounds(offset, Short.BYTES, mem.getCapacity());
1✔
141
          array[i] = mem.getShort(offset);
1✔
142
          offset += Short.BYTES;
1✔
143
          break;
1✔
144
        case BYTE_INDICATOR:
145
          Util.checkBounds(offset, Byte.BYTES, mem.getCapacity());
1✔
146
          array[i] = mem.getByte(offset);
1✔
147
          offset += Byte.BYTES;
1✔
148
          break;
1✔
149
        case DOUBLE_INDICATOR:
150
          Util.checkBounds(offset, Double.BYTES, mem.getCapacity());
1✔
151
          array[i] = mem.getDouble(offset);
1✔
152
          offset += Double.BYTES;
1✔
153
          break;
1✔
154
        case FLOAT_INDICATOR:
155
          Util.checkBounds(offset, Float.BYTES, mem.getCapacity());
1✔
156
          array[i] = mem.getFloat(offset);
1✔
157
          offset += Float.BYTES;
1✔
158
          break;
1✔
159
        default:
160
          throw new SketchesArgumentException(
1✔
161
              "Item must be one of: Long, Integer, Short, Byte, Double, Float. "
162
              + "index: " + i + ", typeId: " + typeId);
163
      }
164
    }
165
    return array;
1✔
166
  }
167

168
  @Override
169
  public int sizeOf(final Number item) {
170
    Objects.requireNonNull(item, "Item must not be null");
1✔
171
    if ( item instanceof Long)         { return Byte.BYTES + Long.BYTES; }
1✔
172
    else if ( item instanceof Integer) { return Byte.BYTES + Integer.BYTES; }
1✔
173
    else if ( item instanceof Short)   { return Byte.BYTES + Short.BYTES; }
1✔
174
    else if ( item instanceof Byte)    { return Byte.BYTES + Byte.BYTES; }
1✔
175
    else if ( item instanceof Double)  { return Byte.BYTES + Double.BYTES; }
1✔
176
    else if ( item instanceof Float)   { return Byte.BYTES + Float.BYTES; }
1✔
177
    else { throw new SketchesArgumentException(
×
178
        "Item must be one of: Long, Integer, Short, Byte, Double, Float. "
179
        + "item: " + item.toString()); }
×
180
  }
181

182
  @Override
183
  public int sizeOf(final Number[] items) {
184
    Objects.requireNonNull(items, "Items must not be null");
1✔
185
    int totalBytes = 0;
1✔
186
    for (final Number item : items) {
1✔
187
      totalBytes += sizeOf(item);
1✔
188
    }
189
    return totalBytes;
1✔
190
  }
191

192
  @Override
193
  public int sizeOf(final Memory mem, final long offsetBytes, final int numItems) {
194
    Objects.requireNonNull(mem, "Memory must not be null");
1✔
195
    long offset = offsetBytes;
1✔
196
    for (int i = 0; i < numItems; i++) {
1✔
197
      Util.checkBounds(offset, Byte.BYTES, mem.getCapacity());
1✔
198
      final byte typeId = mem.getByte(offset);
1✔
199
      offset += Byte.BYTES;
1✔
200

201
      switch (typeId) {
1✔
202
        case LONG_INDICATOR:
203
          Util.checkBounds(offset, Long.BYTES, mem.getCapacity());
1✔
204
          offset += Long.BYTES;
1✔
205
          break;
1✔
206
        case INTEGER_INDICATOR:
207
          Util.checkBounds(offset, Integer.BYTES, mem.getCapacity());
1✔
208
          offset += Integer.BYTES;
1✔
209
          break;
1✔
210
        case SHORT_INDICATOR:
211
          Util.checkBounds(offset, Short.BYTES, mem.getCapacity());
1✔
212
          offset += Short.BYTES;
1✔
213
          break;
1✔
214
        case BYTE_INDICATOR:
215
          Util.checkBounds(offset, Byte.BYTES, mem.getCapacity());
1✔
216
          offset += Byte.BYTES;
1✔
217
          break;
1✔
218
        case DOUBLE_INDICATOR:
219
          Util.checkBounds(offset, Double.BYTES, mem.getCapacity());
1✔
220
          offset += Double.BYTES;
1✔
221
          break;
1✔
222
        case FLOAT_INDICATOR:
223
          Util.checkBounds(offset, Float.BYTES, mem.getCapacity());
1✔
224
          offset += Float.BYTES;
1✔
225
          break;
1✔
226
        default:
227
          throw new SketchesArgumentException(
×
228
              "Item must be one of: Long, Integer, Short, Byte, Double, Float. "
229
              + "index: " + i + ", typeId: " + typeId);
230
      }
231
    }
232
    return (int)(offset - offsetBytes);
1✔
233
  }
234

235
  @Override
236
  public String toString(final Number item) {
237
    if (item == null) { return "null"; }
1✔
238
    return item.toString();
1✔
239
  }
240

241
  @Override
242
  public Class<Number> getClassOfT() { return Number.class; }
×
243
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc