-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #566 from eclipse/custom-write
Enhance ValueUtil
- Loading branch information
Showing
6 changed files
with
246 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...unication-core/src/main/java/org/eclipse/jnosql/communication/reader/UUIDValueReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
* | ||
*/ | ||
package org.eclipse.jnosql.communication.reader; | ||
|
||
import org.eclipse.jnosql.communication.CommunicationException; | ||
import org.eclipse.jnosql.communication.ValueReader; | ||
|
||
import java.util.UUID; | ||
|
||
public class UUIDValueReader implements ValueReader { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> T read(Class<T> type, Object value) { | ||
if (value instanceof UUID) { | ||
return (T) value; | ||
} | ||
if (value instanceof CharSequence) { | ||
return (T) getUuid(value); | ||
} | ||
return null; | ||
} | ||
|
||
private static UUID getUuid(Object value) { | ||
try { | ||
return UUID.fromString(value.toString()); | ||
} catch (IllegalArgumentException exp) { | ||
throw new CommunicationException("There is an error to convert to UUID, because the value is not UUID format: " + value, exp); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean test(Class<?> type) { | ||
return UUID.class.equals(type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...ation-core/src/test/java/org/eclipse/jnosql/communication/reader/UUIDValueReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
* Contributors: | ||
* | ||
* Otavio Santana | ||
* | ||
*/ | ||
package org.eclipse.jnosql.communication.reader; | ||
|
||
import org.eclipse.jnosql.communication.CommunicationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class UUIDValueReaderTest { | ||
private final UUIDValueReader valueReader = new UUIDValueReader(); | ||
|
||
@Test | ||
void shouldReadUUIDFromUUIDInstance() { | ||
UUID uuid = UUID.randomUUID(); | ||
UUID result = valueReader.read(UUID.class, uuid); | ||
assertEquals(uuid, result); | ||
} | ||
|
||
@Test | ||
void shouldReadUUIDFromString() { | ||
UUID uuid = UUID.randomUUID(); | ||
UUID result = valueReader.read(UUID.class, uuid.toString()); | ||
assertEquals(uuid, result); | ||
} | ||
|
||
@Test | ||
void shouldReturnNullForInvalidString() { | ||
String invalidUUID = "invalid-uuid"; | ||
assertThrows(CommunicationException.class, () -> valueReader.read(UUID.class, invalidUUID)); | ||
} | ||
|
||
@Test | ||
void shouldReturnNullForUnsupportedType() { | ||
Integer unsupportedValue = 42; | ||
UUID result = valueReader.read(UUID.class, unsupportedValue); | ||
assertNull(result); | ||
} | ||
|
||
@Test | ||
void shouldTestUUIDType() { | ||
assertTrue(valueReader.test(UUID.class)); | ||
} | ||
|
||
@Test | ||
void shouldNotTestNonUUIDType() { | ||
assertFalse(valueReader.test(String.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters