source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/bidspace/AllPartialBidsList.java@ 749

Last change on this file since 749 was 744, checked in by wouter, 12 months ago

#254 PyProgram now throws TranslationException. Ignore broken test in tudunit-t

File size: 2.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 geniusweb.issuevalue.ValueSet;
14import tudelft.utilities.immutablelist.AbstractImmutableList;
15import tudelft.utilities.immutablelist.ImmutableList;
16import tudelft.utilities.immutablelist.Outer;
17
18/**
19 * A list containing all bids, both complete and partial, in the space. This is
20 * an {@link ImmutableList} so it contains all bids without pre-computing them.
21 * Constructing this is very cheap.
22 *
23 */
24public class AllPartialBidsList extends AbstractImmutableList<Bid> {
25
26 private final List<String> issues;
27 private final Outer<? extends Value> allValuePermutations;
28
29 /**
30 * A list containing all partial bids (this includes complete bids) in the
31 * space. This is an ImmutableList so it can contain all bids without
32 * pre-computing them.
33 *
34 * @param domain the {@link Domain}
35 */
36 public AllPartialBidsList(Domain domain) {
37 if (domain == null)
38 throw new IllegalArgumentException("domain=null");
39 issues = new ArrayList<>(domain.getIssuesValues());
40
41 /*#PY
42 * values = [ ValueSetWithNull(domain.getValues(issue))
43 * for issue in issues ]
44 */
45 List<ImmutableList<Value>> values = issues.stream()
46 .map(issue -> new ValueSetWithNull(domain.getValues(issue)))
47 .collect(Collectors.toList());
48
49 allValuePermutations = new Outer<Value>(values);
50 }
51
52 @Override
53 public Bid get(BigInteger index) {
54 ImmutableList<? extends Value> nextValues = allValuePermutations
55 .get(index);
56
57 Map<String, Value> issueValues = new HashMap<>();
58 for (int n = 0; n < issues.size(); n = n + 1) {
59 Value value = nextValues.get(BigInteger.valueOf(n));
60 if (value != null) {
61 issueValues.put(issues.get(n), value);
62 }
63 }
64 return new Bid(issueValues);
65 }
66
67 @Override
68 public BigInteger size() {
69 return allValuePermutations.size();
70 }
71
72}
73
74/**
75 * Extends a ValueSet so that the first element is null. Of course null is not a
76 * good value, we catch it to use it as 'value not set'.
77 *
78 */
79class ValueSetWithNull extends AbstractImmutableList<Value>
80 implements ValueSet {
81
82 private ValueSet valueset;
83
84 public ValueSetWithNull(ValueSet valueset) {
85 this.valueset = valueset;
86 }
87
88 @Override
89 public Value get(BigInteger index) {
90 if (BigInteger.ZERO.equals(index)) {
91 return null;
92 }
93 return valueset.get(index.subtract(BigInteger.ONE));
94 }
95
96 @Override
97 public BigInteger size() {
98 return BigInteger.ONE.add(valueset.size());
99 }
100
101 @Override
102 public Boolean contains(Value value) {
103 if (value == null)
104 return true;
105 return valueset.contains(value);
106 }
107
108}
Note: See TracBrowser for help on using the repository browser.