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

Last change on this file since 817 was 810, checked in by wouter, 6 months ago

#287 adding @Nonnull in geniusweb code

File size: 3.4 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 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<? extends @NonNull Value> allValuePermutations;
50
51 /**
52 * A list containing all partial bids (this includes complete bids) in the
53 * space. This is an ImmutableList so it can contain all bids without
54 * pre-computing them.
55 *
56 * @param domain the {@link Domain} not null
57 */
58 @SuppressWarnings("unused")
59 public AllPartialBidsList(@NonNull Domain domain) {
60 if (domain == null)
61 throw new IllegalArgumentException("domain=null");
62 issues = new ArrayList<>(domain.getIssuesValues());
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 ImmutableList<? extends Value> nextValues = allValuePermutations
78 .get(index);
79
80 Map<String, Value> issueValues = new HashMap<>();
81 for (int n = 0; n < issues.size(); n = n + 1) {
82 Value value = nextValues.get(BigInteger.valueOf(n));
83 if (value != NoValue.NOVALUE) {
84 issueValues.put(issues.get(n), value);
85 }
86 }
87 return new Bid(issueValues);
88 }
89
90 @Override
91 public @NonNull BigInteger size() {
92 return allValuePermutations.size();
93 }
94
95}
96
97/**
98 * Extends a ValueSet so that the first element is #NOVALUE. Of course that is
99 * not a good value, we catch it to use it as 'value not set'.
100 *
101 */
102class ValueSetWithNull extends AbstractImmutableList<@NonNull Value>
103 implements ValueSet {
104
105 private ValueSet valueset;
106
107 public ValueSetWithNull(ValueSet valueset) {
108 this.valueset = valueset;
109 }
110
111 @Override
112 public @NonNull Value get(BigInteger index) {
113 if (BigInteger.ZERO.equals(index)) {
114 return NoValue.NOVALUE;
115 }
116 return valueset.get(index.subtract(BigInteger.ONE));
117 }
118
119 @Override
120 public @NonNull BigInteger size() {
121 return BigInteger.ONE.add(valueset.size());
122 }
123
124 @Override
125 public boolean contains(@NonNull Value value) {
126 if (value == NoValue.NOVALUE)
127 return true;
128 return valueset.contains(value);
129 }
130
131}
Note: See TracBrowser for help on using the repository browser.