Skip to content

Commit

Permalink
feat(objectionary#976): add two more codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jan 22, 2025
1 parent 211f879 commit 432a686
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 17 deletions.
116 changes: 116 additions & 0 deletions src/main/java/org/eolang/jeo/representation/bytecode/EoCodec.java
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)
);
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Optional;
import org.objectweb.asm.Type;

public final class PlainCodec implements Codec {
final class PlainCodec implements Codec {
private static final byte[] EMPTY = new byte[0];

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,6 @@ public Iterator<Directive> iterator() {
break;
}
return res.iterator();
// if ("string".equals(type)) {
// res = new DirectivesEoObject(
// type,
// this.name,
// new DirectivesComment(this.comment()),
// new DirectivesBytes(this.hex())
// );
// } else {
// res = new DirectivesJeoObject(
// type,
// this.name,
// new DirectivesComment(this.comment()),
// new DirectivesBytes(this.hex())
// );
// }
// return res.iterator();
}

/**
Expand Down

0 comments on commit 432a686

Please sign in to comment.