Changeset 4 for issuevalue/src
- Timestamp:
- 09/18/19 10:00:22 (5 years ago)
- Location:
- issuevalue/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
issuevalue/src/main/java/geniusweb/issuevalue/Bid.java
r1 r4 3 3 import java.util.Collections; 4 4 import java.util.HashMap; 5 import java.util.HashSet; 5 6 import java.util.Map; 6 7 import java.util.Set; … … 40 41 41 42 /** 43 * Makes partial bid with just 1 issue and value. 44 * 45 * @param issue the issue name 46 * @param value the {@link Value} 47 */ 48 public Bid(String issue, Value value) { 49 if (issue == null || value == null) { 50 throw new NullPointerException("issue and value must be not =null"); 51 } 52 53 issuevalues = new HashMap<>(); 54 issuevalues.put(issue, value); 55 } 56 57 /** 42 58 * @param issue 43 59 * @return the value for the given issue, or null if there is no value for … … 59 75 public Set<String> getIssues() { 60 76 return Collections.unmodifiableSet(issuevalues.keySet()); 77 } 78 79 /** 80 * Merges this partial bid with another partial bid. 81 * 82 * @param otherbid another partial bid. 83 * @return a bid with the combined values of both partial bids. 84 * @throws IllegalArgumentException if issues overlap. 85 */ 86 public Bid merge(Bid otherbid) { 87 Set<String> ourissues = new HashSet<>(issuevalues.keySet()); 88 ourissues.retainAll(otherbid.getIssues()); 89 if (!ourissues.isEmpty()) { 90 throw new IllegalArgumentException( 91 "Otherbid contains issues that are already set:" 92 + ourissues); 93 } 94 HashMap<String, Value> newvalues = new HashMap<>(issuevalues); 95 newvalues.putAll(otherbid.issuevalues); 96 return new Bid(newvalues); 97 61 98 } 62 99 -
issuevalue/src/test/java/geniusweb/issuevalue/BidTest.java
r1 r4 17 17 import com.fasterxml.jackson.databind.ObjectMapper; 18 18 19 import geniusweb.issuevalue.Bid;20 import geniusweb.issuevalue.DiscreteValue;21 import geniusweb.issuevalue.NumberValue;22 import geniusweb.issuevalue.Value;23 19 import tudelft.utilities.junit.GeneralTests; 24 20 … … 125 121 } 126 122 123 @Test 124 public void testMerge() { 125 Map<String, Value> issuevalues = new HashMap<>(); 126 issuevalues.put(ISSUE1, VALUE1); 127 Bid partial1 = new Bid(issuevalues); 128 129 issuevalues = new HashMap<>(); 130 issuevalues.put(ISSUE2, VALUE2); 131 Bid partial2 = new Bid(issuevalues); 132 133 Bid mergedbid = partial1.merge(partial2); 134 assertEquals(bidc, mergedbid); 135 } 136 137 @Test(expected = IllegalArgumentException.class) 138 public void testMergeIssuesOverlap() { 139 Map<String, Value> issuevalues = new HashMap<>(); 140 issuevalues.put(ISSUE1, VALUE1); 141 Bid partial1 = new Bid(issuevalues); 142 143 issuevalues = new HashMap<>(); 144 issuevalues.put(ISSUE1, VALUE2); 145 Bid partial2 = new Bid(issuevalues); 146 147 partial1.merge(partial2); 148 } 149 150 @Test 151 public void smokeTestConstructor2() { 152 new Bid("issue", new DiscreteValue("dss")); 153 } 154 155 @Test 156 public void smokeTestConstructor2b() { 157 new Bid("issue", new NumberValue("0.5")); 158 } 159 160 @Test(expected = NullPointerException.class) 161 public void smokeTestConstructor2Null1() { 162 new Bid(null, new DiscreteValue("dss")); 163 } 164 165 @Test(expected = NullPointerException.class) 166 public void smokeTestConstructor2Null2() { 167 new Bid("issue", null); 168 } 169 127 170 }
Note:
See TracChangeset
for help on using the changeset viewer.