Skip to content

Commit

Permalink
Use pattern matching instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
aroelke committed Apr 30, 2021
1 parent 37b1e06 commit e53193d
Show file tree
Hide file tree
Showing 33 changed files with 125 additions and 157 deletions.
7 changes: 3 additions & 4 deletions src/main/java/editor/collection/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ public boolean equals(Object other)
return false;
if (other == this)
return true;
if (!(other instanceof InventoryEntry))
return false;
InventoryEntry o = (InventoryEntry)other;
return card.equals(o.card);
if (other instanceof InventoryEntry o)
return card.equals(o.card);
return false;
}

@Override
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/editor/collection/deck/CategorySpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,9 @@ public boolean equals(Object other)
return false;
if (other == this)
return true;
if (!(other instanceof CategorySpec))
return false;
CategorySpec o = (CategorySpec)other;
return name.equals(o.name) && color.equals(o.color) && filter.equals(o.filter)
&& blacklist.equals(o.blacklist) && whitelist.equals(o.whitelist);
if (other instanceof CategorySpec o)
return name.equals(o.name) && color.equals(o.color) && filter.equals(o.filter) && blacklist.equals(o.blacklist) && whitelist.equals(o.whitelist);
return false;
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/editor/collection/deck/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,9 @@ public boolean equals(Object other)
return false;
if (other == this)
return true;
if (!(other instanceof DeckEntry))
return false;
DeckEntry o = (DeckEntry)other;
return card.equals(o.card) && o.count == count && o.date.equals(date);
if (other instanceof DeckEntry o)
return card.equals(o.card) && o.count == count && o.date.equals(date);
return false;
}

@Override
Expand Down Expand Up @@ -637,10 +636,9 @@ public boolean equals(Object other)
return false;
if (other == this)
return true;
if (!(other instanceof Deck))
return false;
Deck o = (Deck)other;
return o.masterList.equals(masterList) && o.categories().equals(categories());
if (other instanceof Deck o)
return o.masterList.equals(masterList) && o.categories().equals(categories());
return false;
}

/**
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/editor/database/FormatConstraints.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,13 @@ public boolean equals(Object other)
return false;
if (other == this)
return true;
if (!(other instanceof FormatConstraints))
return false;
FormatConstraints o = (FormatConstraints)other;
return o.deckSize == deckSize &&
o.isExact == isExact &&
o.maxCopies == maxCopies &&
o.sideboardSize == sideboardSize &&
o.hasCommander == hasCommander;
if (other instanceof FormatConstraints o)
return o.deckSize == deckSize &&
o.isExact == isExact &&
o.maxCopies == maxCopies &&
o.sideboardSize == sideboardSize &&
o.hasCommander == hasCommander;
return false;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/editor/database/attributes/Loyalty.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public int compareTo(Loyalty other)
@Override
public boolean equals(Object other)
{
return other != null && (other == this || other instanceof Loyalty && value == ((Loyalty)other).value);
return other != null && (other == this || (other instanceof Loyalty o && value == o.value));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/editor/database/card/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public int compareName(Card other)
@Override
public boolean equals(Object other)
{
return other != null && (other == this || other instanceof Card && scryfallid().equals(((Card)other).scryfallid()));
return other != null && (other == this || (other instanceof Card o && scryfallid().equals(o.scryfallid())));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/database/symbol/ColorSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public Map<ManaType, Double> colorIntensity()
@Override
public int compareTo(ManaSymbol o)
{
if (o instanceof ColorSymbol)
return color.compareTo(((ColorSymbol)o).color);
if (o instanceof ColorSymbol s)
return color.compareTo(s.color);
else
return super.compareTo(o);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/database/symbol/HalfColorSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public Map<ManaType, Double> colorIntensity()
@Override
public int compareTo(ManaSymbol o)
{
if (o instanceof HalfColorSymbol)
return color.colorOrder(((HalfColorSymbol)o).color);
if (o instanceof HalfColorSymbol s)
return color.colorOrder(s.color);
else
return super.compareTo(o);
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/editor/database/symbol/HybridSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public class HybridSymbol extends ManaSymbol
/**
* Map mapping each pair of colors to their corresponding hybrid symbols.
*/
public static final Map<ManaType, Map<ManaType, HybridSymbol>> SYMBOLS = Collections.unmodifiableMap(
Arrays.stream(ManaType.colors()).collect(Collectors.toMap(Function.identity(), (m) -> Arrays.stream(
ManaType.colors()).filter((n) -> n != m).collect(Collectors.toMap(Function.identity(), (n) -> new HybridSymbol(m, n))))));
public static final Map<ManaType, Map<ManaType, HybridSymbol>> SYMBOLS = Collections.unmodifiableMap(Arrays.stream(ManaType.colors()).collect(
Collectors.toMap(
Function.identity(),
(m) -> Arrays.stream(ManaType.colors()).filter((n) -> n != m).collect(Collectors.toMap(Function.identity(), (n) -> new HybridSymbol(m, n)))
)
));

/**
* Get the HybridSymbol corresponding to the String, which is two color characters
Expand Down Expand Up @@ -90,11 +93,8 @@ public Map<ManaType, Double> colorIntensity()
@Override
public int compareTo(ManaSymbol o)
{
if (o instanceof HybridSymbol)
{
HybridSymbol other = (HybridSymbol)o;
return color1.compareTo(other.color1) * 10 + color2.compareTo(other.color2);
}
if (o instanceof HybridSymbol s)
return color1.compareTo(s.color1)*10 + color2.compareTo(s.color2);
else
return super.compareTo(o);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/database/symbol/PhyrexianSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public Map<ManaType, Double> colorIntensity()
@Override
public int compareTo(ManaSymbol o)
{
if (o instanceof PhyrexianSymbol)
return color.colorOrder(((PhyrexianSymbol)o).color);
if (o instanceof PhyrexianSymbol s)
return color.colorOrder(s.color);
else
return super.compareTo(o);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/database/symbol/TwobridSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public Map<ManaType, Double> colorIntensity()
@Override
public int compareTo(ManaSymbol o)
{
if (o instanceof TwobridSymbol)
return color.colorOrder(((TwobridSymbol)o).color);
if (o instanceof TwobridSymbol s)
return color.colorOrder(s.color);
else
return super.compareTo(o);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/database/symbol/VariableSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public Map<ManaType, Double> colorIntensity()
@Override
public int compareTo(ManaSymbol other)
{
if (other instanceof VariableSymbol)
return var - ((VariableSymbol)other).var;
if (other instanceof VariableSymbol s)
return var - s.var;
else
return super.compareTo(other);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/editor/database/version/DatabaseVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public boolean equals(Object other)
return false;
if (other == this)
return true;
if (!(other instanceof DatabaseVersion))
return false;
return compareTo((DatabaseVersion)other) == 0;
if (other instanceof DatabaseVersion o)
return compareTo((DatabaseVersion)other) == 0;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public int getSourceActions(JComponent c)
@Override
public void exportDone(JComponent source, Transferable data, int action)
{
if (data instanceof EntryTransferData)
if (data instanceof EntryTransferData d)
{
EntryTransferData d = (EntryTransferData)data;
switch (action)
{
case TransferHandler.MOVE:
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/editor/gui/display/CardTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public EmptyTableRowSorter(TableModel m)
@Override
public Comparator<?> getComparator(int column)
{
if (model instanceof CardTableModel)
if (model instanceof CardTableModel m)
{
boolean ascending = getSortKeys().get(0).getSortOrder() == SortOrder.ASCENDING;
CardAttribute attribute = ((CardTableModel)model).getColumnData(column);
CardAttribute attribute = m.getColumnData(column);
// Have to special-case P/T/L so they are always last if missing
return switch (attribute) {
case POWER, TOUGHNESS, LOYALTY -> (a, b) -> {
Expand Down Expand Up @@ -111,8 +111,7 @@ else if (!second.exists())
@Override
protected boolean useToString(int column)
{
return !(model instanceof CardTableModel &&
NO_STRING.contains(((CardTableModel)model).getColumnData(column))) && super.useToString(column);
return !(model instanceof CardTableModel m && NO_STRING.contains(m.getColumnData(column))) && super.useToString(column);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/gui/display/CardTableCellRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public CardTableCellRenderer()
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (table.getModel() instanceof CardTableModel)
if (table.getModel() instanceof CardTableModel m)
{
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
Border border = BorderFactory.createEmptyBorder(1, 1, 1, 1);
StringJoiner join = new StringJoiner(Card.FACE_SEPARATOR);
switch (((CardTableModel)table.getModel()).getColumnData(column))
switch (m.getColumnData(column))
{
case MANA_COST:
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/editor/gui/display/CardTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,14 @@ public void setValueAt(Object value, int row, int column)
switch (characteristics.get(column))
{
case COUNT:
if (value instanceof Integer)
list.set(list.get(row), (Integer)value);
if (value instanceof Integer i)
list.set(list.get(row), i);
else
throw new IllegalArgumentException("Illegal count value " + value);
break;
case CATEGORIES:
if (value instanceof IncludeExcludePanel)
{
IncludeExcludePanel iePanel = (IncludeExcludePanel)value;
if (value instanceof IncludeExcludePanel iePanel)
editor.editInclusion(iePanel.getIncluded(), iePanel.getExcluded());
}
else
throw new IllegalArgumentException("Illegal inclusion value " + value);
break;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/editor/gui/editor/InclusionCellEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ public Object getCellEditorValue()
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
if (table instanceof CardTable)
if (table instanceof CardTable cTable)
{
CardTable cTable = (CardTable)table;
iePanel = new IncludeExcludePanel(frame.getCategories().stream().sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).collect(Collectors.toList()), frame.getCardAt(cTable, row));
included = ((Collection<?>)value).stream().filter((o) -> o instanceof CategorySpec).map((o) -> (CategorySpec)o).collect(Collectors.toList());
if (!table.isRowSelected(row))
Expand All @@ -118,6 +117,6 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean
@Override
public boolean isCellEditable(EventObject eo)
{
return eo instanceof MouseEvent && ((MouseEvent)eo).getClickCount() > 1;
return eo instanceof MouseEvent m && m.getClickCount() > 1;
}
}
9 changes: 3 additions & 6 deletions src/main/java/editor/gui/filter/FilterGroupPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public void remove(FilterPanel<?> panel)
{
if (children.contains(panel))
{
if (panel instanceof FilterGroupPanel)
if (panel instanceof FilterGroupPanel g)
{
// Make this insert in place of the old group
filtersPanel.remove(panel);
children.remove(panel);
for (FilterPanel<?> child : ((FilterGroupPanel)panel).children)
for (FilterPanel<?> child : g.children)
add(child);
}
else if (children.size() > 1)
Expand All @@ -216,10 +216,7 @@ public void setContents(Filter filter)
{
FilterGroup group;
clear();
if (filter instanceof FilterGroup)
group = (FilterGroup)filter;
else
group = new FilterGroup(filter);
group = filter instanceof FilterGroup g ? g : new FilterGroup(filter);
modeBox.setSelectedItem(group.mode);
for (Filter child : group)
{
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/gui/filter/editor/ColorFilterPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public void setContents(ColorFilter filter)
@Override
public void setContents(FilterLeaf<?> filter) throws IllegalArgumentException
{
if (filter instanceof ColorFilter)
setContents((ColorFilter)filter);
if (filter instanceof ColorFilter f)
setContents(f);
else
throw new IllegalArgumentException("Illegal color filter " + filter.type());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public Filter filter()
@Override
public void setContents(FilterLeaf<?> filter) throws IllegalArgumentException
{
if (filter instanceof LegalityFilter)
setContents((LegalityFilter)filter);
if (filter instanceof LegalityFilter f)
setContents(f);
else
throw new IllegalArgumentException("Illegal legality filter " + filter.type());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public void setContents(ManaCostFilter filter)
@Override
public void setContents(FilterLeaf<?> filter) throws IllegalArgumentException
{
if (filter instanceof ManaCostFilter)
setContents((ManaCostFilter)filter);
if (filter instanceof ManaCostFilter f)
setContents(f);
else
throw new IllegalArgumentException("Illegal mana cost filter " + filter.type());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/gui/filter/editor/NumberFilterPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Filter filter()
@Override
public void setContents(FilterLeaf<?> filter) throws IllegalArgumentException
{
if (filter instanceof NumberFilter)
setContents((NumberFilter)filter);
if (filter instanceof NumberFilter f)
setContents(f);
else
throw new IllegalArgumentException("Illegal number filter " + filter.type());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ private void addItem(T value)
if (options.length > 0)
{
Object child = box.getAccessibleContext().getAccessibleChild(0);
if (child instanceof BasicComboPopup)
if (child instanceof BasicComboPopup popup)
SwingUtilities.invokeLater(() -> {
BasicComboPopup popup = (BasicComboPopup)child;
JScrollPane scrollPane = (JScrollPane)SwingUtilities.getAncestorOfClass(JScrollPane.class, popup.getList());

int popupWidth = popup.getList().getPreferredSize().width +
(options.length > box.getMaximumRowCount() ? scrollPane.getVerticalScrollBar().getPreferredSize().width : 0);
int popupWidth = popup.getList().getPreferredSize().width + (options.length > box.getMaximumRowCount() ? scrollPane.getVerticalScrollBar().getPreferredSize().width : 0);
scrollPane.setPreferredSize(new Dimension(Math.max(popupWidth, scrollPane.getPreferredSize().width), scrollPane.getPreferredSize().height));
scrollPane.setMaximumSize(scrollPane.getPreferredSize());
Point location = box.getLocationOnScreen();
Expand Down Expand Up @@ -185,7 +182,7 @@ public Filter filter()
* {@inheritDoc}
*
* @throws IllegalArgumentException if the given filter is not the same
* type as this OptionsFilterPanel or isn't even an {@link OptionsFilter}
* type as this OptionsFilterPanel or isn't even an {@link OptionsFilter}
*/
@SuppressWarnings("unchecked")
@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/gui/filter/editor/TextFilterPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public Filter filter()
@Override
public void setContents(FilterLeaf<?> filter) throws IllegalArgumentException
{
if (filter instanceof TextFilter)
setContents((TextFilter)filter);
if (filter instanceof TextFilter f)
setContents(f);
else
throw new IllegalArgumentException("Illegal text filter " + filter.type());
}
Expand Down

0 comments on commit e53193d

Please sign in to comment.