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

wuwen5 / hessian / 17803828826

15 Sep 2025 01:09AM UTC coverage: 69.283% (+0.2%) from 69.121%
17803828826

push

github

web-flow
perf(calendar): performance optimization of calendar serialization and deserialization (#44)

1830 of 2837 branches covered (64.5%)

Branch coverage included in aggregate %.

4226 of 5904 relevant lines covered (71.58%)

3.12 hits per line

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

93.52
hessian2-codec/src/main/java/io/github/wuwen5/hessian/util/IdentityIntMap.java
1
/*
2
 * Copyright (c) 2001-2008 Caucho Technology, Inc.  All rights reserved.
3
 *
4
 * The Apache Software License, Version 1.1
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in
15
 *    the documentation and/or other materials provided with the
16
 *    distribution.
17
 *
18
 * 3. The end-user documentation included with the redistribution, if
19
 *    any, must include the following acknowlegement:
20
 *       "This product includes software developed by the
21
 *        Caucho Technology (http://www.caucho.com/)."
22
 *    Alternately, this acknowlegement may appear in the software itself,
23
 *    if and wherever such third-party acknowlegements normally appear.
24
 *
25
 * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
26
 *    endorse or promote products derived from this software without prior
27
 *    written permission. For written permission, please contact
28
 *    info@caucho.com.
29
 *
30
 * 5. Products derived from this software may not be called "Resin"
31
 *    nor may "Resin" appear in their names without prior written
32
 *    permission of Caucho Technology.
33
 *
34
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37
 * DISCLAIMED.  IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
 *
46
 * @author Scott Ferguson
47
 */
48

49
package io.github.wuwen5.hessian.util;
50

51
/**
52
 * The IntMap provides a simple hashmap from keys to integers.  The API is
53
 * an abbreviation of the HashMap collection API.
54
 *
55
 * <p>The convenience of IntMap is avoiding all the silly wrapping of
56
 * integers.
57
 */
58
public class IdentityIntMap {
59
    /**
60
     * Encoding of a null entry.  Since NULL is equal to Integer.MIN_VALUE,
61
     * it's impossible to distinguish between the two.
62
     * Integer.MIN_VALUE + 1;
63
     */
64
    public static final int NULL = 0xdeadbeef;
65

66
    private Object[] keys;
67
    private int[] values;
68

69
    private int size;
70
    private int prime;
71

72
    public IdentityIntMap() {
2✔
73
        keys = new Object[256];
4✔
74
        values = new int[256];
4✔
75
        prime = 251;
3✔
76
        size = 0;
3✔
77
    }
1✔
78

79
    /**
80
     * Create a new IntMap.  Default size is 16.
81
     */
82
    public IdentityIntMap(int capacity) {
2✔
83
        keys = new Object[capacity];
4✔
84
        values = new int[capacity];
4✔
85

86
        prime = getBiggestPrime(keys.length);
6✔
87
        size = 0;
3✔
88
    }
1✔
89

90
    /**
91
     * Clear the hashmap.
92
     */
93
    public void clear() {
94

95
        for (int i = keys.length - 1; i >= 0; i--) {
10✔
96
            keys[i] = null;
5✔
97
            values[i] = 0;
5✔
98
        }
99

100
        size = 0;
3✔
101
    }
1✔
102
    /**
103
     * Returns the current number of entries in the map.
104
     */
105
    public final int size() {
106
        return size;
3✔
107
    }
108

109
    /**
110
     * Puts a new value in the property table with the appropriate flags
111
     */
112
    public final int get(Object key) {
113
        int hash = System.identityHashCode(key) % prime;
6✔
114

115
        while (true) {
116
            Object mapKey = keys[hash];
5✔
117

118
            if (mapKey == null) {
2✔
119
                return NULL;
2✔
120
            } else if (mapKey == key) {
3!
121
                return values[hash];
5✔
122
            }
123

124
            hash = (hash + 1) % prime;
×
125
        }
×
126
    }
127

128
    /**
129
     * Puts a new value in the property table with the appropriate flags
130
     */
131
    public final int put(Object key, int value, boolean isReplace) {
132
        int hash = Math.abs(System.identityHashCode(key) % prime);
7✔
133

134
        while (true) {
135
            Object testKey = keys[hash];
5✔
136

137
            if (testKey == null) {
2✔
138
                keys[hash] = key;
5✔
139
                values[hash] = value;
5✔
140

141
                size++;
6✔
142

143
                if (keys.length <= 4 * size) {
8✔
144
                    resize(4 * keys.length);
7✔
145
                }
146

147
                return value;
2✔
148
            } else if (key != testKey) {
3✔
149
                hash = (hash + 1) % prime;
8✔
150

151
            } else if (isReplace) {
2✔
152
                int old = values[hash];
5✔
153

154
                values[hash] = value;
5✔
155

156
                return old;
2✔
157
            } else {
158
                return values[hash];
5✔
159
            }
160
        }
1✔
161
    }
162

163
    /**
164
     * Removes a value in the property table.
165
     */
166
    public final void remove(Object key) {
167
        if (put(key, NULL, true) != NULL) {
7!
168
            size--;
6✔
169
        }
170
    }
1✔
171

172
    /**
173
     * Expands the property table
174
     */
175
    private void resize(int newSize) {
176
        Object[] preKeys = keys;
3✔
177
        int[] preValues = values;
3✔
178

179
        keys = new Object[newSize];
4✔
180
        values = new int[newSize];
4✔
181
        size = 0;
3✔
182

183
        prime = getBiggestPrime(keys.length);
6✔
184

185
        for (int i = preKeys.length - 1; i >= 0; i--) {
9✔
186
            Object key = preKeys[i];
4✔
187
            int value = preValues[i];
4✔
188

189
            if (key != null && value != NULL) {
5!
190
                put(key, value, true);
6✔
191
            }
192
        }
193
    }
1✔
194

195
    @Override
196
    public String toString() {
197
        StringBuilder sbuf = new StringBuilder();
4✔
198

199
        sbuf.append("IntMap[");
4✔
200
        boolean isFirst = true;
2✔
201

202
        for (int i = 0; i < keys.length; i++) {
9✔
203
            if (keys[i] != null) {
5✔
204
                if (!isFirst) {
2!
205
                    sbuf.append(", ");
×
206
                }
207

208
                isFirst = false;
2✔
209
                sbuf.append(keys[i]);
7✔
210
                sbuf.append(":");
4✔
211
                sbuf.append(values[i]);
7✔
212
            }
213
        }
214
        sbuf.append("]");
4✔
215

216
        return sbuf.toString();
3✔
217
    }
218

219
    public static final int[] PRIMES = {
120✔
220
        1, /* 1<< 0 = 1 */ 2, /* 1<< 1 = 2 */ 3, /* 1<< 2 = 4 */ 7, /* 1<< 3 = 8 */ 13, /* 1<< 4 = 16 */
221
        31, /* 1<< 5 = 32 */ 61, /* 1<< 6 = 64 */ 127, /* 1<< 7 = 128 */ 251, /* 1<< 8 = 256 */ 509, /* 1<< 9 = 512 */
222
        1021, /* 1<<10 = 1024 */ 2039, /* 1<<11 = 2048 */ 4093, /* 1<<12 = 4096 */ 8191, /* 1<<13 = 8192 */
223
        16381, /* 1<<14 = 16384 */ 32749, /* 1<<15 = 32768 */ 65521, /* 1<<16 = 65536 */ 131071, /* 1<<17 = 131072 */
224
        262139, /* 1<<18 = 262144 */ 524287, /* 1<<19 = 524288 */ 1048573, /* 1<<20 = 1048576 */
225
        2097143, /* 1<<21 = 2097152 */ 4194301, /* 1<<22 = 4194304 */ 8388593, /* 1<<23 = 8388608 */
226
        16777213, /* 1<<24 = 16777216 */ 33554393, /* 1<<25 = 33554432 */ 67108859, /* 1<<26 = 67108864 */
227
        134217689, /* 1<<27 = 134217728 */ 268435399, /* 1<<28 = 268435456 */
228
    };
229

230
    public static int getBiggestPrime(int value) {
231
        for (int i = PRIMES.length - 1; i >= 0; i--) {
9✔
232
            if (PRIMES[i] <= value) {
5✔
233
                return PRIMES[i];
4✔
234
            }
235
        }
236

237
        return 2;
2✔
238
    }
239
}
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

© 2026 Coveralls, Inc