| | |
| | | */ |
| | | package org.opends.server.types; |
| | | |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.lang.reflect.Method; |
| | | import java.util.*; |
| | | |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | |
| | | * The default value to return if the attribute is empty. |
| | | * @return The first value decoded as a {@code T}. |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public <T> T as(final Function<ByteString, ? extends T, Void> f, final T defaultValue) { |
| | | if (!isEmpty(attribute)) { |
| | | return f.apply(attribute.iterator().next(), null); |
| | | ByteString value = attribute.iterator().next(); |
| | | try |
| | | { |
| | | // use existing already committed code |
| | | return f.apply(value, null); |
| | | } |
| | | catch (NoSuchMethodError e1) |
| | | { |
| | | try |
| | | { |
| | | // use the new code that is not yet committed and compiled (with Void parameter) |
| | | final Method method = f.getClass().getMethod("apply", Object.class, Void.class); |
| | | return (T) method.invoke(f, value, null); |
| | | } |
| | | catch (NoSuchMethodException e2) |
| | | { |
| | | return invokeForgerockUtilFunctionApply(f, value); |
| | | } |
| | | catch (SecurityException e2) |
| | | { |
| | | return invokeForgerockUtilFunctionApply(f, value); |
| | | } |
| | | catch (IllegalAccessException e) |
| | | { |
| | | return invokeForgerockUtilFunctionApply(f, value); |
| | | } |
| | | catch (InvocationTargetException e) |
| | | { |
| | | return invokeForgerockUtilFunctionApply(f, value); |
| | | } |
| | | catch (RuntimeException e2) |
| | | { |
| | | throw e2; |
| | | } |
| | | } |
| | | } else { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | private <T> T invokeForgerockUtilFunctionApply(final Function<ByteString, ? extends T, Void> f, ByteString value) |
| | | { |
| | | try |
| | | { |
| | | // use forgerock util Function |
| | | final Method method = f.getClass().getMethod("apply", Object.class); |
| | | return (T) method.invoke(f, value); |
| | | } |
| | | catch (RuntimeException e3) |
| | | { |
| | | throw e3; |
| | | } |
| | | catch (Exception e3) |
| | | { |
| | | throw new RuntimeException(e3); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Returns the first value decoded as a boolean, or {@code null} if the |
| | | * attribute does not contain any values. |