Skip to content

Commit

Permalink
Code Cleanup - Use pattern matching for instanceof operator
Browse files Browse the repository at this point in the history
This replaces code of the form:
if (o instanceof <type>) {
  <type> t = (<type>) o;
  ...
}

with:
if (o instanceof <type> t) {
  ...
}
  • Loading branch information
ptziegler authored and azoitl committed Aug 19, 2023
1 parent 1c02ff4 commit ff0b9c3
Show file tree
Hide file tree
Showing 41 changed files with 68 additions and 139 deletions.
3 changes: 1 addition & 2 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/ChopboxAnchor.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public Point getReferencePoint() {
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof ChopboxAnchor) {
ChopboxAnchor other = (ChopboxAnchor) obj;
if (obj instanceof ChopboxAnchor other) {
return other.getOwner() == getOwner() && other.getBox().equals(getBox());
}
return false;
Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/EllipseAnchor.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public Point getLocation(Point reference) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof EllipseAnchor) {
EllipseAnchor other = (EllipseAnchor) o;
if (o instanceof EllipseAnchor other) {
return other.getOwner() == getOwner();
}
return false;
Expand Down
6 changes: 2 additions & 4 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ public void addKeyListener(KeyListener listener) {
*/
@Override
public void addLayoutListener(LayoutListener listener) {
if (layoutManager instanceof LayoutNotifier) {
LayoutNotifier notifier = (LayoutNotifier) layoutManager;
if (layoutManager instanceof LayoutNotifier notifier) {
notifier.listeners.add(listener);
} else
layoutManager = new LayoutNotifier(layoutManager, listener);
Expand Down Expand Up @@ -1391,8 +1390,7 @@ public void removeKeyListener(KeyListener listener) {
*/
@Override
public void removeLayoutListener(LayoutListener listener) {
if (layoutManager instanceof LayoutNotifier) {
LayoutNotifier notifier = (LayoutNotifier) layoutManager;
if (layoutManager instanceof LayoutNotifier notifier) {
notifier.listeners.remove(listener);
if (notifier.listeners.isEmpty())
layoutManager = notifier.realLayout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public LabeledContainer(Border border) {
private static LabeledBorder findLabeledBorder(Border border) {
if (border instanceof LabeledBorder)
return (LabeledBorder) border;
if (border instanceof CompoundBorder) {
CompoundBorder cb = (CompoundBorder) border;
if (border instanceof CompoundBorder cb) {
LabeledBorder labeled = findLabeledBorder(cb.getInnerBorder());
if (labeled == null)
labeled = findLabeledBorder(cb.getOuterBorder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public void addNotify() {
* @since 3.2
*/
public void addRoutingListener(RoutingListener listener) {
if (connectionRouter instanceof RoutingNotifier) {
RoutingNotifier notifier = (RoutingNotifier) connectionRouter;
if (connectionRouter instanceof RoutingNotifier notifier) {
notifier.listeners.add(listener);
} else
connectionRouter = new RoutingNotifier(connectionRouter, listener);
Expand Down Expand Up @@ -208,8 +207,7 @@ public void removeNotify() {
* @since 3.2
*/
public void removeRoutingListener(RoutingListener listener) {
if (connectionRouter instanceof RoutingNotifier) {
RoutingNotifier notifier = (RoutingNotifier) connectionRouter;
if (connectionRouter instanceof RoutingNotifier notifier) {
notifier.listeners.remove(listener);
if (notifier.listeners.isEmpty())
connectionRouter = notifier.realRouter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ public boolean equals(int w, int h) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof Dimension) {
Dimension d = (Dimension) o;
if (o instanceof Dimension d) {
return (d.width() == width && d.height() == height);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public Insets add(Insets insets) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof Insets) {
Insets i = (Insets) o;
if (o instanceof Insets i) {
return i.top == top && i.bottom == bottom && i.left == left && i.right == right;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public boolean equals(int x, int y) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof Point) {
Point p = (Point) o;
if (o instanceof Point p) {
return p.x() == x && p.y() == y;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public boolean containsProper(Dimension d) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof PrecisionDimension) {
PrecisionDimension d = (PrecisionDimension) o;
if (o instanceof PrecisionDimension d) {
return d.preciseWidth() == preciseWidth() && d.preciseHeight() == preciseHeight();
}
return super.equals(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public PrecisionPoint(Point p) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof PrecisionPoint) {
PrecisionPoint p = (PrecisionPoint) o;
if (o instanceof PrecisionPoint p) {
return p.preciseX() == preciseX() && p.preciseY() == preciseY();
}
return super.equals(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ private boolean containsPrecise(double x, double y) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof PrecisionRectangle) {
PrecisionRectangle rect = (PrecisionRectangle) o;
if (o instanceof PrecisionRectangle rect) {
return super.equals(o) && Math.abs(rect.preciseX() - preciseX()) < 0.000000001
&& Math.abs(rect.preciseY() - preciseY()) < 0.000000001
&& Math.abs(rect.preciseWidth() - preciseWidth()) < 0.000000001
Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Ray.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ long dotProductL(Ray r) {
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj instanceof Ray) {
Ray r = (Ray) obj;
if (obj instanceof Ray r) {
return x == r.x && y == r.y;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ public boolean equals(int x, int y, int width, int height) {
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof Rectangle) {
Rectangle r = (Rectangle) o;
if (o instanceof Rectangle r) {
return (x == r.x()) && (y == r.y()) && (width == r.width()) && (height == r.height());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ public boolean isParallelTo(Straight other) {
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof Straight)) {
if (!(other instanceof Straight otherStraight)) {
return false;
} else {
Straight otherStraight = (Straight) other;
return contains(otherStraight.position) && isParallelTo(otherStraight);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ public String toString() {
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj instanceof Vector) {
Vector r = (Vector) obj;
if (obj instanceof Vector r) {
return x == r.x && y == r.y;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public NodePair(Node n1, Node n2) {

@Override
public boolean equals(Object obj) {
if (obj instanceof NodePair) {
NodePair np = (NodePair) obj;
if (obj instanceof NodePair np) {
return np.n1 == n1 && np.n2 == n2;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public void put(Object key, Object value) {
map.put(key, value);
return;
}
if (existingValues instanceof ArrayList) {
ArrayList v = (ArrayList) existingValues;
if (existingValues instanceof ArrayList v) {
if (!v.contains(value))
v.add(value);
return;
Expand All @@ -53,8 +52,7 @@ public void put(Object key, Object value) {
public int remove(Object key, Object value) {
Object existingValues = map.get(key);
if (existingValues != null) {
if (existingValues instanceof ArrayList) {
ArrayList v = (ArrayList) existingValues;
if (existingValues instanceof ArrayList v) {
int result = v.indexOf(value);
if (result == -1)
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ private boolean canPerformAction() {
List parts = getSelectedObjects();
for (int i = 0; i < parts.size(); i++) {
Object o = parts.get(i);
if (!(o instanceof EditPart))
if (!(o instanceof EditPart part))
return false;
EditPart part = (EditPart) o;
if (!(part.getModel() instanceof LED))
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ public void run(IAction action) {
*/
@Override
public void selectionChanged(IAction action, ISelection selection) {
if (!(selection instanceof IStructuredSelection))
if (!(selection instanceof IStructuredSelection sel))
return;
IStructuredSelection sel = (IStructuredSelection) selection;
if (sel.size() != 1)
return;
selectedFile = (IFile) sel.getFirstElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public void setOffsetV(int offsetV) {
*/
@Override
public boolean equals(Object o) {
if (o instanceof FixedConnectionAnchor) {
FixedConnectionAnchor fa = (FixedConnectionAnchor) o;

if (o instanceof FixedConnectionAnchor fa) {
if (fa.leftToRight == this.leftToRight && fa.topDown == this.topDown && fa.offsetH == this.offsetH
&& fa.offsetV == this.offsetV && fa.getOwner() == this.getOwner()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public LayoutLabelProvider() {

@Override
public String getText(Object element) {
if (element instanceof Integer) {
Integer integer = (Integer) element;
if (element instanceof Integer integer) {
if (LAYOUT_SINGLE_ROW.intValue() == integer.intValue())
return LogicMessages.PropertyDescriptor_LogicFlowContainer_SingleColumnLayout;
if (LAYOUT_MULTI_ROW.intValue() == integer.intValue())
Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.gef/src/org/eclipse/gef/KeyStroke.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ public static KeyStroke getReleased(char character, int keyCode, int stateMask)
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof KeyStroke) {
KeyStroke stroke = (KeyStroke) obj;
if (obj instanceof KeyStroke stroke) {
return stroke.character == character && stroke.keyCode == keyCode && stroke.onPressed == onPressed
&& stroke.stateMask == stateMask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,7 @@ public void update(String propertyName) {
boolean checkChanged = (action.getStyle() == IAction.AS_CHECK_BOX)
&& (propertyName == null || propertyName.equals(Action.CHECKED));

if (widget instanceof ToolItem) {
ToolItem ti = (ToolItem) widget;
if (widget instanceof ToolItem ti) {
if (imageChanged) {
updateImages(true);
}
Expand All @@ -550,8 +549,7 @@ public void update(String propertyName) {
return;
}

if (widget instanceof MenuItem) {
MenuItem mi = (MenuItem) widget;
if (widget instanceof MenuItem mi) {
boolean isContextMenu = belongsToContextMenu(mi);

// We only install an accelerator if the menu item doesn't
Expand Down Expand Up @@ -610,8 +608,7 @@ public void update(String propertyName) {
return;
}

if (widget instanceof Button) {
Button button = (Button) widget;
if (widget instanceof Button button) {
if (imageChanged) {
if (updateImages(false)) {
// don't update text if it has an image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,9 @@ private boolean isPrimaryMarqueeSelectedEditPart(GraphicalEditPart editPart) {
*/
private boolean isSecondaryMarqueeSelectedEditPart(Collection directlyMarqueeSelectedEditParts, EditPart editPart) {
boolean included = false;
if (editPart instanceof ConnectionEditPart
if (editPart instanceof ConnectionEditPart connection
&& (marqueeBehavior == BEHAVIOR_NODES_CONTAINED_AND_RELATED_CONNECTIONS
|| marqueeBehavior == BEHAVIOR_NODES_TOUCHED_AND_RELATED_CONNECTIONS)) {
// connections are included, if related nodes are included
ConnectionEditPart connection = (ConnectionEditPart) editPart;
GraphicalEditPart source = (GraphicalEditPart) connection.getSource();
GraphicalEditPart target = (GraphicalEditPart) connection.getTarget();
boolean sourceIncludedInMarqueeSelection = directlyMarqueeSelectedEditParts.contains(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ public void run() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
ISelection s = event.getSelection();
if (!(s instanceof IStructuredSelection))
if (!(s instanceof IStructuredSelection selection))
return;
IStructuredSelection selection = (IStructuredSelection) s;
template = null;
if (selection != null && selection.size() == 1) {
Object obj = selection.getFirstElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ protected Command createPasteCommand() {
List selection = getSelectedObjects();
if (selection != null && selection.size() == 1) {
Object obj = selection.get(0);
if (obj instanceof GraphicalEditPart) {
GraphicalEditPart gep = (GraphicalEditPart) obj;
if (obj instanceof GraphicalEditPart gep) {
Object template = getClipboardContents();
if (template != null) {
CreationFactory factory = getFactory(template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public void removeDrawer(DrawerEditPart drawer) {
*/
@Override
public void init(IFigure figure) {
if (figure instanceof DrawerFigure) {
DrawerFigure drawer = (DrawerFigure) figure;
if (figure instanceof DrawerFigure drawer) {
if (drawer.isExpanded())
autoCollapse(drawer);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,8 @@ protected void setImageInFigure(Image image) {
}

private void traverseChildren(PaletteEntry parent, boolean add) {
if (!(parent instanceof PaletteContainer))
if (!(parent instanceof PaletteContainer container))
return;
PaletteContainer container = (PaletteContainer) parent;
traverseChildren(container.getChildren(), add);
}

Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.gef/src/org/eclipse/gef/ui/parts/TreeViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,8 @@ public void widgetDefaultSelected(SelectionEvent e) {
*/
@Override
public void reveal(EditPart part) {
if (!(part instanceof TreeEditPart))
if (!(part instanceof TreeEditPart treePart))
return;
TreeEditPart treePart = (TreeEditPart) part;
Tree tree = (Tree) getControl();
Widget widget = treePart.getWidget();
if (widget instanceof TreeItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void setFocus() {
protected PageRec doCreatePage(IWorkbenchPart part) {
// Try to get a custom command stack page.
Object obj = part.getAdapter(CommandStackInspectorPage.class);
if (obj instanceof IPage) {
IPage page = (IPage) obj;
if (obj instanceof IPage page) {
page.createControl(getPageBook());
return new PageRec(part, page);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public void dispose() {
*/
@Override
public Image getImage(Object o) {
if (o instanceof Command) {
Command command = (Command) o;
if (o instanceof Command command) {
// if(((DefaultCommandStack)stack).canUndoCommand(command))
// return yesIcon;
// if(((DefaultCommandStack)stack).canRedoCommand(command))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public EntityConnectionData(Object source, Object dest) {
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof EntityConnectionData)) {
if (!(obj instanceof EntityConnectionData that)) {
return false;
}
EntityConnectionData that = (EntityConnectionData) obj;
return (this.source.equals(that.source) && this.dest.equals(that.dest));
}

Expand Down
Loading

0 comments on commit ff0b9c3

Please sign in to comment.