source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/profile/utilityspace/DiscreteValueSetUtilities.java@ 810

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

#287 adding @Nonnull in geniusweb code

File size: 3.8 KB
Line 
1package geniusweb.profile.utilityspace;
2
3import java.math.BigDecimal;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.HashSet;
7import java.util.Map;
8
9import org.eclipse.jdt.annotation.NonNull;
10
11import com.fasterxml.jackson.annotation.JsonAutoDetect;
12import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
13
14import geniusweb.issuevalue.DiscreteValue;
15import geniusweb.issuevalue.DiscreteValueSet;
16import geniusweb.issuevalue.Value;
17import geniusweb.issuevalue.ValueSet;
18
19/**
20 * Links a set of values to utilities for that value. Does not link to some
21 * issue so may need further checking when used for an actual issue. Constructor
22 * guarantees that
23 * <ul>
24 * <li>All values in the provided map (the utilities) are in [0,1]
25 * <li>All keys are proper {@link DiscreteValue}s
26 * </ul>
27 */
28@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
29public class DiscreteValueSetUtilities implements ValueSetUtilities {
30
31 private @NonNull HashMap<@NonNull DiscreteValue, @NonNull BigDecimal> valueUtilities = new HashMap<>();
32
33 @SuppressWarnings("unused") // for deserializer
34 private DiscreteValueSetUtilities() {
35
36 }
37
38 /**
39 * create new object based on the given mapping from values to utilities.
40 *
41 * @param valueUtils map with key {@link DiscreteValue}s and value a Double
42 * in the range [0,1].
43 * @throws NullPointerException if one of the args is null
44 * @throws IllegalArgumentException if values are not in range [0,1].
45 */
46 public DiscreteValueSetUtilities(
47 @NonNull Map<@NonNull DiscreteValue, @NonNull BigDecimal> valueUtils) {
48 if (valueUtils == null) {
49 throw new NullPointerException("valueUtils==null");
50 }
51 if (valueUtils.keySet().contains(null)) {
52 throw new NullPointerException(
53 "one of the keys in valueUtils is null");
54 }
55 /*#PY
56 * if True in (x==None or x<0 pr x>1 for x in self._valueUtils.values():
57 * raise ValueError("part weights must all be in [0,1]")
58 */
59 if (valueUtils.values().stream()
60 .anyMatch(v -> v == null || v.compareTo(BigDecimal.ZERO) < 0
61 || v.compareTo(BigDecimal.ONE) > 0)) {
62 throw new IllegalArgumentException(
63 "Weights in valueUtils must all be in [0,1]");
64 }
65 this.valueUtilities.putAll(valueUtils);
66
67 }
68
69 @Override
70 public @NonNull BigDecimal getUtility(@NonNull Value value) {
71 if (!valueUtilities.containsKey(value)) {
72 return BigDecimal.ZERO;
73 }
74 return valueUtilities.get(value);
75 }
76
77 /**
78 * @return copy of the value-utility pair map.
79 */
80 public @NonNull Map<@NonNull DiscreteValue, @NonNull BigDecimal> getUtilities() {
81 return Collections.unmodifiableMap(valueUtilities);
82 }
83
84 @Override
85 public String isFitting(ValueSet valueset) {
86 if (!(valueset instanceof DiscreteValueSet)) {
87 return "The utilities are for a discrete valueset but the given values are "
88 + valueset;
89 }
90 DiscreteValueSet discvalueset = (DiscreteValueSet) valueset;
91 if (!valueUtilities.keySet()
92 .equals(new HashSet<>(discvalueset.getValues())))
93 return "The values in the set " + valueset
94 + " do not match the values mapped to utilities "
95 + valueUtilities.keySet();
96 return null;
97 }
98
99 @Override
100 public @NonNull String toString() {
101 return "DiscreteValueSetUtilities" + valueUtilities;
102 }
103
104 @Override
105 public int hashCode() {
106 final int prime = 31;
107 int result = 1;
108 result = prime * result
109 + ((valueUtilities == null) ? 0 : valueUtilities.hashCode());
110 return result;
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj)
116 return true;
117 if (obj == null)
118 return false;
119 if (getClass() != obj.getClass())
120 return false;
121 DiscreteValueSetUtilities other = (DiscreteValueSetUtilities) obj;
122 if (valueUtilities == null) {
123 if (other.valueUtilities != null)
124 return false;
125 } else if (!valueUtilities.equals(other.valueUtilities))
126 return false;
127 return true;
128 }
129
130}
Note: See TracBrowser for help on using the repository browser.