Replace anonymous types with lambdas and method refs

This commit is contained in:
Chris Beams 2014-12-27 02:36:03 +01:00
parent 366f6246ce
commit d099100adf
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
14 changed files with 156 additions and 280 deletions

View File

@ -65,11 +65,7 @@ public class EthereumImpl implements Ethereum {
worldManager.loadBlockchain();
if (CONFIG.listenPort() > 0) {
Executors.newSingleThreadExecutor().submit(
new Runnable() {
public void run() {
peerServer.start(CONFIG.listenPort());
}
}
() -> peerServer.start(CONFIG.listenPort())
);
}
}

View File

@ -132,20 +132,17 @@ public abstract class FastByteComparisons {
static {
theUnsafe = (Unsafe) AccessController.doPrivileged(
new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return f.get(null);
} catch (NoSuchFieldException e) {
// It doesn't matter what we throw;
// it's swallowed in getBestComparer().
throw new Error();
} catch (IllegalAccessException e) {
throw new Error();
}
(PrivilegedAction<Object>) () -> {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return f.get(null);
} catch (NoSuchFieldException e) {
// It doesn't matter what we throw;
// it's swallowed in getBestComparer().
throw new Error();
} catch (IllegalAccessException e) {
throw new Error();
}
});

View File

@ -26,7 +26,6 @@ import java.math.BigInteger;
import java.security.SignatureException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import static com.google.common.base.Preconditions.checkNotNull;
@ -175,12 +174,7 @@ public class ECKeyTest {
final ECKey key = new ECKey();
for (byte i = 0; i < ITERATIONS; i++) {
final byte[] hash = HashUtil.sha3(new byte[]{i});
sigFutures.add(executor.submit(new Callable<ECKey.ECDSASignature>() {
@Override
public ECKey.ECDSASignature call() throws Exception {
return key.doSign(hash);
}
}));
sigFutures.add(executor.submit(() -> key.doSign(hash)));
}
List<ECKey.ECDSASignature> sigs = Futures.allAsList(sigFutures).get();
for (ECKey.ECDSASignature signature : sigs) {

View File

@ -823,16 +823,12 @@ public class BlockChainTable extends JFrame implements ActionListener {
transactionPanel.add(nonce, c);
JButton data = new JButton("Data");
data.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (transactionDataWindow == null)
transactionDataWindow = new TransactionData(blockchainTable);
transactionDataWindow.setData(transaction.getData());
transactionDataWindow.setVisible(true);
transactionDataWindow.highlightText(findText.getText(), painter);
}
data.addActionListener(event -> {
if (transactionDataWindow == null)
transactionDataWindow = new TransactionData(blockchainTable);
transactionDataWindow.setData(transaction.getData());
transactionDataWindow.setVisible(true);
transactionDataWindow.highlightText(findText.getText(), painter);
});
data.setFont(plain);
if (findText.getText().length() > 0 && ByteUtil.toHexString(transaction.getData()).contains(findText.getText

View File

@ -89,14 +89,12 @@ public class ConnectionConsoleWindow extends JFrame {
UIEthereumManager.ethereum.addListener(new EthereumListenerAdapter() {
@Override
public void trace(final String output) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(output);
textArea.append("\n");
SwingUtilities.invokeLater(() -> {
textArea.append(output);
textArea.append("\n");
if (autoScroll)
textArea.setCaretPosition(textArea.getText().length());
}
if (autoScroll)
textArea.setCaretPosition(textArea.getText().length());
});
}
});
@ -149,11 +147,7 @@ public class ConnectionConsoleWindow extends JFrame {
public static void main(String[] args) {
// Start all Swing applications on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ConnectionConsoleWindow(null).setVisible(true);
}
});
SwingUtilities.invokeLater(() -> new ConnectionConsoleWindow(null).setVisible(true));
}

View File

@ -70,11 +70,7 @@ class ContractCallDialog extends JDialog implements MessageAwareDialog {
@Override
public void focusLost(FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
populateContractDetails();
}
});
SwingUtilities.invokeLater(ContractCallDialog.this::populateContractDetails);
}
});

View File

@ -235,11 +235,9 @@ class PayOutDialog extends JDialog implements MessageAwareDialog {
inputMap.put(stroke, "ESCAPE");
rootPane.getActionMap().put("ESCAPE", actionListener);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setSize(500, 255);
setVisible(true);
}
SwingUtilities.invokeLater(() -> {
setSize(500, 255);
setVisible(true);
});
@ -250,26 +248,22 @@ class PayOutDialog extends JDialog implements MessageAwareDialog {
final PayOutDialog dialog = this;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
dialog.statusMsg.setForeground(Color.GREEN.darker().darker());
dialog.statusMsg.setText(text);
dialog.revalidate();
dialog.repaint();
}
SwingUtilities.invokeLater(() -> {
dialog.statusMsg.setForeground(Color.GREEN.darker().darker());
dialog.statusMsg.setText(text);
dialog.revalidate();
dialog.repaint();
});
}
public void alertStatusMsg(final String text) {
final PayOutDialog dialog = this;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
dialog.statusMsg.setForeground(Color.RED);
dialog.statusMsg.setText(text);
dialog.revalidate();
dialog.repaint();
}
SwingUtilities.invokeLater(() -> {
dialog.statusMsg.setForeground(Color.RED);
dialog.statusMsg.setText(text);
dialog.revalidate();
dialog.repaint();
});
}

View File

@ -81,10 +81,6 @@ public class PeerInfoWindow extends JFrame {
public static void main(String[] args) {
// Start all Swing applications on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PeerInfoWindow(null).setVisible(true);
}
});
SwingUtilities.invokeLater(() -> new PeerInfoWindow(null).setVisible(true));
}
}

View File

@ -32,12 +32,7 @@ public class PeersTableModel extends AbstractTableModel {
updater.scheduleAtFixedRate(new TimerTask() {
public void run() {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
updateModel();
}
}
PeersTableModel.this::updateModel
);
}
}, 0, 100);

View File

@ -64,11 +64,7 @@ public class PeersTableWindow extends JFrame {
PeersTableModel model = (PeersTableModel) table.getModel();
if (me.getClickCount() == 2) {
final PeersTableModel.PeerInfo peerInfo = model.getPeerInfo(row);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PeerInfoWindow(peerInfo).setVisible(true);
}
});
SwingUtilities.invokeLater(() -> new PeerInfoWindow(peerInfo).setVisible(true));
System.out.println(peerInfo);
}
}

View File

@ -190,10 +190,6 @@ public class ProgramPlayDialog extends JPanel implements ActionListener,
// final byte[] code = SerpentCompiler.compileAssemblyToMachine(asmCode);
final byte[] code = Hex.decode("7f4e616d65526567000000000000000000000000000000000000000000000000003057307f4e616d6552656700000000000000000000000000000000000000000000000000573360455760415160566000396000f20036602259604556330e0f600f5933ff33560f601e5960003356576000335700604158600035560f602b590033560f60365960003356573360003557600035335700");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(code, null, null);
}
});
SwingUtilities.invokeLater(() -> createAndShowGUI(code, null, null));
}
}

View File

@ -14,7 +14,6 @@ import org.slf4j.LoggerFactory;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
@ -277,21 +276,18 @@ public class SerpentEditor extends JFrame {
mainContentPane.getActionMap().put("OpenFileButton", openFile);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file = callFileChooser();
try {
if (file == null)
return;
String content = new Scanner(file).useDelimiter("\\Z").next();
codeArea.setText(content);
} catch (FileNotFoundException e1) {
logger.error(e1.getMessage(), e1);
} catch (java.util.NoSuchElementException e2) {
// don't worry it's just the file is empty
codeArea.setText("");
}
button.addActionListener(e -> {
File file = callFileChooser();
try {
if (file == null)
return;
String content = new Scanner(file).useDelimiter("\\Z").next();
codeArea.setText(content);
} catch (FileNotFoundException e1) {
logger.error(e1.getMessage(), e1);
} catch (java.util.NoSuchElementException e2) {
// don't worry it's just the file is empty
codeArea.setText("");
}
});
toolbar.add(button);
@ -331,32 +327,29 @@ public class SerpentEditor extends JFrame {
mainContentPane.getActionMap().put("OpenSaveButton", saveFile);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.addActionListener(e -> {
File file = null;
File file = null;
if (e.getModifiers() == (InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)) {
file = callFileChooser();
if (file == null)
return;
} else if (fileChooser == null
|| fileChooser.getSelectedFile() == null) {
file = callFileChooser();
if (file == null)
return;
} else {
file = fileChooser.getSelectedFile();
}
if (e.getModifiers() == (InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)) {
file = callFileChooser();
if (file == null)
return;
} else if (fileChooser == null
|| fileChooser.getSelectedFile() == null) {
file = callFileChooser();
if (file == null)
return;
} else {
file = fileChooser.getSelectedFile();
}
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file), 32768);
out.write(codeArea.getText());
out.close();
} catch (IOException e1) {
logger.error(e1.getMessage(), e1);
}
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file), 32768);
out.write(codeArea.getText());
out.close();
} catch (IOException e1) {
logger.error(e1.getMessage(), e1);
}
});
toolbar.add(button);
@ -384,12 +377,7 @@ public class SerpentEditor extends JFrame {
mainContentPane.getActionMap().put("CompileButton", compile);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
compileCode();
}
});
button.addActionListener(e -> compileCode());
toolbar.add(button);
}
@ -415,14 +403,12 @@ public class SerpentEditor extends JFrame {
mainContentPane.getActionMap().put("DeployButton", deploy);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
byte[] machineCode = prepareCodeForSend();
if (machineCode == null) return;
ContractSubmitDialog payOutDialog =
new ContractSubmitDialog((Frame) SwingUtilities.getAncestorOfClass(JFrame.class,
contentPane), machineCode);
}
button.addActionListener(e -> {
byte[] machineCode = prepareCodeForSend();
if (machineCode == null) return;
ContractSubmitDialog payOutDialog =
new ContractSubmitDialog((Frame) SwingUtilities.getAncestorOfClass(JFrame.class,
contentPane), machineCode);
});
toolbar.add(button);
}
@ -449,12 +435,10 @@ public class SerpentEditor extends JFrame {
mainContentPane.getActionMap().put("CallButton", call);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ContractCallDialog payOutDialog = new ContractCallDialog(
(Frame) SwingUtilities.getAncestorOfClass(
JFrame.class, contentPane));
}
button.addActionListener(e -> {
ContractCallDialog payOutDialog = new ContractCallDialog(
(Frame) SwingUtilities.getAncestorOfClass(
JFrame.class, contentPane));
});
toolbar.add(button);
}
@ -489,10 +473,6 @@ public class SerpentEditor extends JFrame {
public static void main(String[] args) {
// Start all Swing applications on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SerpentEditor(null).setVisible(true);
}
});
SwingUtilities.invokeLater(() -> new SerpentEditor(null).setVisible(true));
}
}

View File

@ -10,10 +10,6 @@ import org.ethereum.vm.Program;
import org.spongycastle.util.encoders.Hex;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
@ -70,18 +66,11 @@ public class StateExplorerWindow extends JFrame {
btnListAccounts.setBorderPainted(false);
btnListAccounts.setFocusPainted(false);
btnListAccounts.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnListAccounts.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (accountsListWindow == null)
accountsListWindow = new AccountsListWindow();
accountsListWindow.setVisible(true);
}
});
}
});
btnListAccounts.addItemListener(e -> SwingUtilities.invokeLater(() -> {
if (accountsListWindow == null)
accountsListWindow = new AccountsListWindow();
accountsListWindow.setVisible(true);
}));
horizontalBox.add(btnListAccounts);
@ -136,12 +125,10 @@ public class StateExplorerWindow extends JFrame {
l1.setAlignmentX(Component.CENTER_ALIGNMENT);
JComboBox cmbKey = new JComboBox(dataTypes);
cmbKey.setSelectedIndex(1);
cmbKey.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cmb = (JComboBox) e.getSource();
DataEncodingType t = DataEncodingType.getTypeFromString((String) cmb.getSelectedItem());
dataModel.setKeyEncoding(t);
}
cmbKey.addActionListener(e -> {
JComboBox cmb = (JComboBox) e.getSource();
DataEncodingType t = DataEncodingType.getTypeFromString((String) cmb.getSelectedItem());
dataModel.setKeyEncoding(t);
});
VBox1.add(l1);
VBox1.add(cmbKey);
@ -152,12 +139,10 @@ public class StateExplorerWindow extends JFrame {
l2.setAlignmentX(Component.CENTER_ALIGNMENT);
JComboBox cmbValue = new JComboBox(dataTypes);
cmbValue.setSelectedIndex(1);
cmbValue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cmb = (JComboBox) e.getSource();
DataEncodingType t = DataEncodingType.getTypeFromString((String) cmb.getSelectedItem());
dataModel.setValueEncoding(t);
}
cmbValue.addActionListener(e -> {
JComboBox cmb = (JComboBox) e.getSource();
DataEncodingType t = DataEncodingType.getTypeFromString((String) cmb.getSelectedItem());
dataModel.setValueEncoding(t);
});
VBox2.add(l2);
VBox2.add(cmbValue);
@ -319,10 +304,6 @@ public class StateExplorerWindow extends JFrame {
public static void main(String[] args) {
// Start all Swing applications on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StateExplorerWindow(null).setVisible(true);
}
});
SwingUtilities.invokeLater(() -> new StateExplorerWindow(null).setVisible(true));
}
}

View File

@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@ -108,20 +107,15 @@ public class ToolBar extends JFrame {
editorToggle.setBorderPainted(false);
editorToggle.setFocusPainted(false);
editorToggle.setCursor(new Cursor(Cursor.HAND_CURSOR));
editorToggle.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (serpentEditor == null)
serpentEditor = new SerpentEditor(ToolBar.this);
serpentEditor.setVisible(true);
}
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
serpentEditor.setVisible(false);
}
editorToggle.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(() -> {
if (serpentEditor == null)
serpentEditor = new SerpentEditor(ToolBar.this);
serpentEditor.setVisible(true);
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
serpentEditor.setVisible(false);
}
});
@ -133,20 +127,15 @@ public class ToolBar extends JFrame {
logToggle.setBorderPainted(false);
logToggle.setFocusPainted(false);
logToggle.setCursor(new Cursor(Cursor.HAND_CURSOR));
logToggle.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (connectionConsoleWindow == null)
connectionConsoleWindow = new ConnectionConsoleWindow(ToolBar.this);
connectionConsoleWindow.setVisible(true);
}
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
connectionConsoleWindow.setVisible(false);
}
logToggle.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(() -> {
if (connectionConsoleWindow == null)
connectionConsoleWindow = new ConnectionConsoleWindow(ToolBar.this);
connectionConsoleWindow.setVisible(true);
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
connectionConsoleWindow.setVisible(false);
}
});
@ -158,20 +147,15 @@ public class ToolBar extends JFrame {
peersToggle.setBorderPainted(false);
peersToggle.setFocusPainted(false);
peersToggle.setCursor(new Cursor(Cursor.HAND_CURSOR));
peersToggle.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (mainFrame == null)
mainFrame = new PeersTableWindow(ToolBar.this);
mainFrame.setVisible(true);
}
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
mainFrame.setVisible(false);
}
peersToggle.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(() -> {
if (mainFrame == null)
mainFrame = new PeersTableWindow(ToolBar.this);
mainFrame.setVisible(true);
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
mainFrame.setVisible(false);
}
});
@ -183,21 +167,16 @@ public class ToolBar extends JFrame {
chainToggle.setBorderPainted(false);
chainToggle.setFocusPainted(false);
chainToggle.setCursor(new Cursor(Cursor.HAND_CURSOR));
chainToggle.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
chainToggle.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(() -> {
if (blockchainWindow == null)
blockchainWindow = new BlockChainTable(ToolBar.this);
blockchainWindow.setVisible(true);
}
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
blockchainWindow.setVisible(false);
}
if (blockchainWindow == null)
blockchainWindow = new BlockChainTable(ToolBar.this);
blockchainWindow.setVisible(true);
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
blockchainWindow.setVisible(false);
}
});
@ -210,20 +189,15 @@ public class ToolBar extends JFrame {
walletToggle.setFocusPainted(false);
walletToggle.setCursor(new Cursor(Cursor.HAND_CURSOR));
walletToggle.addItemListener(
new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (walletWindow == null)
walletWindow = new WalletWindow(ToolBar.this);
walletWindow.setVisible(true);
}
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
walletWindow.setVisible(false);
}
e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(() -> {
if (walletWindow == null)
walletWindow = new WalletWindow(ToolBar.this);
walletWindow.setVisible(true);
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
walletWindow.setVisible(false);
}
}
);
@ -237,20 +211,15 @@ public class ToolBar extends JFrame {
stateExplorer.setFocusPainted(false);
stateExplorer.setCursor(new Cursor(Cursor.HAND_CURSOR));
stateExplorer.addItemListener(
new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (stateExplorerWindow == null)
stateExplorerWindow = new StateExplorerWindow(ToolBar.this);
stateExplorerWindow.setVisible(true);
}
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
stateExplorerWindow.setVisible(false);
}
e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(() -> {
if (stateExplorerWindow == null)
stateExplorerWindow = new StateExplorerWindow(ToolBar.this);
stateExplorerWindow.setVisible(true);
});
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
stateExplorerWindow.setVisible(false);
}
}
);
@ -268,10 +237,6 @@ public class ToolBar extends JFrame {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ToolBar().setVisible(true);
}
});
SwingUtilities.invokeLater(() -> new ToolBar().setVisible(true));
}
}