]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | package org.apache.thrift.protocol; | |
20 | ||
21 | import java.util.Arrays; | |
22 | ||
23 | /** | |
24 | * ShortStack is a short-specific Stack implementation written for the express | |
25 | * purpose of very fast operations on TCompactProtocol's field id stack. This | |
26 | * implementation performs at least 10x faster than java.util.Stack. | |
27 | */ | |
28 | class ShortStack { | |
29 | ||
30 | private short[] vector; | |
31 | ||
32 | /** Always points to the next location */ | |
33 | private int top = 0; | |
34 | ||
35 | public ShortStack(int initialCapacity) { | |
36 | vector = new short[initialCapacity]; | |
37 | } | |
38 | ||
39 | public short pop() { | |
40 | return vector[--top]; | |
41 | } | |
42 | ||
43 | public void push(short pushed) { | |
44 | if (vector.length == top) { | |
45 | grow(); | |
46 | } | |
47 | vector[top++] = pushed; | |
48 | } | |
49 | ||
50 | private void grow() { | |
51 | vector = Arrays.copyOf(vector, vector.length << 1); | |
52 | } | |
53 | ||
54 | public void clear() { | |
55 | top = 0; | |
56 | } | |
57 | ||
58 | @Override | |
59 | public String toString() { | |
60 | StringBuilder sb = new StringBuilder(); | |
61 | sb.append("<ShortStack vector:["); | |
62 | for (int i = 0; i < vector.length; i++) { | |
63 | boolean isTop = (i == (top - 1)); | |
64 | short value = vector[i]; | |
65 | if (i != 0) { | |
66 | sb.append(' '); | |
67 | } | |
68 | if (isTop) { | |
69 | sb.append(">>").append(value).append("<<"); | |
70 | } else { | |
71 | sb.append(value); | |
72 | } | |
73 | } | |
74 | sb.append("]>"); | |
75 | return sb.toString(); | |
76 | } | |
77 | } |