Make payload classes final, extract Attachment in own class

This commit is contained in:
Manfred Karrer 2016-02-18 13:05:35 +01:00
parent 800013768b
commit 245d41f07d
9 changed files with 69 additions and 65 deletions

View file

@ -6,7 +6,7 @@ import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
// Util for comparing byte arrays // Util for comparing byte arrays
public class ByteArray implements Serializable { public final class ByteArray implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;

View file

@ -35,7 +35,7 @@ import java.security.spec.X509EncodedKeySpec;
* Same as KeyRing but with public keys only. * Same as KeyRing but with public keys only.
* Used to send public keys over the wire to other peer. * Used to send public keys over the wire to other peer.
*/ */
public class PubKeyRing implements Serializable { public final class PubKeyRing implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;

View file

@ -38,7 +38,7 @@ import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public class Dispute implements Serializable { public final class Dispute implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
private static final Logger log = LoggerFactory.getLogger(Dispute.class); private static final Logger log = LoggerFactory.getLogger(Dispute.class);

View file

@ -21,6 +21,7 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.inject.Inject; import com.google.inject.Inject;
import io.bitsquare.app.Log; import io.bitsquare.app.Log;
import io.bitsquare.arbitration.messages.*; import io.bitsquare.arbitration.messages.*;
import io.bitsquare.arbitration.payload.Attachment;
import io.bitsquare.btc.TradeWalletService; import io.bitsquare.btc.TradeWalletService;
import io.bitsquare.btc.WalletService; import io.bitsquare.btc.WalletService;
import io.bitsquare.btc.exceptions.TransactionVerificationException; import io.bitsquare.btc.exceptions.TransactionVerificationException;
@ -274,7 +275,7 @@ public class DisputeManager {
} }
// traders send msg to the arbitrator or arbitrator to 1 trader (trader to trader is not allowed) // traders send msg to the arbitrator or arbitrator to 1 trader (trader to trader is not allowed)
public DisputeCommunicationMessage sendDisputeDirectMessage(Dispute dispute, String text, ArrayList<DisputeCommunicationMessage.Attachment> attachments) { public DisputeCommunicationMessage sendDisputeDirectMessage(Dispute dispute, String text, ArrayList<Attachment> attachments) {
DisputeCommunicationMessage disputeCommunicationMessage = new DisputeCommunicationMessage(dispute.getTradeId(), DisputeCommunicationMessage disputeCommunicationMessage = new DisputeCommunicationMessage(dispute.getTradeId(),
dispute.getTraderPubKeyRing().hashCode(), dispute.getTraderPubKeyRing().hashCode(),
isTrader(dispute), isTrader(dispute),

View file

@ -30,7 +30,7 @@ import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
public class DisputeResult implements Serializable { public final class DisputeResult implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
private static final Logger log = LoggerFactory.getLogger(DisputeResult.class); private static final Logger log = LoggerFactory.getLogger(DisputeResult.class);

View file

@ -18,6 +18,7 @@
package io.bitsquare.arbitration.messages; package io.bitsquare.arbitration.messages;
import io.bitsquare.app.Version; import io.bitsquare.app.Version;
import io.bitsquare.arbitration.payload.Attachment;
import io.bitsquare.p2p.NodeAddress; import io.bitsquare.p2p.NodeAddress;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
@ -26,9 +27,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -180,58 +179,4 @@ public final class DisputeCommunicationMessage extends DisputeMessage {
", attachments=" + attachments + ", attachments=" + attachments +
'}'; '}';
} }
///////////////////////////////////////////////////////////////////////////////////////////
// Static classes
///////////////////////////////////////////////////////////////////////////////////////////
public static class Attachment implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
private static final Logger log = LoggerFactory.getLogger(Attachment.class);
private final byte[] bytes;
private final String fileName;
public Attachment(String fileName, byte[] bytes) {
this.fileName = fileName;
this.bytes = bytes;
}
public byte[] getBytes() {
return bytes;
}
public String getFileName() {
return fileName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Attachment)) return false;
Attachment that = (Attachment) o;
if (!Arrays.equals(bytes, that.bytes)) return false;
return !(fileName != null ? !fileName.equals(that.fileName) : that.fileName != null);
}
@Override
public int hashCode() {
int result = bytes != null ? Arrays.hashCode(bytes) : 0;
result = 31 * result + (fileName != null ? fileName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Attachment{" +
"description=" + fileName +
", data=" + Arrays.toString(bytes) +
'}';
}
}
} }

View file

@ -0,0 +1,57 @@
package io.bitsquare.arbitration.payload;
import io.bitsquare.app.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Arrays;
public final class Attachment implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
private static final Logger log = LoggerFactory.getLogger(Attachment.class);
private final byte[] bytes;
private final String fileName;
public Attachment(String fileName, byte[] bytes) {
this.fileName = fileName;
this.bytes = bytes;
}
public byte[] getBytes() {
return bytes;
}
public String getFileName() {
return fileName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Attachment)) return false;
Attachment that = (Attachment) o;
if (!Arrays.equals(bytes, that.bytes)) return false;
return !(fileName != null ? !fileName.equals(that.fileName) : that.fileName != null);
}
@Override
public int hashCode() {
int result = bytes != null ? Arrays.hashCode(bytes) : 0;
result = 31 * result + (fileName != null ? fileName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Attachment{" +
"description=" + fileName +
", data=" + Arrays.toString(bytes) +
'}';
}
}

View file

@ -22,7 +22,7 @@ import io.bitsquare.app.Version;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
public class RawInput implements Serializable { public final class RawInput implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility. // That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION; private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;

View file

@ -23,6 +23,7 @@ import de.jensd.fx.fontawesome.AwesomeIcon;
import io.bitsquare.arbitration.Dispute; import io.bitsquare.arbitration.Dispute;
import io.bitsquare.arbitration.DisputeManager; import io.bitsquare.arbitration.DisputeManager;
import io.bitsquare.arbitration.messages.DisputeCommunicationMessage; import io.bitsquare.arbitration.messages.DisputeCommunicationMessage;
import io.bitsquare.arbitration.payload.Attachment;
import io.bitsquare.common.UserThread; import io.bitsquare.common.UserThread;
import io.bitsquare.common.crypto.KeyRing; import io.bitsquare.common.crypto.KeyRing;
import io.bitsquare.gui.common.view.ActivatableView; import io.bitsquare.gui.common.view.ActivatableView;
@ -83,7 +84,7 @@ public class TraderDisputeView extends ActivatableView<VBox, Void> {
private final ContractPopup contractPopup; private final ContractPopup contractPopup;
private final TradeDetailsPopup tradeDetailsPopup; private final TradeDetailsPopup tradeDetailsPopup;
private final List<DisputeCommunicationMessage.Attachment> tempAttachments = new ArrayList<>(); private final List<Attachment> tempAttachments = new ArrayList<>();
private TableView<Dispute> disputesTable; private TableView<Dispute> disputesTable;
private Dispute selectedDispute; private Dispute selectedDispute;
@ -273,7 +274,7 @@ public class TraderDisputeView extends ActivatableView<VBox, Void> {
try (InputStream inputStream = url.openStream()) { try (InputStream inputStream = url.openStream()) {
byte[] filesAsBytes = ByteStreams.toByteArray(inputStream); byte[] filesAsBytes = ByteStreams.toByteArray(inputStream);
if (filesAsBytes.length <= Connection.getMaxMsgSize()) { if (filesAsBytes.length <= Connection.getMaxMsgSize()) {
tempAttachments.add(new DisputeCommunicationMessage.Attachment(result.getName(), filesAsBytes)); tempAttachments.add(new Attachment(result.getName(), filesAsBytes));
inputTextArea.setText(inputTextArea.getText() + "\n[Attachment " + result.getName() + "]"); inputTextArea.setText(inputTextArea.getText() + "\n[Attachment " + result.getName() + "]");
} else { } else {
new Popup().warning("The max. allowed file size is " + maxSizeInKB + " kB.").show(); new Popup().warning("The max. allowed file size is " + maxSizeInKB + " kB.").show();
@ -292,7 +293,7 @@ public class TraderDisputeView extends ActivatableView<VBox, Void> {
} }
} }
private void onOpenAttachment(DisputeCommunicationMessage.Attachment attachment) { private void onOpenAttachment(Attachment attachment) {
FileChooser fileChooser = new FileChooser(); FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save file to disk"); fileChooser.setTitle("Save file to disk");
fileChooser.setInitialFileName(attachment.getFileName()); fileChooser.setInitialFileName(attachment.getFileName());