Skip to content

Commit

Permalink
General code cleanup: (mostly) cosmetic optimizations
Browse files Browse the repository at this point in the history
- removed redundant code
- make use of modern code features where possible
- fixed some typos
- added more logging
  • Loading branch information
Argent77 committed Dec 1, 2024
1 parent 194cda9 commit 5e65c55
Show file tree
Hide file tree
Showing 210 changed files with 546 additions and 638 deletions.
6 changes: 3 additions & 3 deletions src/org/infinity/AppOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class AppOption {
lang = Arrays
.stream(Profile.Game.values())
.filter(Profile::isEnhancedEdition)
.map(g -> g.toString() + "=" + OptionsMenuItem.getDefaultGameLanguage())
.map(g -> g + "=" + OptionsMenuItem.getDefaultGameLanguage())
.collect(Collectors.joining(";"));
}
return lang;
Expand Down Expand Up @@ -431,7 +431,7 @@ public static List<AppOption> getOptionsByPrefs(String prefsNode) {

/** Discards any changes made to the options and resets them to their initial values. */
public static void revertAll() {
AppOption.getInstances().stream().forEach(AppOption::revert);
AppOption.getInstances().forEach(AppOption::revert);
}

/** Writes all options of this enum back to the persistent Preferences storage. */
Expand All @@ -449,7 +449,7 @@ public static void storePreferences(Collection<AppOption> collection) {
if (collection == null) {
collection = AppOption.getInstances();
}
collection.stream().forEach(AppOption::storeValue);
collection.forEach(AppOption::storeValue);
}

// /** Used internally to allow only supported types for the generic argument. */
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/check/ResourceUseChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private void checkSound(StringRef ref) {
* <p>
* This method can be called from several threads
*
* @param ref Reference to entry in string table that contains sound file name
* @param strref Reference to entry in string table that contains sound file name
*/
private void checkSound(int strref) {
if (strref >= 0) {
Expand Down
7 changes: 2 additions & 5 deletions src/org/infinity/check/StringDuplicatesChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import javax.swing.BorderFactory;
import javax.swing.JButton;
Expand Down Expand Up @@ -247,7 +244,7 @@ public List<List<Integer>> toList() {
for (final Map.Entry<String, List<Integer>> entry : strings.entrySet()) {
retVal.add(entry.getValue());
}
retVal.sort((a, b) -> a.get(0) - b.get(0));
retVal.sort(Comparator.comparingInt(a -> a.get(0)));

return retVal;
}
Expand Down
14 changes: 7 additions & 7 deletions src/org/infinity/check/StringValidationChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private void repairEntriesBackground() {

repairWorker = new SwingWorker<Couple<Integer, Integer>, Void>() {
@Override
protected Couple<Integer, Integer> doInBackground() throws Exception {
protected Couple<Integer, Integer> doInBackground() {
return repairEntries(repairWorker);
}
};
Expand Down Expand Up @@ -710,7 +710,7 @@ public static final class StringError implements Comparable<StringError> {
* @param offset Byte offset of malformed characters.
* @param length Number of malformed bytes.
* @param errorDesc A short error description.
* @throws IllegalArgumentException
* @throws IllegalArgumentException if one or more of the given parameters are invalid.
*/
public StringError(boolean isFemale, int strref, byte[] buffer, int offset, int length, String errorDesc)
throws IllegalArgumentException {
Expand Down Expand Up @@ -762,7 +762,7 @@ public String getErrorDesc() {
* @return Repaired string representation of the malformed bytes if successful, an empty string otherwise.
*/
public String getRepaired() {
String retVal = "";
StringBuilder retVal = new StringBuilder();
final byte[] data = Arrays.copyOfRange(buffer, offset, offset + length);
if (StringTable.getCharset().equals(StandardCharsets.UTF_8)) {
// Fixing UTF-8 charset
Expand All @@ -782,7 +782,7 @@ public String getRepaired() {
final CharBuffer outBuf = CharBuffer.allocate(maxCharLength);
final CoderResult cr = csd.decode(ByteBuffer.wrap(data), outBuf, true);
if (!cr.isError()) {
retVal = outBuf.flip().toString();
retVal = new StringBuilder(outBuf.flip().toString());
break;
}
}
Expand Down Expand Up @@ -831,7 +831,7 @@ public String getRepaired() {
final CoderResult ecr = cse.encode(CharBuffer.wrap(chars), ByteBuffer.allocate(chars.length * 4), true);
if (!ecr.isError()) {
// Add only if utf-8 code point defines a valid character in the local charset of the string table
retVal += new String(chars);
retVal.append(new String(chars));
} else {
isUtf = false;
}
Expand All @@ -851,15 +851,15 @@ public String getRepaired() {
if (!dcr.isError()) {
// Restoring original content
cb.flip();
retVal += cb.toString();
retVal.append(cb);
}
}
}
}
}
}

return retVal;
return retVal.toString();
}

/** Returns the character set used for encoding bytes into string data. */
Expand Down
7 changes: 3 additions & 4 deletions src/org/infinity/check/StructChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ private void search(ResourceEntry entry, AbstractStruct struct) {
// Checking signature and version fields
StructInfo info = FILE_INFO.get(entry.getExtension());
if (info != null) {
String sig = ((TextString) struct.getAttribute(AbstractStruct.COMMON_SIGNATURE)).toString();
String sig = struct.getAttribute(AbstractStruct.COMMON_SIGNATURE).toString();
if (info.isSignature(sig)) {
String ver = ((TextString) struct.getAttribute(AbstractStruct.COMMON_VERSION)).toString();
String ver = struct.getAttribute(AbstractStruct.COMMON_VERSION).toString();
if (!info.isVersion(ver)) {
// invalid version?
synchronized (table) {
Expand Down Expand Up @@ -489,8 +489,8 @@ private void showInViewer(Corruption corruption, boolean newWindow) {
return;
}

final ResourceEntry entry = corruption.getResourceEntry();
if (newWindow) {
final ResourceEntry entry = corruption.getResourceEntry();
final Resource res = ResourceFactory.getResource(entry);
final int offset = corruption.getOffset();
new ViewFrame(resultFrame, res);
Expand All @@ -502,7 +502,6 @@ private void showInViewer(Corruption corruption, boolean newWindow) {
}
}
} else {
final ResourceEntry entry = corruption.getResourceEntry();
final int offset = corruption.getOffset();
NearInfinity.getInstance().showResourceEntry(entry);
if (parent instanceof ViewFrame && parent.isVisible()) {
Expand Down
8 changes: 4 additions & 4 deletions src/org/infinity/datatype/AbstractBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AbstractBitmap<T> extends Datatype implements Editable, IsNumeric {
public final BiFunction<Long, T, String> formatterDefault = (value, item) -> {
String number = isShowAsHex() ? getHexValue(value) : value.toString();
if (item != null) {
return item.toString() + " - " + number;
return item + " - " + number;
} else {
return "Unknown - " + number;
}
Expand All @@ -54,7 +54,7 @@ public class AbstractBitmap<T> extends Datatype implements Editable, IsNumeric {
public final BiFunction<Long, T, String> formatterBitmap = (value, item) -> {
String number = isShowAsHex() ? getHexValue(value) : value.toString();
if (item != null) {
return item.toString() + " (" + number + ")";
return item + " (" + number + ")";
} else {
return "Unknown (" + number + ")";
}
Expand All @@ -67,7 +67,7 @@ public class AbstractBitmap<T> extends Datatype implements Editable, IsNumeric {
public final BiFunction<Long, T, String> formatterHashBitmapReverse = (value, item) -> {
String number = isShowAsHex() ? getHexValue(value) : value.toString();
if (item != null) {
return number + " - " + item.toString();
return number + " - " + item;
} else {
return number + " - Unknown - ";
}
Expand All @@ -80,7 +80,7 @@ public class AbstractBitmap<T> extends Datatype implements Editable, IsNumeric {
public final BiFunction<Long, T, String> formatterBitmapReverse = (value, item) -> {
String number = isShowAsHex() ? getHexValue(value) : value.toString();
if (item != null) {
return "(" + number + ") " + item.toString();
return "(" + number + ") " + item;
} else {
return "(" + number + ") Unknown";
}
Expand Down
5 changes: 1 addition & 4 deletions src/org/infinity/datatype/Bestiary.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,15 +615,12 @@ public Class<?> getColumnClass(int columnIndex) {
case 0:
return Boolean.class;
case 1:
case 5:
return Integer.class;
case 2:
return String.class;
case 3:
return String.class;
case 4:
return String.class;
case 5:
return Integer.class;
default:
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/ColorPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public enum Format {
private final int shiftGreen;
private final int shiftBlue;

private Format(int shiftRed, int shiftGreen, int shiftBlue) {
Format(int shiftRed, int shiftGreen, int shiftBlue) {
this.shiftRed = shiftRed;
this.shiftGreen = shiftGreen;
this.shiftBlue = shiftBlue;
Expand Down
6 changes: 3 additions & 3 deletions src/org/infinity/datatype/ColorValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public int getValue() {

@Override
public String toString() {
String retVal = "Color index " + Integer.toString(number);
String retVal = "Color index " + number;
String name = getColorName(number);
if (name != null) {
retVal += " (" + name + ")";
Expand Down Expand Up @@ -409,7 +409,7 @@ private void initEntries(int defaultWidth, int defaultHeight) {
// scanning range of colors
int maxValue = 255; // default size
if (colorValue.colorEntry != null) {
BufferedImage image = null;
BufferedImage image;
try {
image = new GraphicsResource(colorValue.colorEntry).getImage();
maxValue = Math.max(maxValue, image.getHeight() - 1);
Expand Down Expand Up @@ -459,7 +459,7 @@ private BufferedImage getFixedColor(BufferedImage colorRanges, int index, int wi

// Returns an image describing a random color or invalid color entry
private BufferedImage getVirtualColor(int index, int width, int height) {
BufferedImage retVal = null;
BufferedImage retVal;

Color invalidColor = new Color(0xe0e0e0);
boolean isRandom = colorValue.randomColors.containsKey(index);
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/DecNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public boolean equals(Object obj) {
}

/** Attempts to parse the specified object into a decimal or, optionally, hexadecimal number. */
public static long parseNumber(Object value, int size, boolean negativeAllowed, boolean hexAllowed) throws Exception {
public static long parseNumber(Object value, int size, boolean negativeAllowed, boolean hexAllowed) {
if (value == null) {
throw new NullPointerException();
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/infinity/datatype/InlineEditable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

public interface InlineEditable extends StructEntry {
/** The background color used for table cells. */
static final Color GRID_BACKGROUND = (UIManager.getColor("Table.focusCellBackground") != null)
Color GRID_BACKGROUND = (UIManager.getColor("Table.focusCellBackground") != null)
? UIManager.getColor("Table.focusCellBackground")
: Color.WHITE;

/** The border color used for table cells. */
static final Color GRID_BORDER = (UIManager.getColor("Table.gridColor") != null)
Color GRID_BORDER = (UIManager.getColor("Table.gridColor") != null)
? UIManager.getColor("Table.gridColor")
: Color.BLACK;

/** The default component used for the inline editor. */
static final JTextField DEFAULT_EDITOR = new JTextField() {
JTextField DEFAULT_EDITOR = new JTextField() {
{
setFont(Misc.getScaledFont(BrowserMenuBar.getInstance().getOptions().getScriptFont()));
setBorder(new LineBorder(GRID_BORDER, 1));
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/ItemTypeBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static TreeMap<Long, String> buildCategories() {
for (int row = 0, count = table.getRowCount(); row < count; row++) {
final String idxValue = table.get(row, 0);
final int radix = idxValue.startsWith("0x") ? 16 : 10;
String catName = "";
String catName;
try {
int idx = Integer.parseInt(idxValue, radix);
if (idx >= 0 && idx < CATEGORIES_ARRAY.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/MultiNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public ValueTableModel(int value, int bits, int numValues, String[] labels, bool
if (labels != null && i < labels.length && labels[i] != null) {
data[ATTRIBUTE][i] = labels[i];
} else {
data[ATTRIBUTE][i] = "Value " + Integer.toString(i + 1);
data[ATTRIBUTE][i] = "Value " + (i + 1);
}
data[VALUE][i] = bitRangeAsNumber(value, bits, i * bits, this.signed);
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/ResourceBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public RefEntry(long value, String ref, String search, List<Path> searchDirs) {
this.searchString = (search != null) ? search : "";
this.name = ref;
}
this.desc = String.format(FMT_REF_VALUE, getResourceName(), getSearchString(), Long.toString(value));
this.desc = String.format(FMT_REF_VALUE, getResourceName(), getSearchString(), value);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/TableBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TableBitmap(ByteBuffer buffer, int offset, int length, String name, Strin
}

private static String[] generateList(String tableName, int column, boolean normalize) {
String[] retVal = null;
String[] retVal;

Table2da table = Table2daCache.get(tableName);
if (table != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/TextBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public int read(ByteBuffer buffer, int offset) {
while (sb.length() < getSize() - text.length()) {
sb.append(' ');
}
text = text + sb.toString();
text = text + sb;
}

return offset + getSize();
Expand Down
4 changes: 2 additions & 2 deletions src/org/infinity/datatype/TextEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
* </ul>
*/
public final class TextEdit extends Datatype implements Editable, IsTextual {
public static enum EOLType {
public enum EOLType {
UNIX, WINDOWS
}

public static enum Align {
public enum Align {
LEFT, RIGHT, TOP, BOTTOM
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/datatype/UpdateListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface UpdateListener extends EventListener {
* @param event Contains associated data
* @return true if table data other than the current item has been changed, false otherwise.
*/
public boolean valueUpdated(UpdateEvent event);
boolean valueUpdated(UpdateEvent event);
}
2 changes: 1 addition & 1 deletion src/org/infinity/gui/BIFFEditorTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public enum State {

private final ImageIcon icon;

private State(Icons icon) {
State(Icons icon) {
this.icon = icon.getIcon();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/infinity/gui/BookmarkEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private void initData(List<Bookmark> bookmarks) {
int platformIdx = Math.max(cbPlatformModel.getIndexOf(Platform.OS.getCurrentOS()), 0);
cbPlatform.setSelectedIndex(platformIdx);
for (int idx = 0; idx < cbPlatformModel.getSize(); idx++) {
listBinPathModels.put(cbPlatformModel.getElementAt(idx), new DefaultListModel<Path>());
listBinPathModels.put(cbPlatformModel.getElementAt(idx), new DefaultListModel<>());
}
listBinPaths.setModel(getBinPathModel());
listBinPaths.addListSelectionListener(this);
Expand Down Expand Up @@ -496,7 +496,7 @@ private DefaultListModel<Path> getBinPathModel(Platform.OS os) {
private void addBinPathInteractive() {
JFileChooser fc = new JFileChooser(Profile.getGameRoot().toFile());
fc.setDialogTitle("Select game executable");
FileFilter exeFilter = null;
FileFilter exeFilter;
fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
if (cbPlatform.getSelectedItem() == Platform.OS.WINDOWS) {
exeFilter = new FileNameExtensionFilter("Executable Files", "exe", "lnk", "cmd", "bat", "ps1", "pif");
Expand Down
6 changes: 3 additions & 3 deletions src/org/infinity/gui/ButtonPopupWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ public void removeGlobalKeyStroke(Object key, KeyStroke keyStroke) {

protected void firePopupWindowListener(boolean becomeVisible) {
PopupWindowEvent event = null;
for (int i = 0, size = listeners.size(); i < size; i++) {
for (final PopupWindowListener listener : listeners) {
if (event == null) {
event = new PopupWindowEvent(this);
}
if (becomeVisible) {
listeners.get(i).popupWindowWillBecomeVisible(event);
listener.popupWindowWillBecomeVisible(event);
} else {
listeners.get(i).popupWindowWillBecomeInvisible(event);
listener.popupWindowWillBecomeInvisible(event);
}
}
}
Expand Down
Loading

0 comments on commit 5e65c55

Please sign in to comment.