Skip to content

Commit 4154ae3

Browse files
committed
Switch to using AtomicReference/AtomicInteger
1 parent 17609f6 commit 4154ae3

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

modules/javafx.swing/src/main/java/javafx/embed/swing/InputMethodSupport.java

+24-20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.text.CharacterIterator;
4444
import java.util.ArrayList;
4545
import java.util.List;
46+
import java.util.concurrent.atomic.AtomicInteger;
47+
import java.util.concurrent.atomic.AtomicReference;
4648

4749
/**
4850
* A utility class containing the functions to support Input Methods
@@ -60,58 +62,59 @@ public InputMethodRequestsAdapter(javafx.scene.input.InputMethodRequests fxReque
6062

6163
@Override
6264
public Rectangle getTextLocation(TextHitInfo offset) {
63-
Point2D[] location = { new Point2D(0.0, 0.0) };
65+
AtomicReference<Point2D> location = new AtomicReference<>(new Point2D(0.0, 0.0));
6466
if (fxRequests != null) {
6567
PlatformImpl.runAndWait(() -> {
66-
location[0] = fxRequests.getTextLocation(offset.getInsertionIndex());
68+
location.set(fxRequests.getTextLocation(offset.getInsertionIndex()));
6769
});
6870
}
69-
return new Rectangle((int)location[0].getX(), (int)location[0].getY(), 0, 0);
71+
return new Rectangle((int)location.get().getX(), (int)location.get().getY(), 0, 0);
7072
}
7173

7274
@Override
7375
public TextHitInfo getLocationOffset(int x, int y) {
74-
int[] offset = { 0 };
76+
AtomicInteger offset = new AtomicInteger(0);
7577
if (fxRequests != null) {
7678
PlatformImpl.runAndWait(() -> {
77-
offset[0] = fxRequests.getLocationOffset(x, y);
79+
offset.set(fxRequests.getLocationOffset(x, y));
7880
});
7981
}
80-
return TextHitInfo.afterOffset(offset[0]);
82+
return TextHitInfo.afterOffset(offset.get());
8183
}
8284

8385
@Override
8486
public int getInsertPositionOffset() {
85-
int[] offset = { 0 };
87+
AtomicInteger offset = new AtomicInteger(0);
8688
if (fxRequests instanceof ExtendedInputMethodRequests) {
8789
PlatformImpl.runAndWait(() -> {
88-
offset[0] = ((ExtendedInputMethodRequests)fxRequests).getInsertPositionOffset();
90+
offset.set(((ExtendedInputMethodRequests)fxRequests).getInsertPositionOffset());
8991
});
9092
}
91-
return offset[0];
93+
return offset.get();
9294
}
9395

9496
@Override
9597
public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex, AttributedCharacterIterator.Attribute[] attributes) {
96-
String[] comitted = { null };
98+
AtomicReference<String> committed = new AtomicReference<>(null);
9799
if (fxRequests instanceof ExtendedInputMethodRequests) {
98100
PlatformImpl.runAndWait(() -> {
99-
comitted[0] = ((ExtendedInputMethodRequests)fxRequests).getCommittedText(beginIndex, endIndex);
101+
committed.set(((ExtendedInputMethodRequests)fxRequests).getCommittedText(beginIndex, endIndex));
100102
});
101103
}
102-
if (comitted[0] == null) comitted[0] = "";
103-
return new AttributedString(comitted[0]).getIterator();
104+
String text = committed.get();
105+
if (text == null) text = "";
106+
return new AttributedString(text).getIterator();
104107
}
105108

106109
@Override
107110
public int getCommittedTextLength() {
108-
int[] length = { 0 };
111+
AtomicInteger length = new AtomicInteger(0);
109112
if (fxRequests instanceof ExtendedInputMethodRequests) {
110113
PlatformImpl.runAndWait(() -> {
111-
length[0] = ((ExtendedInputMethodRequests)fxRequests).getCommittedTextLength();
114+
length.set(((ExtendedInputMethodRequests)fxRequests).getCommittedTextLength());
112115
});
113116
}
114-
return length[0];
117+
return length.get();
115118
}
116119

117120
@Override
@@ -122,14 +125,15 @@ public AttributedCharacterIterator cancelLatestCommittedText(AttributedCharacter
122125

123126
@Override
124127
public AttributedCharacterIterator getSelectedText(AttributedCharacterIterator.Attribute[] attributes) {
125-
String[] selected = { null };
128+
AtomicReference<String> selected = new AtomicReference<>(null);
126129
if (fxRequests != null) {
127130
PlatformImpl.runAndWait(() -> {
128-
selected[0] = fxRequests.getSelectedText();
131+
selected.set(fxRequests.getSelectedText());
129132
});
130133
}
131-
if (selected[0] == null) selected[0] = "";
132-
return new AttributedString(selected[0]).getIterator();
134+
String text = selected.get();
135+
if (text == null) text = "";
136+
return new AttributedString(text).getIterator();
133137
}
134138
}
135139

0 commit comments

Comments
 (0)