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

Last change on this file since 804 was 566, checked in by wouter, 18 months ago

fixes to help translator

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