package unsw.blackout; public class File { private String filename; private String contents; private int transmitted; private String target_id; /** * File represents a potentially in flight transmission. Transmitted should be <= contents.size(). */ File(String filename, String contents, int transmitted, String target_id) { this.filename = filename; this.contents = contents; this.transmitted = transmitted; this.target_id = target_id; } public String getFilename() { return filename; } public String getContents() { return contents; } public String getTransmittedContents() { return this.contents.substring(0, this.transmitted); } public int getTransmitted() { return transmitted; } public int getContentsSize() { return contents.length(); } public String getTargetId() { return this.target_id; } /** * Returns true if the file has fully transmitted, false otherwise. */ public boolean hasFullyTransmitted() { return this.transmitted == this.contents.length(); } /** * Returns true if the file contains quantum, false otherwise. */ public boolean isQuantum() { return this.contents.contains("quantum"); } /** * Add bytes to the transmitted content, clamps to the 0 and the filesize. */ public void addBytes(int bytes) { this.transmitted = Math.min(this.contents.length(), this.transmitted + bytes); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; File other = (File) obj; if (contents == null) { if (other.contents != null) return false; } else if (!contents.equals(other.contents)) return false; if (filename == null) { if (other.filename != null) return false; } else if (!filename.equals(other.filename)) return false; if (target_id == null) { if (other.target_id != null) return false; } else if (!target_id.equals(other.target_id)) return false; if (transmitted != other.transmitted) return false; return true; } }