[java-support] 18/21: Add 64-bit long support to DDF library.
Scott Cantor
cantor.2 at osu.edu
Thu Jun 2 14:39:05 UTC 2022
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch dev/JSPT-111
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=b378b5f77213e7a7bbdefc671ef0f81d233e6bff
commit b378b5f77213e7a7bbdefc671ef0f81d233e6bff
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon May 23 11:15:45 2022 -0400
Add 64-bit long support to DDF library.
---
.../shibboleth/utilities/java/support/ddf/DDF.java | 137 ++++++++++++++++++++-
.../utilities/java/support/ddf/DDFTest.java | 30 ++++-
.../utilities/java/support/ddf/long-name.ddf | 1 +
3 files changed, 164 insertions(+), 4 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/ddf/DDF.java b/src/main/java/net/shibboleth/utilities/java/support/ddf/DDF.java
index 3689c7f..b6e1798 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/ddf/DDF.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/ddf/DDF.java
@@ -77,7 +77,7 @@ public class DDF implements Iterable<DDF> {
/** A string value. */
DDF_STRING(1),
- /** An integral value. */
+ /** An integral value of no more than 32-bits. */
DDF_INT(2),
/** A floating point value. */
@@ -93,7 +93,10 @@ public class DDF implements Iterable<DDF> {
DDF_POINTER(6),
/** A string that cannot be assumed to be UTF-8 (see above docs). */
- DDF_STRING_UNSAFE(7);
+ DDF_STRING_UNSAFE(7),
+
+ /** An integral value of no more than 64-bits. */
+ DDF_LONG(8);
/** Type value. */
private final int value;
@@ -163,6 +166,10 @@ public class DDF implements Iterable<DDF> {
case 7:
type = DDF_STRING_UNSAFE;
break;
+
+ case 8:
+ type = DDF_LONG;
+ break;
default:
throw new IllegalArgumentException("Unrecognized DDF type");
@@ -234,6 +241,19 @@ public class DDF implements Iterable<DDF> {
integer(val);
}
+ /**
+ * Constructor.
+ *
+ * <p>For compatibility, the name is constrained to <= 255 characters.</p>
+ *
+ * @param n node name
+ * @param val long integer value
+ */
+ public DDF(@Nullable @NotEmpty final String n, final long val) {
+ this(n);
+ longinteger(val);
+ }
+
/**
* Constructor.
*
@@ -303,6 +323,10 @@ public class DDF implements Iterable<DDF> {
case DDF_INT:
dup.integer((Integer) value);
break;
+
+ case DDF_LONG:
+ dup.longinteger((Long) value);
+ break;
case DDF_FLOAT:
dup.floating((Double) value);
@@ -405,6 +429,15 @@ public class DDF implements Iterable<DDF> {
return type == DDFType.DDF_INT;
}
+ /**
+ * Returns true iff the node is a long integer.
+ *
+ * @return true iff the node is a long integer
+ */
+ public boolean islong() {
+ return type == DDFType.DDF_LONG;
+ }
+
/**
* Returns true iff the node is a floating point.
*
@@ -476,6 +509,8 @@ public class DDF implements Iterable<DDF> {
switch(type) {
case DDF_INT:
return (Integer) value;
+ case DDF_LONG:
+ return ((Long) value).intValue();
case DDF_FLOAT:
return ((Double) value).intValue();
case DDF_STRING:
@@ -495,6 +530,41 @@ public class DDF implements Iterable<DDF> {
return null;
}
+
+ /**
+ * Get the long integer value of this node.
+ *
+ * <p>Longs are coerced from other types based on numeric conversions
+ * or the count of a structure or list.</p>
+ *
+ * @return the long integer value or null
+ */
+ @Nullable public Long longinteger() {
+
+ switch(type) {
+ case DDF_INT:
+ return ((Integer) value).longValue();
+ case DDF_LONG:
+ return (Long) value;
+ case DDF_FLOAT:
+ return ((Double) value).longValue();
+ case DDF_STRING:
+ try {
+ return Long.valueOf((String) value);
+ } catch (final NumberFormatException e) {
+ // Swallow.
+ return null;
+ }
+ case DDF_STRUCT:
+ return (long) ((Map<?,?>) value).size();
+ case DDF_LIST:
+ return (long) ((List<?>) value).size();
+ default:
+ break;
+ }
+
+ return null;
+ }
/**
* Get the floating point value of this node.
@@ -509,6 +579,8 @@ public class DDF implements Iterable<DDF> {
switch(type) {
case DDF_INT:
return ((Integer) value).doubleValue();
+ case DDF_LONG:
+ return ((Long) value).doubleValue();
case DDF_FLOAT:
return (Double) value;
case DDF_STRING:
@@ -594,6 +666,17 @@ public class DDF implements Iterable<DDF> {
return string(Integer.toString(val));
}
+ /**
+ * Converts this node to a string type/value based on the converted form of the input.
+ *
+ * @param val input value
+ *
+ * @return this object
+ */
+ @Nonnull public DDF string(final long val) {
+ return string(Long.toString(val));
+ }
+
/**
* Converts this node to a string type/value based on the converted form of the input.
*
@@ -637,6 +720,38 @@ public class DDF implements Iterable<DDF> {
}
}
+ /**
+ * Converts this node to a long integer type/value.
+ *
+ * @param val value to inject
+ *
+ * @return this object
+ */
+ @Nonnull public DDF longinteger(final long val) {
+ empty();
+ value = Long.valueOf(val);
+ type = DDFType.DDF_LONG;
+ return this;
+ }
+
+ /**
+ * Converts this node to a long integer type/value based on the converted form of the input.
+ *
+ * <p>A conversion error will assign zero as the value.</p>
+ *
+ * @param val value to inject
+ *
+ * @return this object
+ */
+ @Nonnull public DDF longinteger(@Nonnull @NotEmpty final String val) {
+ empty();
+ try {
+ return longinteger(Long.valueOf(val));
+ } catch (final NumberFormatException e) {
+ return longinteger(0);
+ }
+ }
+
/**
* Converts this node to an floating point type/value.
*
@@ -1089,6 +1204,14 @@ public class DDF implements Iterable<DDF> {
builder.append(" = ").append(value);
break;
+ case DDF_LONG:
+ builder.append("Long");
+ if (name != null) {
+ builder.append(' ').append(name);
+ }
+ builder.append(" = ").append(value);
+ break;
+
case DDF_FLOAT:
builder.append("Double");
if (name != null) {
@@ -1205,6 +1328,13 @@ public class DDF implements Iterable<DDF> {
os.write('\n');
break;
+ case DDF_LONG:
+ os.write(Integer.toString(type.getValue()).getBytes("UTF8"));
+ os.write(' ');
+ os.write(Long.toString((Long) value).getBytes("UTF8"));
+ os.write('\n');
+ break;
+
case DDF_FLOAT:
os.write(Integer.toString(type.getValue()).getBytes("UTF8"));
os.write(' ');
@@ -1360,6 +1490,7 @@ public class DDF implements Iterable<DDF> {
}
case DDF_INT:
+ case DDF_LONG:
case DDF_FLOAT:
if (ch != 0x20) {
throw new IOException("Type field not followed by space character");
@@ -1383,6 +1514,8 @@ public class DDF implements Iterable<DDF> {
if (type == DDFType.DDF_INT) {
return obj.integer(valueBuilder.toString());
+ } else if (type == DDFType.DDF_LONG) {
+ return obj.longinteger(valueBuilder.toString());
}
return obj.floating(valueBuilder.toString());
diff --git a/src/test/java/net/shibboleth/utilities/java/support/ddf/DDFTest.java b/src/test/java/net/shibboleth/utilities/java/support/ddf/DDFTest.java
index d44cb68..bbaf33e 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/ddf/DDFTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/ddf/DDFTest.java
@@ -56,6 +56,11 @@ public class DDFTest {
assertTrue(obj.isint());
assertEquals(obj.integer(), Integer.valueOf(42));
+ obj = new DDF("foo", 42000000000L);
+ assertEquals(obj.name(), "foo");
+ assertTrue(obj.islong());
+ assertEquals(obj.longinteger(), Long.valueOf(42000000000L));
+
obj = new DDF("foo", 42.42);
assertEquals(obj.name(), "foo");
assertTrue(obj.isfloat());
@@ -92,7 +97,17 @@ public class DDFTest {
assertTrue(obj.isint());
assertEquals(obj.integer(), Integer.valueOf(42));
assertEquals(obj.floating(), Double.valueOf(42));
-
+
+ obj.longinteger(42000000000L);
+ assertTrue(obj.islong());
+ assertEquals(obj.longinteger(), Long.valueOf(42000000000L));
+ assertEquals(obj.floating(), Double.valueOf(42000000000L));
+
+ obj.longinteger("42000000000");
+ assertTrue(obj.islong());
+ assertEquals(obj.longinteger(), Long.valueOf(42000000000L));
+ assertEquals(obj.floating(), Double.valueOf(42000000000L));
+
obj.floating(42.42);
assertTrue(obj.isfloat());
assertEquals(obj.integer(), Integer.valueOf(42));
@@ -230,6 +245,11 @@ public class DDFTest {
assertEquals(sink.toByteArray(), testFile("int-name.ddf"));
sink.reset();
+ obj.longinteger(42000000000L);
+ obj.serialize(sink);
+ assertEquals(sink.toByteArray(), testFile("long-name.ddf"));
+ sink.reset();
+
obj.floating(42.1315927);
obj.serialize(sink);
assertEquals(sink.toByteArray(), testFile("float-name.ddf"));
@@ -297,6 +317,13 @@ public class DDFTest {
assertEquals(obj.name(), "foo bar");
assertEquals(obj.integer(), Integer.valueOf(42));
}
+
+ try (final InputStream is = getClass().getResourceAsStream("long-name.ddf")) {
+ final DDF obj = DDF.deserialize(is);
+ assertTrue(obj.islong());
+ assertEquals(obj.name(), "foo bar");
+ assertEquals(obj.longinteger(), Long.valueOf(42000000000L));
+ }
try (final InputStream is = getClass().getResourceAsStream("float-name.ddf")) {
final DDF obj = DDF.deserialize(is);
@@ -446,7 +473,6 @@ public class DDFTest {
} catch (final IOException e) {
}
-
}
}
\ No newline at end of file
diff --git a/src/test/resources/net/shibboleth/utilities/java/support/ddf/long-name.ddf b/src/test/resources/net/shibboleth/utilities/java/support/ddf/long-name.ddf
new file mode 100644
index 0000000..f1c355e
--- /dev/null
+++ b/src/test/resources/net/shibboleth/utilities/java/support/ddf/long-name.ddf
@@ -0,0 +1 @@
+foo%20bar 8 42000000000
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list