Skip to content

Commit dc9ea17

Browse files
apappascsmarkpollack
authored andcommitted
feat: equals, hashCode, deep copy, and tests to MiniMaxChatOptions
This commit enhances MiniMaxChatOptions by: - Updating `equals` and `hashCode` methods for proper object comparison. - Updating `copy()` method, creating new instances of mutable collections (List, Set, Map, Metadata) to prevent shared state. - Adding `MiniMaxChatOptionsTests` to verify `copy()`, builders, setters, and default values. - Add more tests for objects not equal, defensive unmodifiable getters etc. Signed-off-by: Alexandros Pappas <apappascs@gmail.com>
1 parent f89530c commit dc9ea17

File tree

2 files changed

+235
-178
lines changed

2 files changed

+235
-178
lines changed

models/spring-ai-minimax/src/main/java/org/springframework/ai/minimax/MiniMaxChatOptions.java

Lines changed: 27 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.HashMap;
2223
import java.util.HashSet;
2324
import java.util.List;
2425
import java.util.Map;
26+
import java.util.Objects;
2527
import java.util.Set;
2628

2729
import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -45,6 +47,7 @@
4547
* @author Geng Rong
4648
* @author Thomas Vitale
4749
* @author Ilayaperumal Gopinathan
50+
* @author Alexandros Pappas
4851
* @since 1.0.0 M1
4952
*/
5053
@JsonInclude(Include.NON_NULL)
@@ -167,11 +170,11 @@ public static MiniMaxChatOptions fromOptions(MiniMaxChatOptions fromOptions) {
167170
.presencePenalty(fromOptions.getPresencePenalty())
168171
.responseFormat(fromOptions.getResponseFormat())
169172
.seed(fromOptions.getSeed())
170-
.stop(fromOptions.getStop())
173+
.stop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null)
171174
.temperature(fromOptions.getTemperature())
172175
.topP(fromOptions.getTopP())
173176
.maskSensitiveInfo(fromOptions.getMaskSensitiveInfo())
174-
.tools(fromOptions.getTools())
177+
.tools(fromOptions.getTools() != null ? new ArrayList<>(fromOptions.getTools()) : null)
175178
.toolChoice(fromOptions.getToolChoice())
176179
.toolCallbacks(fromOptions.getToolCallbacks())
177180
.toolNames(fromOptions.getToolNames())
@@ -252,7 +255,7 @@ public void setStopSequences(List<String> stopSequences) {
252255
}
253256

254257
public List<String> getStop() {
255-
return this.stop;
258+
return (this.stop != null) ? Collections.unmodifiableList(this.stop) : null;
256259
}
257260

258261
public void setStop(List<String> stop) {
@@ -286,7 +289,7 @@ public void setMaskSensitiveInfo(Boolean maskSensitiveInfo) {
286289
}
287290

288291
public List<MiniMaxApi.FunctionTool> getTools() {
289-
return this.tools;
292+
return (this.tools != null) ? Collections.unmodifiableList(this.tools) : null;
290293
}
291294

292295
public void setTools(List<MiniMaxApi.FunctionTool> tools) {
@@ -310,7 +313,7 @@ public Integer getTopK() {
310313
@Override
311314
@JsonIgnore
312315
public List<ToolCallback> getToolCallbacks() {
313-
return this.toolCallbacks;
316+
return Collections.unmodifiableList(this.toolCallbacks);
314317
}
315318

316319
@Override
@@ -324,7 +327,7 @@ public void setToolCallbacks(List<ToolCallback> toolCallbacks) {
324327
@Override
325328
@JsonIgnore
326329
public Set<String> getToolNames() {
327-
return this.toolNames;
330+
return Collections.unmodifiableSet(this.toolNames);
328331
}
329332

330333
@Override
@@ -351,7 +354,7 @@ public void setInternalToolExecutionEnabled(@Nullable Boolean internalToolExecut
351354

352355
@Override
353356
public Map<String, Object> getToolContext() {
354-
return this.toolContext;
357+
return (this.toolContext != null) ? Collections.unmodifiableMap(this.toolContext) : null;
355358
}
356359

357360
@Override
@@ -361,182 +364,28 @@ public void setToolContext(Map<String, Object> toolContext) {
361364

362365
@Override
363366
public int hashCode() {
364-
final int prime = 31;
365-
int result = 1;
366-
result = prime * result + ((this.model == null) ? 0 : this.model.hashCode());
367-
result = prime * result + ((this.frequencyPenalty == null) ? 0 : this.frequencyPenalty.hashCode());
368-
result = prime * result + ((this.maxTokens == null) ? 0 : this.maxTokens.hashCode());
369-
result = prime * result + ((this.n == null) ? 0 : this.n.hashCode());
370-
result = prime * result + ((this.presencePenalty == null) ? 0 : this.presencePenalty.hashCode());
371-
result = prime * result + ((this.responseFormat == null) ? 0 : this.responseFormat.hashCode());
372-
result = prime * result + ((this.seed == null) ? 0 : this.seed.hashCode());
373-
result = prime * result + ((this.stop == null) ? 0 : this.stop.hashCode());
374-
result = prime * result + ((this.temperature == null) ? 0 : this.temperature.hashCode());
375-
result = prime * result + ((this.topP == null) ? 0 : this.topP.hashCode());
376-
result = prime * result + ((this.maskSensitiveInfo == null) ? 0 : this.maskSensitiveInfo.hashCode());
377-
result = prime * result + ((this.tools == null) ? 0 : this.tools.hashCode());
378-
result = prime * result + ((this.toolChoice == null) ? 0 : this.toolChoice.hashCode());
379-
result = prime * result + ((this.toolCallbacks == null) ? 0 : this.toolCallbacks.hashCode());
380-
result = prime * result + ((this.toolNames == null) ? 0 : this.toolNames.hashCode());
381-
result = prime * result
382-
+ ((this.internalToolExecutionEnabled == null) ? 0 : this.internalToolExecutionEnabled.hashCode());
383-
result = prime * result + ((this.toolContext == null) ? 0 : this.toolContext.hashCode());
384-
return result;
367+
return Objects.hash(model, frequencyPenalty, maxTokens, n, presencePenalty, responseFormat, seed, stop,
368+
temperature, topP, maskSensitiveInfo, tools, toolChoice, toolCallbacks, toolNames, toolContext,
369+
internalToolExecutionEnabled);
385370
}
386371

387372
@Override
388-
public boolean equals(Object obj) {
389-
if (this == obj) {
373+
public boolean equals(Object o) {
374+
if (this == o)
390375
return true;
391-
}
392-
if (obj == null) {
393-
return false;
394-
}
395-
if (getClass() != obj.getClass()) {
396-
return false;
397-
}
398-
MiniMaxChatOptions other = (MiniMaxChatOptions) obj;
399-
if (this.model == null) {
400-
if (other.model != null) {
401-
return false;
402-
}
403-
}
404-
else if (!this.model.equals(other.model)) {
405-
return false;
406-
}
407-
if (this.frequencyPenalty == null) {
408-
if (other.frequencyPenalty != null) {
409-
return false;
410-
}
411-
}
412-
else if (!this.frequencyPenalty.equals(other.frequencyPenalty)) {
413-
return false;
414-
}
415-
if (this.maxTokens == null) {
416-
if (other.maxTokens != null) {
417-
return false;
418-
}
419-
}
420-
else if (!this.maxTokens.equals(other.maxTokens)) {
421-
return false;
422-
}
423-
if (this.n == null) {
424-
if (other.n != null) {
425-
return false;
426-
}
427-
}
428-
else if (!this.n.equals(other.n)) {
429-
return false;
430-
}
431-
if (this.presencePenalty == null) {
432-
if (other.presencePenalty != null) {
433-
return false;
434-
}
435-
}
436-
else if (!this.presencePenalty.equals(other.presencePenalty)) {
437-
return false;
438-
}
439-
if (this.responseFormat == null) {
440-
if (other.responseFormat != null) {
441-
return false;
442-
}
443-
}
444-
else if (!this.responseFormat.equals(other.responseFormat)) {
445-
return false;
446-
}
447-
if (this.seed == null) {
448-
if (other.seed != null) {
449-
return false;
450-
}
451-
}
452-
else if (!this.seed.equals(other.seed)) {
453-
return false;
454-
}
455-
if (this.stop == null) {
456-
if (other.stop != null) {
457-
return false;
458-
}
459-
}
460-
else if (!this.stop.equals(other.stop)) {
461-
return false;
462-
}
463-
if (this.temperature == null) {
464-
if (other.temperature != null) {
465-
return false;
466-
}
467-
}
468-
else if (!this.temperature.equals(other.temperature)) {
469-
return false;
470-
}
471-
if (this.topP == null) {
472-
if (other.topP != null) {
473-
return false;
474-
}
475-
}
476-
else if (!this.topP.equals(other.topP)) {
477-
return false;
478-
}
479-
if (this.maskSensitiveInfo == null) {
480-
if (other.maskSensitiveInfo != null) {
481-
return false;
482-
}
483-
}
484-
else if (!this.maskSensitiveInfo.equals(other.maskSensitiveInfo)) {
485-
return false;
486-
}
487-
if (this.tools == null) {
488-
if (other.tools != null) {
489-
return false;
490-
}
491-
}
492-
else if (!this.tools.equals(other.tools)) {
493-
return false;
494-
}
495-
if (this.toolChoice == null) {
496-
if (other.toolChoice != null) {
497-
return false;
498-
}
499-
}
500-
else if (!this.toolChoice.equals(other.toolChoice)) {
501-
return false;
502-
}
503-
if (this.internalToolExecutionEnabled == null) {
504-
if (other.internalToolExecutionEnabled != null) {
505-
return false;
506-
}
507-
}
508-
else if (!this.internalToolExecutionEnabled.equals(other.internalToolExecutionEnabled)) {
509-
return false;
510-
}
511-
512-
if (this.toolNames == null) {
513-
if (other.toolNames != null) {
514-
return false;
515-
}
516-
}
517-
else if (!this.toolNames.equals(other.toolNames)) {
376+
if (o == null || getClass() != o.getClass())
518377
return false;
519-
}
520-
521-
if (this.toolCallbacks == null) {
522-
if (other.toolCallbacks != null) {
523-
return false;
524-
}
525-
}
526-
else if (!this.toolCallbacks.equals(other.toolCallbacks)) {
527-
return false;
528-
}
529-
530-
if (this.toolContext == null) {
531-
if (other.toolContext != null) {
532-
return false;
533-
}
534-
}
535-
else if (!this.toolContext.equals(other.toolContext)) {
536-
return false;
537-
}
538-
539-
return true;
378+
MiniMaxChatOptions that = (MiniMaxChatOptions) o;
379+
return Objects.equals(model, that.model) && Objects.equals(frequencyPenalty, that.frequencyPenalty)
380+
&& Objects.equals(maxTokens, that.maxTokens) && Objects.equals(n, that.n)
381+
&& Objects.equals(presencePenalty, that.presencePenalty)
382+
&& Objects.equals(responseFormat, that.responseFormat) && Objects.equals(seed, that.seed)
383+
&& Objects.equals(stop, that.stop) && Objects.equals(temperature, that.temperature)
384+
&& Objects.equals(topP, that.topP) && Objects.equals(maskSensitiveInfo, that.maskSensitiveInfo)
385+
&& Objects.equals(tools, that.tools) && Objects.equals(toolChoice, that.toolChoice)
386+
&& Objects.equals(toolCallbacks, that.toolCallbacks) && Objects.equals(toolNames, that.toolNames)
387+
&& Objects.equals(toolContext, that.toolContext)
388+
&& Objects.equals(internalToolExecutionEnabled, that.internalToolExecutionEnabled);
540389
}
541390

542391
@Override

0 commit comments

Comments
 (0)