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

Last change on this file since 519 was 519, checked in by wouter, 17 months ago

#169 added geniusweb sources hard copy in the project as I need to hack it a bit.

File size: 2.6 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.getIssues());
40
41 List<ImmutableList<Value>> values = issues.stream()
42 .map(issue -> new ValueSetWithNull(domain.getValues(issue)))
43 .collect(Collectors.toList());
44
45 allValuePermutations = new Outer<Value>(values);
46 }
47
48 @Override
49 public Bid get(BigInteger index) {
50 ImmutableList<? extends Value> nextValues = allValuePermutations
51 .get(index);
52
53 Map<String, Value> issueValues = new HashMap<>();
54 for (int n = 0; n < issues.size(); n++) {
55 Value value = nextValues.get(BigInteger.valueOf(n));
56 if (value != null) {
57 issueValues.put(issues.get(n), value);
58 }
59 }
60 return new Bid(issueValues);
61 }
62
63 @Override
64 public BigInteger size() {
65 return allValuePermutations.size();
66 }
67
68}
69
70/**
71 * Extends a ValueSet so that the first element is null. Of course null is not a
72 * good value, we catch it to use it as 'value not set'.
73 *
74 */
75class ValueSetWithNull extends AbstractImmutableList<Value>
76 implements ValueSet {
77
78 private ValueSet valueset;
79
80 public ValueSetWithNull(ValueSet valueset) {
81 this.valueset = valueset;
82 }
83
84 @Override
85 public Value get(BigInteger index) {
86 if (BigInteger.ZERO.equals(index)) {
87 return null;
88 }
89 return valueset.get(index.subtract(BigInteger.ONE));
90 }
91
92 @Override
93 public BigInteger size() {
94 return BigInteger.ONE.add(valueset.size());
95 }
96
97 @Override
98 public Boolean contains(Value value) {
99 if (value == null)
100 return true;
101 return valueset.contains(value);
102 }
103
104}
Note: See TracBrowser for help on using the repository browser.