Skip to content

Commit

Permalink
[lang][core] Add function for enabling casting from sequence of chara…
Browse files Browse the repository at this point in the history
…cters to UUID.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Dec 13, 2020
1 parent 04d14ad commit f669a90
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -446,4 +447,25 @@ public static BigDecimal toBigDecimal(CharSequence value) {
return BigDecimal.valueOf(0.);
}

/** Decodes a {@code CharSequence} into a {@code UUID}.
*
* <p>In opposite to the functions of {@link UUID}, this function is
* null-safe and does not generate an exception.
* If the given string cannot by parsed, {@code null} is replied.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code UUID} type, or {@code null}
* if a UUID cannot be parsed from the string of characters.
* @since 0.12
*/
@Pure
public static UUID toUUID(CharSequence value) {
try {
return UUID.fromString(value.toString());
} catch (Throwable exception) {
// Silent error
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
import static io.sarl.tests.api.tools.TestAssertions.assertEpsilonEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -230,4 +231,17 @@ public void toBigDecimal_CharSequence() {
assertEpsilonEquals(0., PrimitiveCastExtensions.toBigDecimal(null).doubleValue());
}

@Test
public void toUUID_CharSequence() throws Exception {
final String strValue0 = "433907b7-f201-4727-907a-9a34eb9a136c";
final String strValue1 = "d51c801f-9253-4777-82ce-1bfbb5f10ea1";
final String strValue2 = "zzzzzzzz-xxxx-zzzz-xxxx-zzzzzzzzzzzz";
assertNull(PrimitiveCastExtensions.toUUID(null));
assertNull(PrimitiveCastExtensions.toUUID(""));
assertNull(PrimitiveCastExtensions.toUUID("a"));
assertEquals(UUID.fromString(strValue0), PrimitiveCastExtensions.toUUID(strValue0));
assertEquals(UUID.fromString(strValue1), PrimitiveCastExtensions.toUUID(strValue1));
assertNull(PrimitiveCastExtensions.toUUID(strValue2));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,58 @@ public void string_as_bigdecimal(ResourceSetGlobalCompilationContext ctx) throws
ctx.compileTo(STRING_AS_BIGDECIMAL_SARL, STRING_AS_BIGDECIMAL_JAVA);
}

private static final String STRING_AS_UUID_SARL = multilineString(
"import java.util.UUID",
"class A {",
" def fct(left : String) : UUID {",
" left as UUID",
" }",
"}");

private static final String STRING_AS_UUID_JAVA = multilineString(
"import io.sarl.lang.annotation.SarlElementType;",
"import io.sarl.lang.annotation.SarlSpecification;",
"import io.sarl.lang.annotation.SyntheticMember;",
"import io.sarl.lang.scoping.extensions.cast.PrimitiveCastExtensions;",
"import java.uutil.UUID;",
"import org.eclipse.xtext.xbase.lib.Pure;",
"",
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SarlElementType(" + SarlPackage.SARL_CLASS + ")",
"@SuppressWarnings(\"all\")",
"public class A {",
" @Pure",
" public UUID fct(final String left) {",
" return (left == null ? null : PrimitiveCastExtensions.toUUID(left));",
" }",
" ",
" @SyntheticMember",
" public A() {",
" super();",
" }",
"}",
"");

@Test
@Tag("sarlValidation")
public void string_as_uuid_issues() throws Exception {
validate(getValidationHelper(), getInjector(), file(getParseHelper(), STRING_AS_UUID_SARL))
.assertNoErrors(
TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(),
org.eclipse.xtext.xbase.validation.IssueCodes.INVALID_CAST)
.assertNoWarnings(
TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(),
org.eclipse.xtext.xbase.validation.IssueCodes.OBSOLETE_CAST)
.assertWarning(
TypesPackage.eINSTANCE.getJvmParameterizedTypeReference(),
IssueCodes.POTENTIAL_INEFFICIENT_VALUE_CONVERSION,
"'toUUID'");
}

@GlobalCompilationTestContribution
@Tag("compileToJava")
public void string_as_uuid(ResourceSetGlobalCompilationContext ctx) throws Exception {
ctx.compileTo(STRING_AS_UUID_SARL, STRING_AS_UUID_JAVA);
}

}

0 comments on commit f669a90

Please sign in to comment.