source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/issuevalue/Bid.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: 4.0 KB
Line 
1package geniusweb.issuevalue;
2
3import java.util.Collections;
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.Map;
7import java.util.Set;
8
9import org.eclipse.jdt.annotation.NonNull;
10
11import com.fasterxml.jackson.annotation.JsonAutoDetect;
12import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
13import com.fasterxml.jackson.annotation.JsonCreator;
14import com.fasterxml.jackson.annotation.JsonProperty;
15
16/**
17 * Contains a (possibly partial) bid. Basically a map. immutable, thread safe.
18 */
19@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
20public class Bid {
21 // not immutable and explicit HashMap for better serializability, must be
22 // kept immutable..
23 private final @NonNull Map<@NonNull String, @NonNull Value> issuevalues;
24
25 /**
26 *
27 * @param issuevalues a map of issue, Value pairs. The String is the issue
28 * name. A partial bid may not have values for all
29 * issues.
30 */
31 @JsonCreator
32 public Bid(
33 @JsonProperty("issuevalues") @NonNull Map<@NonNull String, @NonNull Value> issuevalues) {
34 if (issuevalues == null) {
35 throw new NullPointerException("issuevalues=null");
36 }
37 for (final @NonNull String issue : issuevalues.keySet()) {
38 if (issuevalues.get(issue) == null) {
39 throw new IllegalArgumentException(
40 "value of issue " + issue + " can not be null");
41 }
42 }
43 this.issuevalues = new HashMap<String, Value>(issuevalues);
44 }
45
46 /**
47 * Makes partial bid with just 1 issue and value.
48 *
49 * @param issue the issue name
50 * @param value the {@link Value}
51 */
52 public Bid(@NonNull String issue, @NonNull Value value) {
53 if (issue == null || value == null) {
54 throw new NullPointerException("issue and value must be not =null");
55 }
56
57 issuevalues = new HashMap<>();
58 issuevalues.put(issue, value);
59 }
60
61 /**
62 * @param issue name of the issue
63 * @return the value for the given issue, or null if there is no value for
64 * the given issue.
65 */
66 public @NonNull Value getValue(@NonNull String issue) {
67 return issuevalues.get(issue);
68 }
69
70 /**
71 * @param issue name of the issue (not null)
72 * @return true iff the bid contains a value for the given issue.
73 */
74 public boolean containsIssue(@NonNull String issue) {
75 return issuevalues.containsKey(issue);
76 }
77
78 public @NonNull Set<@NonNull String> getIssues() {
79 return Collections.unmodifiableSet(issuevalues.keySet());
80 }
81
82 /**
83 * Merges this partial bid with another partial bid.
84 *
85 * @param otherbid another partial bid (not null).
86 * @return a bid with the combined values of both partial bids.
87 * @throws IllegalArgumentException if issues overlap.
88 */
89 public @NonNull Bid merge(final @NonNull Bid otherbid) {
90 @NonNull
91 Set<@NonNull String> ourissues = new HashSet<>(issuevalues.keySet());
92 ourissues.retainAll(otherbid.getIssues());
93 if (!ourissues.isEmpty()) {
94 throw new IllegalArgumentException(
95 "Otherbid contains issues that are already set:"
96 + ourissues);
97 }
98 @NonNull
99 HashMap<@NonNull String, @NonNull Value> newvalues = new HashMap<>(
100 issuevalues);
101 newvalues.putAll(otherbid.issuevalues);
102 return new Bid(newvalues);
103 }
104
105 /**
106 *
107 * @return (unmodifyable) map with all issues and values in the bid.
108 */
109 public @NonNull Map<@NonNull String, @NonNull Value> getIssueValues() {
110 return Collections.unmodifiableMap(issuevalues);
111 }
112
113 @Override
114 public @NonNull String toString() {
115 return "Bid" + issuevalues.toString();
116 }
117
118 @Override
119 public int hashCode() {
120 final int prime = 31;
121 int result = 1;
122 result = prime * result
123 + ((issuevalues == null) ? 0 : issuevalues.hashCode());
124 return result;
125 }
126
127 @Override
128 public boolean equals(Object obj) {
129 if (this == obj)
130 return true;
131 if (obj == null)
132 return false;
133 if (getClass() != obj.getClass())
134 return false;
135 Bid other = (Bid) obj;
136 if (issuevalues == null) {
137 if (other.issuevalues != null)
138 return false;
139 } else if (!issuevalues.equals(other.issuevalues))
140 return false;
141 return true;
142 }
143
144}
Note: See TracBrowser for help on using the repository browser.