Skip to content

Make DefaultSdkAutoConstructList/Map Serializable. #5936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.awssdk.core.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -30,7 +31,7 @@
* @param <T> The element type.
*/
@SdkProtectedApi
public final class DefaultSdkAutoConstructList<T> implements SdkAutoConstructList<T> {
public final class DefaultSdkAutoConstructList<T> implements SdkAutoConstructList<T>, Serializable {
private static final DefaultSdkAutoConstructList INSTANCE = new DefaultSdkAutoConstructList();

private final List impl = Collections.emptyList();
Expand Down Expand Up @@ -173,4 +174,8 @@ public String toString() {
return impl.toString();
}

private Object readResolve() {
return getInstance();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.awssdk.core.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand All @@ -30,7 +31,7 @@
* @param <V> The value type.
*/
@SdkProtectedApi
public final class DefaultSdkAutoConstructMap<K, V> implements SdkAutoConstructMap<K, V> {
public final class DefaultSdkAutoConstructMap<K, V> implements SdkAutoConstructMap<K, V>, Serializable {
private static final DefaultSdkAutoConstructMap INSTANCE = new DefaultSdkAutoConstructMap();

private final Map<K, V> impl = Collections.emptyMap();
Expand Down Expand Up @@ -117,4 +118,9 @@ public int hashCode() {
public String toString() {
return impl.toString();
}

private Object readResolve() {
return getInstance();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class DefaultSdkAutoConstructListTest {
Expand All @@ -42,4 +48,32 @@ public void hashCode_sameAsEmptyList() {
public void toString_emptyList() {
assertThat(INSTANCE.toString()).isEqualTo("[]");
}

@Test
@DisplayName("DefaultSdkAutoConstruct is Serializable, and is same instance.")
public void serialization_sameSingletonInstance() throws Exception {
// Create instance of DefaultSdkAutoConstructList
List defaultSdkAutoConstructList = DefaultSdkAutoConstructList.getInstance();

// Serialize instance into byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(baos);

objectOutputStream.writeObject(defaultSdkAutoConstructList);
objectOutputStream.flush();
objectOutputStream.close();

// Serialization result
byte[] bytes = baos.toByteArray();

// Deserialize bytes
ByteArrayInputStream fileInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// Deserialized result
DefaultSdkAutoConstructList resultList = (DefaultSdkAutoConstructList) objectInputStream.readObject();
objectInputStream.close();

// Compare using "==", to make sure it is the same reference.
assertThat(resultList == defaultSdkAutoConstructList).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class DefaultSdkAutoConstructMapTest {
Expand All @@ -41,4 +48,31 @@ public void hashCode_sameAsEmptyMap() {
public void toString_emptyMap() {
assertThat(AUTO_CONSTRUCT_MAP.toString()).isEqualTo("{}");
}
@Test
@DisplayName("DefaultSdkAutoConstructMap is Serializable, and is same instance.")
public void serialization_sameSingletonInstance() throws Exception {
// Create instance of DefaultSdkAutoConstructMap
Map defaultSdkAutoConstructMap = DefaultSdkAutoConstructMap.getInstance();

// Serialize instance into byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(baos);

objectOutputStream.writeObject(defaultSdkAutoConstructMap);
objectOutputStream.flush();
objectOutputStream.close();

// Serialization result
byte[] bytes = baos.toByteArray();

// Deserialize bytes
ByteArrayInputStream fileInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
// Deserialized result
DefaultSdkAutoConstructMap resultMap = (DefaultSdkAutoConstructMap) objectInputStream.readObject();
objectInputStream.close();

// Compare using "==", to make sure it is the same reference.
assertThat(resultMap == defaultSdkAutoConstructMap).isTrue();
}
}