summaryrefslogtreecommitdiff
path: root/p2/P2_GeneratePermutations/Allocation.java
blob: a5ab72fe3d951cbe6dbea48f8534eb4f23d71b14 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.util.*;

/**
 * The Allocation class represents an unmodifiable relief solution.
 * It provides methods to retrieve the total cost and total helped population
 * of the solution. The ordering of the regions in the solution determines
 * the population that can be helped.
 */
public class Allocation {

    private List<Region> regions;

    /**
     * Creates a new Allocation object representing the given regions.
     * @param regions the regions in the solution
     */
    private Allocation(List<Region> regions) {
        this.regions = new ArrayList<>(regions);
    }

    /**
     * Creates a new Allocation object with no regions in it.
     */
    public Allocation() {
        this(new ArrayList<>());
    }

    /**
     * Returns a copy of this allocation's regions.
     */
    public List<Region> getRegions() {
        return new ArrayList<>(regions);
    }

    /**
     * Returns a new Allocation with the contents of this allocation
     * and the passed in region added to it.
     * @param r Region to be added to the end of the new Allocation.
     * @return a new Allocation with r added to it.
     */
    public Allocation withRegion(Region r) {
        if (regions.contains(r)) {
            throw new IllegalArgumentException("Allocation already contains region " + r);
        }
        List<Region> newRegions = new ArrayList<>(regions);
        newRegions.add(r);
        return new Allocation(newRegions);
    }

    /**
     * Returns a new Allocation with the contents of this allocation
     * and the passed in region removed from it.
     * @param r Region to be removed from the new Allocation.
     * @return a new Allocation with r removed from it.
     */
    public Allocation withoutRegion(Region r) {
        if (!regions.contains(r)) {
            throw new IllegalArgumentException("Allocation doesn't contain region " + r);
        }
        List<Region> newRegions = new ArrayList<>(regions);
        newRegions.remove(r);
        return new Allocation(newRegions);
    }

    /**
     * Returns the number of regions in this Allocation.
     */
    public int size() {
        return regions.size();
    }

    /**
     * Calculates and returns the total population that can be helped
     * by this Allocation.
     * @return the total population that can be helped by this Allocation.
     */
    public int totalPeople() {
        int total = 0;
        for (Region r : regions) {
            total += r.getPopulation();
        }
        return total;
    }

    /**
     * Calculates and returns the combined cost of this Allocation.
     * @return the combined cost of this Allocation.
     */
    public double totalCost() {
        double total = 0;
        for (int i = 0; i < regions.size(); i++) {
            total += regions.get(i).getCost(i);
        }
        return total;
    }

    /**
     * Returns a String representation of an Allocation object in the format:
     * "[Region, ..., Region]" where each Region is in its string representation.
     * @return the String representation of an Allocation object
     */
    public String toString() {
        return regions.toString();
    }

    /**
     * Compares the specified object with this allocation for equality. Returns true if the
     * specified object is also an Allocation and the two Allocations have the same
     * collection of regions.
     * @param other object to be compared for equality with this allocation
     * @return true if the specified object is equal to this allocation
     */
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (!(other instanceof Allocation)) {
            return false;
        }
        Allocation otherAlloc = (Allocation)other;
        return this.regions.equals(otherAlloc.getRegions());
    }

    public int hashCode() {
        return regions.hashCode();
    }
}