aboutsummaryrefslogtreecommitdiff
path: root/comp2511/blackout/File.java
blob: df43e174f55f2a82c2f805ddd01912f01741d5bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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;
    }
}