Skip to content

Commit bb08a73

Browse files
HeikoKlareCopilot
andcommitted
Simplify and clean up Cursor.java (Win32)
Several small improvements to the Win32 Cursor implementation: - Replace double map-lookup and setHandleForZoomLevel() helper with a single HashMap.computeIfAbsent() call in win32_getHandle(). The helper method contained redundant null/containsKey guards that were already enforced by the call-site; both are now unnecessary. - Change win32_getHandle() return type from boxed Long to primitive long, consistent with Font.win32_getHandle(), Image.win32_getHandle() and Region.win32_getHandle(). All existing call sites already consumed the result as a long, so no callers required changes. - Fix latent equality bug in equals(): the previous code compared two Long wrapper objects with ==, which only gives correct results for cached values in [-128, 127]. Cursor handles are OS memory addresses and fall well outside that range. With the primitive return type the comparison is now a straightforward == on long values, which is both correct and avoids boxing. - Use Java 16+ instanceof pattern matching in equals() to eliminate the explicit cast after the type check. - Remove unnecessary clone() in ImageDataWithMaskCursorHandleProvider .validateMask(): the cloned ImageData was only ever read for its width/height fields, so the defensive copy was pure overhead. - Remove redundant 'static' modifier on the nested CursorHandleProvider interface declaration; nested interfaces are implicitly static. - Remove redundant 'final' modifier on the private static method getOSCursorIdFromStyle(); private static methods cannot be overridden. - Remove redundant 'final' modifier on the lambda-captured local variable in destroyHandlesExcept(); the variable is effectively final without the explicit keyword. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0f991b5 commit bb08a73

File tree

1 file changed

+14
-26
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+14
-26
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -316,25 +316,14 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
316316
*
317317
* @noreference This method is not intended to be referenced by clients.
318318
*/
319-
public static Long win32_getHandle (Cursor cursor, int zoom) {
319+
public static long win32_getHandle (Cursor cursor, int zoom) {
320320
if (cursor.isDisposed()) {
321321
return 0L;
322322
}
323-
int zoomWithPointerSizeScaleFactor = (int) (zoom * getPointerSizeScaleFactor());
324-
if (cursor.zoomLevelToHandle.get(zoomWithPointerSizeScaleFactor) != null) {
325-
return cursor.zoomLevelToHandle.get(zoomWithPointerSizeScaleFactor).getHandle();
326-
}
327-
328-
CursorHandle handle = cursor.cursorHandleProvider.createHandle(cursor.device, zoomWithPointerSizeScaleFactor);
329-
cursor.setHandleForZoomLevel(handle, zoomWithPointerSizeScaleFactor);
330-
331-
return cursor.zoomLevelToHandle.get(zoomWithPointerSizeScaleFactor).getHandle();
332-
}
333-
334-
private void setHandleForZoomLevel(CursorHandle handle, Integer zoom) {
335-
if (zoom != null && !zoomLevelToHandle.containsKey(zoom)) {
336-
zoomLevelToHandle.put(zoom, handle);
337-
}
323+
int scaledZoom = (int) (zoom * getPointerSizeScaleFactor());
324+
return cursor.zoomLevelToHandle
325+
.computeIfAbsent(scaledZoom, z -> cursor.cursorHandleProvider.createHandle(cursor.device, z))
326+
.getHandle();
338327
}
339328

340329
/**
@@ -389,8 +378,7 @@ void destroy () {
389378
@Override
390379
public boolean equals (Object object) {
391380
if (object == this) return true;
392-
if (!(object instanceof Cursor)) return false;
393-
Cursor cursor = (Cursor) object;
381+
if (!(object instanceof Cursor cursor)) return false;
394382
return device == cursor.device && win32_getHandle(this, DEFAULT_ZOOM) == win32_getHandle(cursor, DEFAULT_ZOOM);
395383
}
396384

@@ -406,7 +394,7 @@ public boolean equals (Object object) {
406394
*/
407395
@Override
408396
public int hashCode () {
409-
return win32_getHandle(this, DEFAULT_ZOOM).intValue();
397+
return (int) win32_getHandle(this, DEFAULT_ZOOM);
410398
}
411399

412400
/**
@@ -439,7 +427,7 @@ public String toString () {
439427
@Override
440428
void destroyHandlesExcept(Set<Integer> zoomLevels) {
441429
zoomLevelToHandle.entrySet().removeIf(entry -> {
442-
final Integer zoom = entry.getKey();
430+
Integer zoom = entry.getKey();
443431
if (!zoomLevels.contains(zoom) && zoom != DPIUtil.getZoomForAutoscaleProperty(DEFAULT_ZOOM)) {
444432
entry.getValue().destroy();
445433
return true;
@@ -492,7 +480,7 @@ void destroy() {
492480
}
493481
}
494482

495-
private static interface CursorHandleProvider {
483+
private interface CursorHandleProvider {
496484
CursorHandle createHandle(Device device, int zoom);
497485
}
498486

@@ -513,7 +501,7 @@ public CursorHandle createHandle(Device device, int zoom) {
513501
return new CustomCursorHandle(handle);
514502
}
515503

516-
private static final long getOSCursorIdFromStyle(int style) {
504+
private static long getOSCursorIdFromStyle(int style) {
517505
long lpCursorName = 0;
518506
switch (style) {
519507
case SWT.CURSOR_HAND:
@@ -663,15 +651,15 @@ public ImageDataWithMaskCursorHandleProvider(ImageData source, ImageData mask, i
663651
}
664652

665653
private void validateMask(ImageData source, ImageData mask) {
666-
ImageData testMask = mask == null ? null : (ImageData) mask.clone();
667-
if (testMask == null) {
654+
ImageData effectiveMask = mask;
655+
if (effectiveMask == null) {
668656
if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
669657
SWT.error(SWT.ERROR_NULL_ARGUMENT);
670658
}
671-
testMask = source.getTransparencyMask();
659+
effectiveMask = source.getTransparencyMask();
672660
}
673661
/* Check the bounds. Mask must be the same size as source */
674-
if (testMask.width != source.width || testMask.height != source.height) {
662+
if (effectiveMask.width != source.width || effectiveMask.height != source.height) {
675663
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
676664
}
677665
}

0 commit comments

Comments
 (0)