From 2bc8d15a28fafab97cefafede06d6b7e738ae0fe Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Fri, 11 Dec 2009 18:45:45 +0000
Subject: [PATCH] Various incremental improvements.

---
 sdk/src/com/sun/opends/sdk/util/Functions.java |  177 ++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 97 insertions(+), 80 deletions(-)

diff --git a/sdk/src/com/sun/opends/sdk/util/Functions.java b/sdk/src/com/sun/opends/sdk/util/Functions.java
index 716f081..3353564 100644
--- a/sdk/src/com/sun/opends/sdk/util/Functions.java
+++ b/sdk/src/com/sun/opends/sdk/util/Functions.java
@@ -46,6 +46,7 @@
       Function<M, N, Void>
   {
     private final Function<M, N, P> function;
+
     private final P parameter;
 
 
@@ -68,99 +69,101 @@
 
   }
 
-  private static final Function<ByteString, AttributeDescription, Schema> BYTESTRING_TO_ATTRIBUTE_DESCRIPTION =
-      new Function<ByteString, AttributeDescription, Schema>()
+
+
+  private static final Function<ByteString, AttributeDescription, Schema> BYTESTRING_TO_ATTRIBUTE_DESCRIPTION = new Function<ByteString, AttributeDescription, Schema>()
+  {
+
+    public AttributeDescription apply(ByteString value, Schema p)
+    {
+      // FIXME: what should we do if parsing fails?
+      return AttributeDescription.valueOf(value.toString(), p);
+    }
+  };
+
+  private static final Function<ByteString, Boolean, Void> BYTESTRING_TO_BOOLEAN = new Function<ByteString, Boolean, Void>()
+  {
+
+    public Boolean apply(ByteString value, Void p)
+    {
+      String valueString = StaticUtils.toLowerCase(value.toString());
+
+      if (valueString.equals("true") || valueString.equals("yes")
+          || valueString.equals("on") || valueString.equals("1"))
       {
-
-        public AttributeDescription apply(ByteString value, Schema p)
-        {
-          // FIXME: what should we do if parsing fails?
-          return AttributeDescription.valueOf(value.toString(), p);
-        }
-      };
-
-  private static final Function<ByteString, Boolean, Void> BYTESTRING_TO_BOOLEAN =
-      new Function<ByteString, Boolean, Void>()
+        return Boolean.TRUE;
+      }
+      else if (valueString.equals("false") || valueString.equals("no")
+          || valueString.equals("off") || valueString.equals("0"))
       {
-
-        public Boolean apply(ByteString value, Void p)
-        {
-          String valueString =
-              StaticUtils.toLowerCase(value.toString());
-
-          if (valueString.equals("true") || valueString.equals("yes")
-              || valueString.equals("on") || valueString.equals("1"))
-          {
-            return Boolean.TRUE;
-          }
-          else if (valueString.equals("false")
-              || valueString.equals("no") || valueString.equals("off")
-              || valueString.equals("0"))
-          {
-            return Boolean.FALSE;
-          }
-          else
-          {
-            throw new NumberFormatException("Invalid boolean value \""
-                + valueString + "\"");
-          }
-        }
-      };
-
-  private static final Function<ByteString, DN, Schema> BYTESTRING_TO_DN =
-      new Function<ByteString, DN, Schema>()
+        return Boolean.FALSE;
+      }
+      else
       {
+        throw new NumberFormatException("Invalid boolean value \""
+            + valueString + "\"");
+      }
+    }
+  };
 
-        public DN apply(ByteString value, Schema p)
-        {
-          // FIXME: what should we do if parsing fails?
+  private static final Function<ByteString, DN, Schema> BYTESTRING_TO_DN = new Function<ByteString, DN, Schema>()
+  {
 
-          // FIXME: we should have a ByteString valueOf implementation.
-          return DN.valueOf(value.toString(), p);
-        }
-      };
+    public DN apply(ByteString value, Schema p)
+    {
+      // FIXME: what should we do if parsing fails?
 
-  private static final Function<ByteString, Integer, Void> BYTESTRING_TO_INTEGER =
-      new Function<ByteString, Integer, Void>()
-      {
+      // FIXME: we should have a ByteString valueOf implementation.
+      return DN.valueOf(value.toString(), p);
+    }
+  };
 
-        public Integer apply(ByteString value, Void p)
-        {
-          // We do not use ByteString.toInt() as we are string based.
-          return Integer.valueOf(value.toString());
-        }
-      };
+  private static final Function<ByteString, Integer, Void> BYTESTRING_TO_INTEGER = new Function<ByteString, Integer, Void>()
+  {
 
-  private static final Function<ByteString, Long, Void> BYTESTRING_TO_LONG =
-      new Function<ByteString, Long, Void>()
-      {
+    public Integer apply(ByteString value, Void p)
+    {
+      // We do not use ByteString.toInt() as we are string based.
+      return Integer.valueOf(value.toString());
+    }
+  };
 
-        public Long apply(ByteString value, Void p)
-        {
-          // We do not use ByteString.toLong() as we are string based.
-          return Long.valueOf(value.toString());
-        }
-      };
+  private static final Function<ByteString, Long, Void> BYTESTRING_TO_LONG = new Function<ByteString, Long, Void>()
+  {
 
-  private static final Function<ByteString, String, Void> BYTESTRING_TO_STRING =
-      new Function<ByteString, String, Void>()
-      {
+    public Long apply(ByteString value, Void p)
+    {
+      // We do not use ByteString.toLong() as we are string based.
+      return Long.valueOf(value.toString());
+    }
+  };
 
-        public String apply(ByteString value, Void p)
-        {
-          return value.toString();
-        }
-      };
+  private static final Function<ByteString, String, Void> BYTESTRING_TO_STRING = new Function<ByteString, String, Void>()
+  {
 
-  private static final Function<String, String, Void> NORMALIZE_STRING =
-      new Function<String, String, Void>()
-      {
+    public String apply(ByteString value, Void p)
+    {
+      return value.toString();
+    }
+  };
 
-        public String apply(String value, Void p)
-        {
-          return StaticUtils.toLowerCase(value).trim();
-        }
-      };
+  private static final Function<Object, ByteString, Void> OBJECT_TO_BYTESTRING = new Function<Object, ByteString, Void>()
+  {
+
+    public ByteString apply(Object value, Void p)
+    {
+      return ByteString.valueOf(value);
+    }
+  };
+
+  private static final Function<String, String, Void> NORMALIZE_STRING = new Function<String, String, Void>()
+  {
+
+    public String apply(String value, Void p)
+    {
+      return StaticUtils.toLowerCase(value).trim();
+    }
+  };
 
 
 
@@ -337,6 +340,20 @@
 
 
 
+  /**
+   * Returns a function which converts an {@code Object} to a {@code
+   * ByteString} using the {@link ByteString#valueOf(Object)} method.
+   *
+   * @return A function which converts an {@code Object} to a {@code
+   *         ByteString}.
+   */
+  public static Function<Object, ByteString, Void> objectToByteString()
+  {
+    return OBJECT_TO_BYTESTRING;
+  }
+
+
+
   // Prevent instantiation
   private Functions()
   {

--
Gitblit v1.10.0