Skip to content

Commit

Permalink
Merge pull request #105 from nls-jajuko/double-id-writer
Browse files Browse the repository at this point in the history
Use DToA.dtoa() to format ID when data type is double
  • Loading branch information
jampukka authored Apr 16, 2024
2 parents 9bbcbdb + 5bf807b commit 6b91f06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fi.nls.hakunapi.core.ObjectArrayValueContainer;
import fi.nls.hakunapi.core.SingleFeatureWriter;
import fi.nls.hakunapi.core.ValueProvider;
import fi.nls.hakunapi.core.util.DToA;

public final class HakunaPropertyWriters {

Expand Down Expand Up @@ -166,12 +167,19 @@ public static HakunaPropertyWriter getIdPropertyWriter(FeatureType ft, String la
};
case DOUBLE:
return (vp, i, writer) -> {
writeStartFeature(ft, layerName, writer, type, vp.getObject(i).toString());
};
writeStartFeature(ft, layerName, writer, type, doubleAsID(vp.getDouble(i)));
};
default:
throw new IllegalArgumentException("Invalid type for id property");
}
}

protected static String doubleAsID(Double d) {
byte[] b = new byte[24];
int len = DToA.dtoa(d, b, 0, 0, 8);
return new String(b, 0, len);
}


private static void writeStartFeature(FeatureType ft, String layerName, FeatureWriter writer, HakunaPropertyType type, Object value) throws Exception {
if (writer instanceof FeatureCollectionWriter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fi.nls.hakunapi.core.property;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class HakunaPropertyWritersTest {

@Test
public void testDoubleAsID() {

assertEquals("1.7976931348623157E308", HakunaPropertyWriters.doubleAsID(Double.MAX_VALUE));
assertEquals("9223372036854775807", HakunaPropertyWriters.doubleAsID(Double.valueOf(Long.MAX_VALUE)));

assertEquals("17263226", HakunaPropertyWriters.doubleAsID(1.7263226E7));
assertEquals("2010474", HakunaPropertyWriters.doubleAsID(2010474.0));
}
}

0 comments on commit 6b91f06

Please sign in to comment.