• 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

97.56
/src/main/java/org/apache/datasketches/common/ArrayOfBooleansSerDe.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 java.util.Objects;
23

24
import org.apache.datasketches.memory.Memory;
25
import org.apache.datasketches.memory.WritableMemory;
26

27
/**
28
 * Methods of serializing and deserializing arrays of Boolean as a bit array.
29
 *
30
 * @author Jon Malkin
31
 */
32
public class ArrayOfBooleansSerDe extends ArrayOfItemsSerDe<Boolean> {
1✔
33
  /**
34
   * Computes number of bytes needed for packed bit encoding of the array of booleans. Rounds
35
   * partial bytes up to return a whole number of bytes.
36
   *
37
   * @param arrayLength Number of items in the array to serialize
38
   * @return Number of bytes needed to encode the array
39
   */
40
  public static int computeBytesNeeded(final int arrayLength) {
41
    return (arrayLength >>> 3) + ((arrayLength & 0x7) > 0 ? 1 : 0);
1✔
42
  }
43

44
  @Override
45
  public byte[] serializeToByteArray(final Boolean item) {
46
    Objects.requireNonNull(item, "Item must not be null");
1✔
47
    final byte[] bytes = new byte[1];
1✔
48
    bytes[0] = (item) ? (byte)1 : 0;
1✔
49
    return bytes;
1✔
50
  }
51

52
  @Override
53
  public byte[] serializeToByteArray(final Boolean[] items) {
54
    Objects.requireNonNull(items, "Items must not be null");
1✔
55
    final int bytesNeeded = computeBytesNeeded(items.length);
1✔
56
    final byte[] bytes = new byte[bytesNeeded];
1✔
57
    final WritableMemory mem = WritableMemory.writableWrap(bytes);
1✔
58

59
    byte val = 0;
1✔
60
    for (int i = 0; i < items.length; ++i) {
1✔
61
      if (items[i]) {
1✔
62
        val |= 0x1 << (i & 0x7);
1✔
63
      }
64
      if ((i & 0x7) == 0x7) {
1✔
65
        mem.putByte(i >>> 3, val);
1✔
66
        val = 0;
1✔
67
      }
68
    }
69
    // write out any remaining values (if val=0, still good to be explicit)
70
    if ((items.length & 0x7) > 0) {
1✔
71
      mem.putByte(bytesNeeded - 1, val);
1✔
72
    }
73
    return bytes;
1✔
74
  }
75

76
  @Override
77
  public Boolean[] deserializeFromMemory(final Memory mem, final int numItems) {
78
    return deserializeFromMemory(mem, 0, numItems);
×
79
  }
80

81
  @Override
82
  public Boolean[] deserializeFromMemory(final Memory mem, final long offsetBytes, final int numItems) {
83
    Objects.requireNonNull(mem, "Memory must not be null");
1✔
84
    if (numItems <= 0) { return new Boolean[0]; }
1✔
85
    final int numBytes = computeBytesNeeded(numItems);
1✔
86
    Util.checkBounds(offsetBytes, numBytes, mem.getCapacity());
1✔
87
    final Boolean[] array = new Boolean[numItems];
1✔
88

89
    byte srcVal = 0;
1✔
90
    for (int i = 0, b = 0; i < numItems; ++i) {
1✔
91
      if ((i & 0x7) == 0x0) { // should trigger on first iteration
1✔
92
        srcVal = mem.getByte(offsetBytes + b++);
1✔
93
      }
94
      array[i] = ((srcVal >>> (i & 0x7)) & 0x1) == 1;
1✔
95
    }
96
    return array;
1✔
97
  }
98

99
  @Override
100
  public int sizeOf(final Boolean item) {
101
    Objects.requireNonNull(item, "Item must not be null");
1✔
102
    return computeBytesNeeded(1);
1✔
103
  }
104

105
  @Override //needs to override default due to the bit packing, which must be computed.
106
  public int sizeOf(final Boolean[] items) {
107
    Objects.requireNonNull(items, "Item must not be null");
1✔
108
    return computeBytesNeeded(items.length);
1✔
109
  }
110

111
  @Override
112
  public int sizeOf(final Memory mem, final long offsetBytes, final int numItems) {
113
    Objects.requireNonNull(mem, "Memory must not be null");
1✔
114
    return computeBytesNeeded(numItems);
1✔
115
  }
116

117
  @Override
118
  public String toString(final Boolean item) {
119
    if (item == null) { return "null"; }
1✔
120
    return item ? "true" : "false";
1✔
121
  }
122

123
  @Override
124
  public Class<Boolean> getClassOfT() { return Boolean.class; }
1✔
125
}
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