source: bidspace/src/main/java/geniusweb/bidspace/AllBidsList.java@ 1

Last change on this file since 1 was 1, checked in by bart, 5 years ago

Initial Release

File size: 1.7 KB
Line 
1package geniusweb.bidspace;
2
3import java.math.BigInteger;
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8import java.util.stream.Collectors;
9
10import geniusweb.issuevalue.Bid;
11import geniusweb.issuevalue.Domain;
12import geniusweb.issuevalue.Value;
13import tudelft.utilities.immutablelist.AbstractImmutableList;
14import tudelft.utilities.immutablelist.ImmutableList;
15import tudelft.utilities.immutablelist.Outer;
16
17/**
18 * A list containing all complete bids in the space. This is an
19 * {@link ImmutableList} so it can contain all bids without pre-computing them.
20 *
21 */
22public class AllBidsList extends AbstractImmutableList<Bid> {
23
24 private final List<String> issues;
25 private final Outer<? extends Value> allValuePermutations;
26
27 /**
28 * A list containing all bids in the space. This is an ImmutableList so it
29 * can contain all bids without pre-computing them.
30 *
31 */
32 public AllBidsList(Domain domain) {
33 if (domain == null)
34 throw new IllegalArgumentException("domain=null");
35 issues = new ArrayList<>(domain.getIssues());
36
37 List<ImmutableList<Value>> values = issues.stream()
38 .map(issue -> domain.getValues(issue))
39 .collect(Collectors.toList());
40
41 allValuePermutations = new Outer<Value>(values);
42 }
43
44 @Override
45 public Bid get(BigInteger index) {
46 ImmutableList<? extends Value> nextValues = allValuePermutations
47 .get(index);
48
49 Map<String, Value> issueValues = new HashMap<>();
50 for (int n = 0; n < issues.size(); n++) {
51 issueValues.put(issues.get(n),
52 nextValues.get(BigInteger.valueOf(n)));
53 }
54 return new Bid(issueValues);
55 }
56
57 @Override
58 public BigInteger size() {
59 return allValuePermutations.size();
60 }
61
62}
Note: See TracBrowser for help on using the repository browser.