forked from objectionary/jeo-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(objectionary#976): add DirectivesNumberValue
- Loading branch information
1 parent
8c4c244
commit 607f091
Showing
5 changed files
with
233 additions
and
32 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
85 changes: 85 additions & 0 deletions
85
src/main/java/org/eolang/jeo/representation/directives/DirectivesNumberBytes.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,85 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2025 Objectionary.com | ||
* | ||
* 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 NON-INFRINGEMENT. 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 org.eolang.jeo.representation.directives; | ||
|
||
import java.util.Iterator; | ||
import org.eolang.jeo.representation.bytecode.BytecodeValue; | ||
import org.xembly.Directive; | ||
import org.xembly.Directives; | ||
|
||
public final class DirectivesNumberBytes implements Iterable<Directive> { | ||
|
||
/** | ||
* Array of hexadecimal characters. | ||
* Used for converting bytes to hexadecimal. | ||
* See {@link #bytesToHex(byte[])}. | ||
*/ | ||
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); | ||
|
||
private final Number number; | ||
|
||
public DirectivesNumberBytes(final Number number) { | ||
this.number = number; | ||
} | ||
|
||
@Override | ||
public Iterator<Directive> iterator() { | ||
return new Directives() | ||
.add("o") | ||
.attr("base", "org.eolang.number") | ||
.append(new DirectivesBytes(this.bytes())) | ||
.up() | ||
.iterator(); | ||
} | ||
|
||
private String bytes() { | ||
return DirectivesNumberBytes.bytesToHex( | ||
new BytecodeValue(this.number.doubleValue()).bytes()); | ||
} | ||
|
||
/** | ||
* @todo Duplicate! | ||
*/ | ||
private static String bytesToHex(final byte[] bytes) { | ||
final String res; | ||
if (bytes == null || bytes.length == 0) { | ||
res = "--"; | ||
} else { | ||
final int length = bytes.length; | ||
final char[] hex = new char[length * 3]; | ||
for (int index = 0; index < length; ++index) { | ||
final int value = bytes[index] & 0xFF; | ||
hex[index * 3] = HEX_ARRAY[value >>> 4]; | ||
hex[index * 3 + 1] = HEX_ARRAY[value & 0x0F]; | ||
hex[index * 3 + 2] = '-'; | ||
} | ||
if (hex.length == 3) { | ||
res = new String(hex); | ||
} else { | ||
res = new String(hex, 0, hex.length - 1); | ||
} | ||
} | ||
return res; | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
src/test/java/org/eolang/jeo/representation/directives/DirectivesNumberBytesTest.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,81 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2025 Objectionary.com | ||
* | ||
* 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 NON-INFRINGEMENT. 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 org.eolang.jeo.representation.directives; | ||
|
||
import com.jcabi.matchers.XhtmlMatchers; | ||
import java.util.stream.Stream; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.xembly.ImpossibleModificationException; | ||
import org.xembly.Xembler; | ||
|
||
/** | ||
* Test case for {@link DirectivesNumberBytes}. | ||
* @since 0.8 | ||
*/ | ||
final class DirectivesNumberBytesTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("numbers") | ||
void convertsDifferentNumbersToBytes( | ||
final Number value, final String bytes | ||
) throws ImpossibleModificationException { | ||
final String xml = new Xembler(new DirectivesNumberBytes(value)).xml(); | ||
MatcherAssert.assertThat( | ||
String.format( | ||
"We expect that number will be converted to the correct XMIR, but got: %n%s%n", | ||
xml | ||
), | ||
xml, | ||
XhtmlMatchers.hasXPaths( | ||
"/o[@base='org.eolang.number']", | ||
String.format( | ||
"/o[@base='org.eolang.number']/o[@base='org.eolang.bytes' and text()='%s']", | ||
bytes | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Provide numbers. | ||
* @return Numbers. | ||
*/ | ||
static Stream<Arguments> numbers() { | ||
final String same = "3F-F0-00-00-00-00-00-00"; | ||
return Stream.of( | ||
Arguments.of(1, same), | ||
Arguments.of(1L, same), | ||
Arguments.of(1.0, same), | ||
Arguments.of(1.0f, same), | ||
Arguments.of(1.0d, same), | ||
Arguments.of((byte) 1, same), | ||
Arguments.of((short) 1, same) | ||
); | ||
} | ||
|
||
|
||
} |
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