Skip to content
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,36 @@ public class HttpPostSnippet {
}
}
```
## Object

### Deep Copy

```java
public class DeepCopySnippet {

/**
* Performs a deep copy of a Serializable object.
*/
@SuppressWarnings("unchecked")
public static <T extends Serializable> T deepCopy(T obj) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);

ObjectInputStream objectInputStream = new ObjectInputStream(
new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));

return (T) objectInputStream.readObject();

} catch (Exception e) {
throw new RuntimeException("Deep copy failed", e);
}
}
}

```


## String

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sonarqube {
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}

// In this section you declare the dependencies for your production and test code
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/object/DeepCopySnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package object;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
* This class provides a method to perform a deep copy of a Serializable object. It uses
* serialization and deserialization to create a new instance that is independent of the original
* object.
*/

public class DeepCopySnippet {

/**
* Performs a deep copy of a Serializable object.
*/
@SuppressWarnings("unchecked")
public static <T extends Serializable> T deepCopy(T obj) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);

ObjectInputStream objectInputStream = new ObjectInputStream(
new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));

return (T) objectInputStream.readObject();

} catch (Exception e) {
throw new RuntimeException("Deep copy failed", e);
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/network/HttpPostSnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void testHttpPost() throws IOException, InterruptedException {
var result = HttpPostSnippet.httpPost("https://postman-echo.com/post", arguments);
//This postman endpoint echoes the HTTP headers, request parameters, the contents
//of the request body and the complete URI requested.
var echoedData = "\"data\": \"Hello World\"";
var echoedData = "\"data\":\"Hello World\"";
assertThat(result.body(), containsString(echoedData));
}
}
63 changes: 63 additions & 0 deletions src/test/java/object/DeepCopySnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package object;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;

import java.io.Serializable;
import org.junit.jupiter.api.Test;

/**
* Unit test for {@link DeepCopySnippet}.
*/
public class DeepCopySnippetTest {

@Test
public void testDeepCopy() {
// SETUP
Owner alice = new Owner("Alice");
Animal animal = new Animal("Dog", alice);

// EXECUTE
Animal deepCopiedAnimal = DeepCopySnippet.deepCopy(animal);

// VERIFY
assertNotSame(animal, deepCopiedAnimal);
assertEquals(animal.name(), deepCopiedAnimal.name());
assertNotSame(animal.owner(), deepCopiedAnimal.owner());
assertEquals(animal.owner().name(), deepCopiedAnimal.owner().name());
}

/**
* Owner record.
*/
record Owner(String name) implements Serializable {}

/**
* Animal record.
*/
record Animal(String name, Owner owner) implements Serializable {}
}