From 970910edcfc4380701c3d67cb4aa4b018cd17d1f Mon Sep 17 00:00:00 2001 From: Soby Chacko Date: Tue, 11 Mar 2025 18:09:46 -0400 Subject: [PATCH] Fix array comparison and add final modifiers in embedding classes - Fix Embedding equals() using Arrays.equals() for embedding array comparison - Fix Embedding hashCode() using Arrays.hashCode() for proper array hashing - Add final modifiers to fields in Embedding classes Signed-off-by: Soby Chacko --- .../ai/embedding/AbstractEmbeddingModel.java | 4 ++-- .../org/springframework/ai/embedding/Embedding.java | 13 +++++++------ .../ai/embedding/EmbeddingResultMetadata.java | 10 +++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/spring-ai-core/src/main/java/org/springframework/ai/embedding/AbstractEmbeddingModel.java b/spring-ai-core/src/main/java/org/springframework/ai/embedding/AbstractEmbeddingModel.java index 94987667443..17b7c5100d2 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/embedding/AbstractEmbeddingModel.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/embedding/AbstractEmbeddingModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ */ public abstract class AbstractEmbeddingModel implements EmbeddingModel { - private static Map KNOWN_EMBEDDING_DIMENSIONS = loadKnownModelDimensions(); + private static final Map KNOWN_EMBEDDING_DIMENSIONS = loadKnownModelDimensions(); /** * Default constructor. diff --git a/spring-ai-core/src/main/java/org/springframework/ai/embedding/Embedding.java b/spring-ai-core/src/main/java/org/springframework/ai/embedding/Embedding.java index 1dabb36d40c..6aebb045da9 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/embedding/Embedding.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/embedding/Embedding.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.ai.embedding; +import java.util.Arrays; import java.util.Objects; import org.springframework.ai.model.ModelResult; @@ -25,11 +26,11 @@ */ public class Embedding implements ModelResult { - private float[] embedding; + private final float[] embedding; - private Integer index; + private final Integer index; - private EmbeddingResultMetadata metadata; + private final EmbeddingResultMetadata metadata; /** * Creates a new {@link Embedding} instance. @@ -83,12 +84,12 @@ public boolean equals(Object o) { return false; } Embedding other = (Embedding) o; - return Objects.equals(this.embedding, other.embedding) && Objects.equals(this.index, other.index); + return Arrays.equals(this.embedding, other.embedding) && Objects.equals(this.index, other.index); } @Override public int hashCode() { - return Objects.hash(this.embedding, this.index); + return Objects.hash(Arrays.hashCode(this.embedding), this.index); } @Override diff --git a/spring-ai-core/src/main/java/org/springframework/ai/embedding/EmbeddingResultMetadata.java b/spring-ai-core/src/main/java/org/springframework/ai/embedding/EmbeddingResultMetadata.java index 4f7654fdd89..b6b84f9ac54 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/embedding/EmbeddingResultMetadata.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/embedding/EmbeddingResultMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,13 +81,13 @@ public enum ModalityType { public static class ModalityUtils { - private static MimeType TEXT_MIME_TYPE = MimeTypeUtils.parseMimeType("text/*"); + private static final MimeType TEXT_MIME_TYPE = MimeTypeUtils.parseMimeType("text/*"); - private static MimeType IMAGE_MIME_TYPE = MimeTypeUtils.parseMimeType("image/*"); + private static final MimeType IMAGE_MIME_TYPE = MimeTypeUtils.parseMimeType("image/*"); - private static MimeType VIDEO_MIME_TYPE = MimeTypeUtils.parseMimeType("video/*"); + private static final MimeType VIDEO_MIME_TYPE = MimeTypeUtils.parseMimeType("video/*"); - private static MimeType AUDIO_MIME_TYPE = MimeTypeUtils.parseMimeType("audio/*"); + private static final MimeType AUDIO_MIME_TYPE = MimeTypeUtils.parseMimeType("audio/*"); /** * Infers the {@link ModalityType} of the source data used to generate the