From 046430396dd40a63d6f574bf53c4d3ad2977a130 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Mon, 07 May 2012 16:48:34 +0000
Subject: [PATCH] Initial implementation for OPENDJ-355: Add fluent API for decoding attributes
---
opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Functions.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Functions.java b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Functions.java
index 8fb41d3..64c21f8 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Functions.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/Functions.java
@@ -22,13 +22,18 @@
*
*
* Copyright 2009-2010 Sun Microsystems, Inc.
+ * Portions copyright 2012 ForgeRock AS.
*/
package com.forgerock.opendj.util;
+import java.util.Calendar;
+
+import org.forgerock.i18n.LocalizedIllegalArgumentException;
import org.forgerock.opendj.ldap.AttributeDescription;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.schema.Schema;
/**
@@ -64,6 +69,14 @@
}
};
+ private static final Function<ByteString, String, Void> BYTESTRING_TO_BASE64 =
+ new Function<ByteString, String, Void>() {
+
+ public String apply(final ByteString value, final Void p) {
+ return Base64.encode(value);
+ }
+ };
+
private static final Function<ByteString, Boolean, Void> BYTESTRING_TO_BOOLEAN =
new Function<ByteString, Boolean, Void>() {
@@ -83,6 +96,18 @@
}
};
+ private static final Function<ByteString, Calendar, Void> BYTESTRING_TO_CALENDAR =
+ new Function<ByteString, Calendar, Void>() {
+
+ public Calendar apply(final ByteString value, final Void p) {
+ try {
+ return GeneralizedTime.decode(value);
+ } catch (DecodeException e) {
+ throw new LocalizedIllegalArgumentException(e.getMessageObject(), e);
+ }
+ }
+ };
+
private static final Function<ByteString, DN, Schema> BYTESTRING_TO_DN =
new Function<ByteString, DN, Schema>() {
@@ -236,6 +261,15 @@
}
/**
+ * Returns a function which encodes a {@code ByteString} as {@code Base64}.
+ *
+ * @return A function which encodes a {@code ByteString} as {@code Base64}.
+ */
+ public static Function<ByteString, String, Void> valueToBase64() {
+ return BYTESTRING_TO_BASE64;
+ }
+
+ /**
* Returns a function which parses the string representation of a
* {@code ByteString} to a {@code Boolean}. The function will accept the
* values {@code 0}, {@code false}, {@code no}, {@code off}, {@code 1},
@@ -251,6 +285,18 @@
/**
* Returns a function which parses the string representation of a
+ * {@code ByteString} as a generalized time syntax. Invalid values will
+ * result in a {@code LocalizedIllegalArgumentException}.
+ *
+ * @return A function which parses the string representation of a
+ * {@code ByteString} as generalized time syntax.
+ */
+ public static Function<ByteString, Calendar, Void> valueToCalendar() {
+ return BYTESTRING_TO_CALENDAR;
+ }
+
+ /**
+ * Returns a function which parses the string representation of a
* {@code ByteString} as a {@code DN} using the default schema. Invalid
* values will result in a {@code LocalizedIllegalArgumentException}.
*
--
Gitblit v1.10.0