4
4
*/
5
5
package org .hibernate .internal .util .collections ;
6
6
7
- import java .lang .reflect .Array ;
8
7
import java .util .Arrays ;
9
8
import java .util .NoSuchElementException ;
10
9
import java .util .function .BiFunction ;
21
20
* @author Marco Belladelli
22
21
*/
23
22
public final class StandardStack <T > implements Stack <T > {
24
- private T [] elements ;
23
+
24
+ private Object [] elements ;
25
25
private int top = 0 ;
26
26
27
- private Class <T > type ;
27
+ public StandardStack () {
28
+ }
28
29
30
+ public StandardStack (T initialValue ) {
31
+ push ( initialValue );
32
+ }
33
+
34
+ /**
35
+ * @deprecated use the default constructor instead
36
+ */
37
+ @ Deprecated (forRemoval = true )
29
38
public StandardStack (Class <T > type ) {
30
- this .type = type ;
31
39
}
32
40
41
+ /**
42
+ * @deprecated use {@link #StandardStack(Object)} instead.
43
+ */
44
+ @ Deprecated (forRemoval = true )
33
45
public StandardStack (Class <T > type , T initial ) {
34
- this ( type );
35
46
push ( initial );
36
47
}
37
48
38
- @ SuppressWarnings ("unchecked" )
39
49
private void init () {
40
- elements = (T []) Array .newInstance ( type , 8 );
41
- type = null ;
50
+ elements = new Object [8 ];
42
51
}
43
52
44
53
@ Override
@@ -57,7 +66,7 @@ public T pop() {
57
66
if ( isEmpty () ) {
58
67
throw new NoSuchElementException ();
59
68
}
60
- T e = elements [--top ];
69
+ T e = ( T ) elements [--top ];
61
70
elements [top ] = null ;
62
71
return e ;
63
72
}
@@ -67,23 +76,23 @@ public T getCurrent() {
67
76
if ( isEmpty () ) {
68
77
return null ;
69
78
}
70
- return elements [top - 1 ];
79
+ return ( T ) elements [top - 1 ];
71
80
}
72
81
73
82
@ Override
74
83
public T peek (int offsetFromTop ) {
75
84
if ( isEmpty () ) {
76
85
return null ;
77
86
}
78
- return elements [top - offsetFromTop - 1 ];
87
+ return ( T ) elements [top - offsetFromTop - 1 ];
79
88
}
80
89
81
90
@ Override
82
91
public T getRoot () {
83
92
if ( isEmpty () ) {
84
93
return null ;
85
94
}
86
- return elements [0 ];
95
+ return ( T ) elements [0 ];
87
96
}
88
97
89
98
@ Override
@@ -107,14 +116,14 @@ public void clear() {
107
116
@ Override
108
117
public void visitRootFirst (Consumer <T > action ) {
109
118
for ( int i = 0 ; i < top ; i ++ ) {
110
- action .accept ( elements [i ] );
119
+ action .accept ( ( T ) elements [i ] );
111
120
}
112
121
}
113
122
114
123
@ Override
115
124
public <X > X findCurrentFirst (Function <T , X > function ) {
116
125
for ( int i = top - 1 ; i >= 0 ; i -- ) {
117
- final X result = function .apply ( elements [i ] );
126
+ final X result = function .apply ( ( T ) elements [i ] );
118
127
if ( result != null ) {
119
128
return result ;
120
129
}
@@ -125,7 +134,7 @@ public <X> X findCurrentFirst(Function<T, X> function) {
125
134
@ Override
126
135
public <X , Y > X findCurrentFirstWithParameter (Y parameter , BiFunction <T , Y , X > biFunction ) {
127
136
for ( int i = top - 1 ; i >= 0 ; i -- ) {
128
- final X result = biFunction .apply ( elements [i ], parameter );
137
+ final X result = biFunction .apply ( ( T ) elements [i ], parameter );
129
138
if ( result != null ) {
130
139
return result ;
131
140
}
@@ -138,4 +147,5 @@ private void grow() {
138
147
final int jump = ( oldCapacity < 64 ) ? ( oldCapacity + 2 ) : ( oldCapacity >> 1 );
139
148
elements = Arrays .copyOf ( elements , oldCapacity + jump );
140
149
}
150
+
141
151
}
0 commit comments