1616 */
1717package com .alibaba .dubbo .common .utils ;
1818
19- import java .util .concurrent .atomic .AtomicInteger ;
19+ import java .util .concurrent .atomic .AtomicIntegerFieldUpdater ;
2020
21- /**
22- * AtomicPositiveInteger
23- */
2421public class AtomicPositiveInteger extends Number {
2522
2623 private static final long serialVersionUID = -3038533876489105940L ;
2724
28- private final AtomicInteger i ;
25+ private static final AtomicIntegerFieldUpdater <AtomicPositiveInteger > indexUpdater =
26+ AtomicIntegerFieldUpdater .newUpdater (AtomicPositiveInteger .class , "index" );
27+
28+ @ SuppressWarnings ("unused" )
29+ private volatile int index = 0 ;
2930
3031 public AtomicPositiveInteger () {
31- i = new AtomicInteger ();
3232 }
3333
3434 public AtomicPositiveInteger (int initialValue ) {
35- i = new AtomicInteger ( initialValue );
35+ indexUpdater . set ( this , initialValue );
3636 }
3737
3838 public final int getAndIncrement () {
39- for (; ; ) {
40- int current = i .get ();
41- int next = (current >= Integer .MAX_VALUE ? 0 : current + 1 );
42- if (i .compareAndSet (current , next )) {
43- return current ;
44- }
45- }
39+ return indexUpdater .getAndIncrement (this ) & Integer .MAX_VALUE ;
4640 }
4741
4842 public final int getAndDecrement () {
49- for (; ; ) {
50- int current = i .get ();
51- int next = (current <= 0 ? Integer .MAX_VALUE : current - 1 );
52- if (i .compareAndSet (current , next )) {
53- return current ;
54- }
55- }
43+ return indexUpdater .getAndDecrement (this ) & Integer .MAX_VALUE ;
5644 }
5745
5846 public final int incrementAndGet () {
59- for (; ; ) {
60- int current = i .get ();
61- int next = (current >= Integer .MAX_VALUE ? 0 : current + 1 );
62- if (i .compareAndSet (current , next )) {
63- return next ;
64- }
65- }
47+ return indexUpdater .incrementAndGet (this ) & Integer .MAX_VALUE ;
6648 }
6749
6850 public final int decrementAndGet () {
69- for (; ; ) {
70- int current = i .get ();
71- int next = (current <= 0 ? Integer .MAX_VALUE : current - 1 );
72- if (i .compareAndSet (current , next )) {
73- return next ;
74- }
75- }
51+ return indexUpdater .decrementAndGet (this ) & Integer .MAX_VALUE ;
7652 }
7753
7854 public final int get () {
79- return i .get () ;
55+ return indexUpdater .get (this ) & Integer . MAX_VALUE ;
8056 }
8157
8258 public final void set (int newValue ) {
8359 if (newValue < 0 ) {
8460 throw new IllegalArgumentException ("new value " + newValue + " < 0" );
8561 }
86- i .set (newValue );
62+ indexUpdater .set (this , newValue );
8763 }
8864
8965 public final int getAndSet (int newValue ) {
9066 if (newValue < 0 ) {
9167 throw new IllegalArgumentException ("new value " + newValue + " < 0" );
9268 }
93- return i .getAndSet (newValue );
69+ return indexUpdater .getAndSet (this , newValue ) & Integer . MAX_VALUE ;
9470 }
9571
9672 public final int getAndAdd (int delta ) {
9773 if (delta < 0 ) {
9874 throw new IllegalArgumentException ("delta " + delta + " < 0" );
9975 }
100- for (; ; ) {
101- int current = i .get ();
102- int next = (current >= Integer .MAX_VALUE - delta + 1 ? delta - 1 : current + delta );
103- if (i .compareAndSet (current , next )) {
104- return current ;
105- }
106- }
76+ return indexUpdater .getAndAdd (this , delta ) & Integer .MAX_VALUE ;
10777 }
10878
10979 public final int addAndGet (int delta ) {
11080 if (delta < 0 ) {
11181 throw new IllegalArgumentException ("delta " + delta + " < 0" );
11282 }
113- for (; ; ) {
114- int current = i .get ();
115- int next = (current >= Integer .MAX_VALUE - delta + 1 ? delta - 1 : current + delta );
116- if (i .compareAndSet (current , next )) {
117- return next ;
118- }
119- }
83+ return indexUpdater .addAndGet (this , delta ) & Integer .MAX_VALUE ;
12084 }
12185
12286 public final boolean compareAndSet (int expect , int update ) {
12387 if (update < 0 ) {
12488 throw new IllegalArgumentException ("update value " + update + " < 0" );
12589 }
126- return i .compareAndSet (expect , update );
90+ return indexUpdater .compareAndSet (this , expect , update );
12791 }
12892
12993 public final boolean weakCompareAndSet (int expect , int update ) {
13094 if (update < 0 ) {
13195 throw new IllegalArgumentException ("update value " + update + " < 0" );
13296 }
133- return i .weakCompareAndSet (expect , update );
97+ return indexUpdater .weakCompareAndSet (this , expect , update );
13498 }
13599
136- @ Override
137100 public byte byteValue () {
138- return i . byteValue ();
101+ return ( byte ) get ();
139102 }
140103
141- @ Override
142104 public short shortValue () {
143- return i . shortValue ();
105+ return ( short ) get ();
144106 }
145107
146- @ Override
147108 public int intValue () {
148- return i . intValue ();
109+ return get ();
149110 }
150111
151- @ Override
152112 public long longValue () {
153- return i . longValue ();
113+ return ( long ) get ();
154114 }
155115
156- @ Override
157116 public float floatValue () {
158- return i . floatValue ();
117+ return ( float ) get ();
159118 }
160119
161- @ Override
162120 public double doubleValue () {
163- return i . doubleValue ();
121+ return ( double ) get ();
164122 }
165123
166- @ Override
167124 public String toString () {
168- return i .toString ();
125+ return Integer .toString (get () );
169126 }
170127
171128 @ Override
172129 public int hashCode () {
173130 final int prime = 31 ;
174131 int result = 1 ;
175- result = prime * result + i . hashCode ();
132+ result = prime * result + get ();
176133 return result ;
177134 }
178135
@@ -181,7 +138,6 @@ public boolean equals(Object obj) {
181138 if (this == obj ) return true ;
182139 if (!(obj instanceof AtomicPositiveInteger )) return false ;
183140 AtomicPositiveInteger other = (AtomicPositiveInteger ) obj ;
184- return i . intValue () == other . i .intValue ();
141+ return intValue () == other .intValue ();
185142 }
186-
187- }
143+ }
0 commit comments