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

Last change on this file since 874 was 854, checked in by wouter, 7 months ago

#264 more fixes

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