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 two more codecs
- Loading branch information
1 parent
211f879
commit 432a686
Showing
4 changed files
with
167 additions
and
17 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/main/java/org/eolang/jeo/representation/bytecode/EoCodec.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,116 @@ | ||
/* | ||
* 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.bytecode; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public final class EoCodec implements Codec { | ||
|
||
/** | ||
* Maximum long value that can be represented as double. | ||
* Any value greater than this will be represented incorrectly. | ||
*/ | ||
private static final long MAX_LONG_DOUBLE = 9_007_199_254_740_992L; | ||
|
||
/** | ||
* Minimum long value that can be represented as double. | ||
* Any value less than this will be represented incorrectly. | ||
*/ | ||
private static final long MIN_LONG_DOUBLE = -9_007_199_254_740_992L; | ||
|
||
/** | ||
* Origin codec. | ||
*/ | ||
private final Codec origin; | ||
|
||
public EoCodec() { | ||
this(new PlainCodec()); | ||
} | ||
|
||
private EoCodec(final Codec origin) { | ||
this.origin = origin; | ||
} | ||
|
||
@Override | ||
public byte[] bytes(final Object object, final DataType type) { | ||
switch (type) { | ||
case BOOL: | ||
case CHAR: | ||
case STRING: | ||
case BYTES: | ||
case LABEL: | ||
case TYPE_REFERENCE: | ||
case CLASS_REFERENCE: | ||
case NULL: | ||
return this.origin.bytes(object, type); | ||
case BYTE: | ||
case SHORT: | ||
case INT: | ||
case FLOAT: | ||
case DOUBLE: | ||
return ByteBuffer.allocate(Double.BYTES).putDouble((double) object).array(); | ||
case LONG: | ||
if (EoCodec.MIN_LONG_DOUBLE <= (long) object && (long) object <= EoCodec.MAX_LONG_DOUBLE) { | ||
return this.origin.bytes(object, type); | ||
} else { | ||
return ByteBuffer.allocate(Long.BYTES).putLong((long) object).array(); | ||
} | ||
default: | ||
throw new IllegalArgumentException( | ||
String.format("Unsupported data type: %s", type) | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public Object object(final byte[] bytes, final DataType type) { | ||
switch (type) { | ||
case BOOL: | ||
case CHAR: | ||
case STRING: | ||
case BYTES: | ||
case LABEL: | ||
case TYPE_REFERENCE: | ||
case CLASS_REFERENCE: | ||
case NULL: | ||
return this.origin.object(bytes, type); | ||
case BYTE: | ||
return (byte) ByteBuffer.wrap(bytes).getDouble(); | ||
case SHORT: | ||
return (short) ByteBuffer.wrap(bytes).getDouble(); | ||
case INT: | ||
return (int) ByteBuffer.wrap(bytes).getDouble(); | ||
case LONG: | ||
return (long) ByteBuffer.wrap(bytes).getDouble(); | ||
case FLOAT: | ||
return (float) ByteBuffer.wrap(bytes).getDouble(); | ||
case DOUBLE: | ||
return ByteBuffer.wrap(bytes).getDouble(); | ||
default: | ||
throw new IllegalArgumentException( | ||
String.format("Unsupported data type: %s", type) | ||
); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/org/eolang/jeo/representation/bytecode/EoLongCodec.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 @@ | ||
/* | ||
* 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.bytecode; | ||
|
||
import java.nio.ByteBuffer; | ||
import org.apache.bcel.classfile.Code; | ||
|
||
public final class EoLongCodec implements Codec { | ||
|
||
private final Codec origin; | ||
|
||
public EoLongCodec(final Codec origin) { | ||
this.origin = origin; | ||
} | ||
|
||
@Override | ||
public byte[] bytes(final Object object, final DataType type) { | ||
return this.origin.bytes(object, type); | ||
} | ||
|
||
@Override | ||
public Object object(final byte[] bytes, final DataType type) { | ||
if (type == DataType.LONG) { | ||
return ByteBuffer.wrap(bytes).getLong(); | ||
} else { | ||
return this.origin.object(bytes, 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