OPENDJ-1550: Replace SDK Function with Function from forgerock-util
* org.forgerock.opendj.ldap.Function
** Removed
* All impacted classes are now using org.forgerock.util.promise.Function
1 files deleted
19 files modified
| | |
| | | <differenceType>8001</differenceType> |
| | | <justification>OPENDJ-1536 Rename FutureResult and ErrorResultException classes hierarchy in the SDK to enhance code consistency</justification> |
| | | </difference> |
| | | <difference> |
| | | <className>org/forgerock/opendj/ldap/Functions</className> |
| | | <differenceType>7002</differenceType> |
| | | <method>*composeFirstP(*)</method> |
| | | <justification>OPENDJ-1550 Replace SDK Function with Function from forgerock-util</justification> |
| | | </difference> |
| | | <difference> |
| | | <className>org/forgerock/opendj/ldap/Functions</className> |
| | | <differenceType>7002</differenceType> |
| | | <method>*composeSecondP(*)</method> |
| | | <justification>OPENDJ-1550 Replace SDK Function with Function from forgerock-util</justification> |
| | | </difference> |
| | | <difference> |
| | | <className>org/forgerock/opendj/ldap/Functions</className> |
| | | <differenceType>7002</differenceType> |
| | | <method>*fixedFunction(*)</method> |
| | | <justification>OPENDJ-1550 Replace SDK Function with Function from forgerock-util</justification> |
| | | </difference> |
| | | <difference> |
| | | <className>org/forgerock/opendj/ldap/Function</className> |
| | | <differenceType>8001</differenceType> |
| | | <justification>OPENDJ-1550 Replace SDK Function with Function from forgerock-util</justification> |
| | | </difference> |
| | | <difference> |
| | | <className>org/forgerock/opendj/ldap/AttributeParser</className> |
| | | <differenceType>7005</differenceType> |
| | | <method>*as*(org.forgerock.opendj.ldap.Function*)</method> |
| | | <to>*as*(org.forgerock.util.promise.Function*)</to> |
| | | <justification>OPENDJ-1550 Replace SDK Function with Function from forgerock-util</justification> |
| | | </difference> |
| | | <difference> |
| | | <className>org/forgerock/opendj/ldap/Functions</className> |
| | | <differenceType>7005</differenceType> |
| | | <method>*compose(org.forgerock.opendj.ldap.Function, org.forgerock.opendj.ldap.Function)</method> |
| | | <to>*compose(org.forgerock.util.promise.Function, org.forgerock.util.promise.Function)</to> |
| | | <justification>OPENDJ-1550 Replace SDK Function with Function from forgerock-util</justification> |
| | | </difference> |
| | | <difference> |
| | | <className>org/forgerock/opendj/ldap/Functions</className> |
| | | <differenceType>7006</differenceType> |
| | | <method>*</method> |
| | | <to>org.forgerock.util.promise.Function</to> |
| | | <justification>OPENDJ-1550 Replace SDK Function with Function from forgerock-util</justification> |
| | | </difference> |
| | | </differences> |
| | |
| | | * |
| | | * |
| | | * Copyright 2009-2010 Sun Microsystems, Inc. |
| | | * Portions Copyright 2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package com.forgerock.opendj.util; |
| | | |
| | | import java.util.AbstractCollection; |
| | |
| | | import java.util.List; |
| | | import java.util.ListIterator; |
| | | |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | /** |
| | | * Additional {@code Collection} based utility methods. |
| | | */ |
| | | public final class Collections2 { |
| | | private static class TransformedCollection<M, N, P, C extends Collection<M>> extends |
| | | private static class TransformedCollection<M, N, C extends Collection<M>> extends |
| | | AbstractCollection<N> implements Collection<N> { |
| | | |
| | | protected final C collection; |
| | | |
| | | protected final Function<? super M, ? extends N, P> funcMtoN; |
| | | protected final Function<? super M, ? extends N, NeverThrowsException> funcMtoN; |
| | | |
| | | protected final Function<? super N, ? extends M, P> funcNtoM; |
| | | |
| | | protected final P p; |
| | | protected final Function<? super N, ? extends M, NeverThrowsException> funcNtoM; |
| | | |
| | | protected TransformedCollection(final C collection, |
| | | final Function<? super M, ? extends N, P> funcMtoN, |
| | | final Function<? super N, ? extends M, P> funcNtoM, final P p) { |
| | | final Function<? super M, ? extends N, NeverThrowsException> funcMtoN, |
| | | final Function<? super N, ? extends M, NeverThrowsException> funcNtoM) { |
| | | this.collection = collection; |
| | | this.funcMtoN = funcMtoN; |
| | | this.funcNtoM = funcNtoM; |
| | | this.p = p; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public boolean add(final N e) { |
| | | return collection.add(funcNtoM.apply(e, p)); |
| | | return collection.add(funcNtoM.apply(e)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void clear() { |
| | | collection.clear(); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | @SuppressWarnings("unchecked") |
| | | public boolean contains(final Object o) { |
| | | final N tmp = (N) o; |
| | | return collection.contains(funcNtoM.apply(tmp, p)); |
| | | return collection.contains(funcNtoM.apply((N) o)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public boolean isEmpty() { |
| | | return collection.isEmpty(); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public Iterator<N> iterator() { |
| | | return Iterators.transformedIterator(collection.iterator(), funcMtoN, p); |
| | | return Iterators.transformedIterator(collection.iterator(), funcMtoN); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | @SuppressWarnings("unchecked") |
| | | public boolean remove(final Object o) { |
| | | final N tmp = (N) o; |
| | | return collection.remove(funcNtoM.apply(tmp, p)); |
| | | return collection.remove(funcNtoM.apply((N) o)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public int size() { |
| | | return collection.size(); |
| | |
| | | |
| | | } |
| | | |
| | | private static final class TransformedList<M, N, P> extends |
| | | TransformedCollection<M, N, P, List<M>> implements List<N> { |
| | | private static final class TransformedList<M, N> extends |
| | | TransformedCollection<M, N, List<M>> implements List<N> { |
| | | |
| | | private TransformedList(final List<M> list, |
| | | final Function<? super M, ? extends N, P> funcMtoN, |
| | | final Function<? super N, ? extends M, P> funcNtoM, final P p) { |
| | | super(list, funcMtoN, funcNtoM, p); |
| | | final Function<? super M, ? extends N, NeverThrowsException> funcMtoN, |
| | | final Function<? super N, ? extends M, NeverThrowsException> funcNtoM) { |
| | | super(list, funcMtoN, funcNtoM); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public void add(final int index, final N element) { |
| | | collection.add(index, funcNtoM.apply(element, p)); |
| | | collection.add(index, funcNtoM.apply(element)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public boolean addAll(final int index, final Collection<? extends N> c) { |
| | | // We cannot transform c here due to type-safety. |
| | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public N get(final int index) { |
| | | return funcMtoN.apply(collection.get(index), p); |
| | | return funcMtoN.apply(collection.get(index)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | @SuppressWarnings("unchecked") |
| | | public int indexOf(final Object o) { |
| | | final N tmp = (N) o; |
| | | return collection.indexOf(funcNtoM.apply(tmp, p)); |
| | | return collection.indexOf(funcNtoM.apply((N) o)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | @SuppressWarnings("unchecked") |
| | | public int lastIndexOf(final Object o) { |
| | | final N tmp = (N) o; |
| | | return collection.lastIndexOf(funcNtoM.apply(tmp, p)); |
| | | return collection.lastIndexOf(funcNtoM.apply((N) o)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public ListIterator<N> listIterator() { |
| | | return listIterator(0); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public ListIterator<N> listIterator(final int index) { |
| | | final ListIterator<M> iterator = collection.listIterator(index); |
| | |
| | | |
| | | @Override |
| | | public void add(final N e) { |
| | | iterator.add(funcNtoM.apply(e, p)); |
| | | iterator.add(funcNtoM.apply(e)); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public N next() { |
| | | return funcMtoN.apply(iterator.next(), p); |
| | | return funcMtoN.apply(iterator.next()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public N previous() { |
| | | return funcMtoN.apply(iterator.previous(), p); |
| | | return funcMtoN.apply(iterator.previous()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public void set(final N e) { |
| | | iterator.set(funcNtoM.apply(e, p)); |
| | | iterator.set(funcNtoM.apply(e)); |
| | | } |
| | | |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public N remove(final int index) { |
| | | return funcMtoN.apply(collection.remove(index), p); |
| | | return funcMtoN.apply(collection.remove(index)); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public N set(final int index, final N element) { |
| | | final M result = collection.set(index, funcNtoM.apply(element, p)); |
| | | return funcMtoN.apply(result, p); |
| | | final M result = collection.set(index, funcNtoM.apply(element)); |
| | | return funcMtoN.apply(result); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | @Override |
| | | public List<N> subList(final int fromIndex, final int toIndex) { |
| | | final List<M> subList = collection.subList(fromIndex, toIndex); |
| | | return new TransformedList<M, N, P>(subList, funcMtoN, funcNtoM, p); |
| | | return new TransformedList<M, N>(subList, funcMtoN, funcNtoM); |
| | | } |
| | | |
| | | } |
| | |
| | | * The type of elements contained in {@code collection}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned collection. |
| | | * @param <P> |
| | | * The type of the additional parameter to the function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for functions |
| | | * that do not need an additional parameter. |
| | | * @param collection |
| | | * The collection to be transformed. |
| | | * @param funcMtoN |
| | | * A function which maps values of type {@code M} to values of |
| | | * type {@code N}. This function will be used when retrieving |
| | | * values from {@code collection}. |
| | | * @param funcNtoM |
| | | * A function which maps values of type {@code N} to values of |
| | | * type {@code M}. This function will be used when performing |
| | | * queries and adding values to {@code collection} . |
| | | * @param p |
| | | * A predicate specified parameter. |
| | | * @return A view of {@code collection} whose values have been mapped to |
| | | * elements of type {@code N} using {@code funcMtoN}. |
| | | */ |
| | | public static <M, N, P> Collection<N> transformedCollection(final Collection<M> collection, |
| | | final Function<? super M, ? extends N, P> funcMtoN, |
| | | final Function<? super N, ? extends M, P> funcNtoM, final P p) { |
| | | return new TransformedCollection<M, N, P, Collection<M>>(collection, funcMtoN, funcNtoM, p); |
| | | } |
| | | |
| | | /** |
| | | * Returns a view of {@code collection} whose values have been mapped to |
| | | * elements of type {@code N} using {@code funcMtoN}. The returned |
| | | * collection supports all operations. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code collection}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned collection. |
| | | * @param collection |
| | | * The collection to be transformed. |
| | | * @param funcMtoN |
| | |
| | | * elements of type {@code N} using {@code funcMtoN}. |
| | | */ |
| | | public static <M, N> Collection<N> transformedCollection(final Collection<M> collection, |
| | | final Function<? super M, ? extends N, Void> funcMtoN, |
| | | final Function<? super N, ? extends M, Void> funcNtoM) { |
| | | return new TransformedCollection<M, N, Void, Collection<M>>(collection, funcMtoN, funcNtoM, |
| | | null); |
| | | } |
| | | |
| | | /** |
| | | * Returns a view of {@code list} whose values have been mapped to elements |
| | | * of type {@code N} using {@code funcMtoN}. The returned list supports all |
| | | * operations. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code list}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned list. |
| | | * @param <P> |
| | | * The type of the additional parameter to the function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for functions |
| | | * that do not need an additional parameter. |
| | | * @param list |
| | | * The list to be transformed. |
| | | * @param funcMtoN |
| | | * A function which maps values of type {@code M} to values of |
| | | * type {@code N}. This function will be used when retrieving |
| | | * values from {@code list}. |
| | | * @param funcNtoM |
| | | * A function which maps values of type {@code N} to values of |
| | | * type {@code M}. This function will be used when performing |
| | | * queries and adding values to {@code list} . |
| | | * @param p |
| | | * A predicate specified parameter. |
| | | * @return A view of {@code list} whose values have been mapped to elements |
| | | * of type {@code N} using {@code funcMtoN}. |
| | | */ |
| | | public static <M, N, P> List<N> transformedList(final List<M> list, |
| | | final Function<? super M, ? extends N, P> funcMtoN, |
| | | final Function<? super N, ? extends M, P> funcNtoM, final P p) { |
| | | return new TransformedList<M, N, P>(list, funcMtoN, funcNtoM, p); |
| | | final Function<? super M, ? extends N, NeverThrowsException> funcMtoN, |
| | | final Function<? super N, ? extends M, NeverThrowsException> funcNtoM) { |
| | | return new TransformedCollection<M, N, Collection<M>>(collection, funcMtoN, funcNtoM); |
| | | } |
| | | |
| | | /** |
| | |
| | | * of type {@code N} using {@code funcMtoN}. |
| | | */ |
| | | public static <M, N> List<N> transformedList(final List<M> list, |
| | | final Function<? super M, ? extends N, Void> funcMtoN, |
| | | final Function<? super N, ? extends M, Void> funcNtoM) { |
| | | return new TransformedList<M, N, Void>(list, funcMtoN, funcNtoM, null); |
| | | final Function<? super M, ? extends N, NeverThrowsException> funcMtoN, |
| | | final Function<? super N, ? extends M, NeverThrowsException> funcNtoM) { |
| | | return new TransformedList<M, N>(list, funcMtoN, funcNtoM); |
| | | } |
| | | |
| | | // Prevent instantiation |
| | | /** Prevent instantiation. */ |
| | | private Collections2() { |
| | | // Do nothing. |
| | | } |
| | |
| | | * |
| | | * |
| | | * Copyright 2009-2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2013 ForgeRock AS. |
| | | * Portions copyright 2013-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package com.forgerock.opendj.util; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.Iterator; |
| | | |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | /** |
| | | * Utility methods for manipulating {@link Iterable}s. |
| | |
| | | } |
| | | } |
| | | |
| | | private static final class TransformedIterable<M, N, P> extends AbstractIterable<N> { |
| | | private final Function<? super M, ? extends N, P> function; |
| | | private static final class TransformedIterable<M, N> extends AbstractIterable<N> { |
| | | private final Function<? super M, ? extends N, NeverThrowsException> function; |
| | | private final Iterable<M> iterable; |
| | | private final P parameter; |
| | | |
| | | // Constructed via factory methods. |
| | | private TransformedIterable(final Iterable<M> iterable, |
| | | final Function<? super M, ? extends N, P> function, final P p) { |
| | | final Function<? super M, ? extends N, NeverThrowsException> function) { |
| | | this.iterable = iterable; |
| | | this.function = function; |
| | | this.parameter = p; |
| | | } |
| | | |
| | | @Override |
| | | public Iterator<N> iterator() { |
| | | return Iterators.transformedIterator(iterable.iterator(), function, parameter); |
| | | return Iterators.transformedIterator(iterable.iterator(), function); |
| | | } |
| | | } |
| | | |
| | |
| | | * The type of elements contained in {@code iterable}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned iterable. |
| | | * @param <P> |
| | | * The type of the additional parameter to the function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for functions |
| | | * that do not need an additional parameter. |
| | | * @param iterable |
| | | * The iterable to be transformed. |
| | | * @param function |
| | | * The function. |
| | | * @param p |
| | | * A predicate specified parameter. |
| | | * @return A view of {@code iterable} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. |
| | | */ |
| | | public static <M, N, P> Iterable<N> transformedIterable(final Iterable<M> iterable, |
| | | final Function<? super M, ? extends N, P> function, final P p) { |
| | | return new TransformedIterable<M, N, P>(iterable, function, p); |
| | | } |
| | | |
| | | /** |
| | | * Returns a view of {@code iterable} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. The returned |
| | | * iterable's iterator supports element removal via the {@code remove()} |
| | | * method subject to any constraints imposed by {@code iterable}. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code iterable}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned iterable. |
| | | * @param iterable |
| | | * The iterable to be transformed. |
| | | * @param function |
| | |
| | | * elements of type {@code N} using {@code function}. |
| | | */ |
| | | public static <M, N> Iterable<N> transformedIterable(final Iterable<M> iterable, |
| | | final Function<? super M, ? extends N, Void> function) { |
| | | return new TransformedIterable<M, N, Void>(iterable, function, null); |
| | | final Function<? super M, ? extends N, NeverThrowsException> function) { |
| | | return new TransformedIterable<M, N>(iterable, function); |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * |
| | | * Copyright 2009-2010 Sun Microsystems, Inc. |
| | | * Portions Copyright 2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package com.forgerock.opendj.util; |
| | |
| | | import java.util.Iterator; |
| | | import java.util.NoSuchElementException; |
| | | |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | /** |
| | | * Utility methods for manipulating {@link Iterator}s. |
| | |
| | | private int i = 0; |
| | | private final M[] a; |
| | | |
| | | // Constructed via factory methods. |
| | | /** Constructed via factory methods. */ |
| | | private ArrayIterator(final M[] a) { |
| | | this.a = a; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public boolean hasNext() { |
| | | return i < a.length; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public M next() { |
| | | if (hasNext()) { |
| | | return a[i++]; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public void remove() { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | |
| | | } |
| | | |
| | | private static final class EmptyIterator<M> implements Iterator<M> { |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public boolean hasNext() { |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public M next() { |
| | | throw new NoSuchElementException(); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public void remove() { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | |
| | | private final P parameter; |
| | | private final Predicate<? super M, P> predicate; |
| | | |
| | | // Constructed via factory methods. |
| | | /** Constructed via factory methods. */ |
| | | private FilteredIterator(final Iterator<M> iterator, |
| | | final Predicate<? super M, P> predicate, final P p) { |
| | | this.iterator = iterator; |
| | |
| | | this.parameter = p; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public boolean hasNext() { |
| | | if (hasNextMustIterate) { |
| | | hasNextMustIterate = false; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public M next() { |
| | | if (!hasNext()) { |
| | | throw new NoSuchElementException(); |
| | |
| | | return next; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public void remove() { |
| | | iterator.remove(); |
| | | } |
| | |
| | | private static final class SingletonIterator<M> implements Iterator<M> { |
| | | private M value; |
| | | |
| | | // Constructed via factory methods. |
| | | /** Constructed via factory methods. */ |
| | | private SingletonIterator(final M value) { |
| | | this.value = value; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public boolean hasNext() { |
| | | return value != null; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public M next() { |
| | | if (value != null) { |
| | | final M tmp = value; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public void remove() { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | | |
| | | } |
| | | |
| | | private static final class TransformedIterator<M, N, P> implements Iterator<N> { |
| | | private static final class TransformedIterator<M, N> implements Iterator<N> { |
| | | |
| | | private final Function<? super M, ? extends N, P> function; |
| | | private final Function<? super M, ? extends N, NeverThrowsException> function; |
| | | private final Iterator<M> iterator; |
| | | private final P parameter; |
| | | |
| | | // Constructed via factory methods. |
| | | /** Constructed via factory methods. */ |
| | | private TransformedIterator(final Iterator<M> iterator, |
| | | final Function<? super M, ? extends N, P> function, final P p) { |
| | | final Function<? super M, ? extends N, NeverThrowsException> function) { |
| | | this.iterator = iterator; |
| | | this.function = function; |
| | | this.parameter = p; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public boolean hasNext() { |
| | | return iterator.hasNext(); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public N next() { |
| | | return function.apply(iterator.next(), parameter); |
| | | return function.apply(iterator.next()); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public void remove() { |
| | | iterator.remove(); |
| | | } |
| | |
| | | this.iterator = iterator; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public boolean hasNext() { |
| | | return iterator.hasNext(); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public M next() { |
| | | return iterator.next(); |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | /** {@inheritDoc} */ |
| | | public void remove() { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | |
| | | * The type of elements contained in {@code iterator}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned iterator. |
| | | * @param <P> |
| | | * The type of the additional parameter to the function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for functions |
| | | * that do not need an additional parameter. |
| | | * @param iterator |
| | | * The iterator to be transformed. |
| | | * @param function |
| | | * The function. |
| | | * @param p |
| | | * A predicate specified parameter. |
| | | * @return A view of {@code iterator} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. |
| | | */ |
| | | public static <M, N, P> Iterator<N> transformedIterator(final Iterator<M> iterator, |
| | | final Function<? super M, ? extends N, P> function, final P p) { |
| | | return new TransformedIterator<M, N, P>(iterator, function, p); |
| | | } |
| | | |
| | | /** |
| | | * Returns a view of {@code iterator} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. The returned iterator |
| | | * supports element removal via the {@code remove()} method subject to any |
| | | * constraints imposed by {@code iterator}. |
| | | * |
| | | * @param <M> |
| | | * The type of elements contained in {@code iterator}. |
| | | * @param <N> |
| | | * The type of elements contained in the returned iterator. |
| | | * @param iterator |
| | | * The iterator to be transformed. |
| | | * @param function |
| | |
| | | * elements of type {@code N} using {@code function}. |
| | | */ |
| | | public static <M, N> Iterator<N> transformedIterator(final Iterator<M> iterator, |
| | | final Function<? super M, ? extends N, Void> function) { |
| | | return new TransformedIterator<M, N, Void>(iterator, function, null); |
| | | final Function<? super M, ? extends N, NeverThrowsException> function) { |
| | | return new TransformedIterator<M, N>(iterator, function); |
| | | } |
| | | |
| | | /** |
| | |
| | | return new UnmodifiableIterator<M>(iterator); |
| | | } |
| | | |
| | | // Prevent instantiation |
| | | /** Prevent instantiation. */ |
| | | private Iterators() { |
| | | // Do nothing. |
| | | } |
| | |
| | | * CDDL HEADER END |
| | | * |
| | | * |
| | | * Copyright 2012 ForgeRock AS. |
| | | * Copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap; |
| | | |
| | | import static com.forgerock.opendj.util.Collections2.transformedCollection; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | |
| | | import java.util.Set; |
| | | |
| | | import org.forgerock.opendj.ldap.schema.Schema; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import static com.forgerock.opendj.util.Collections2.*; |
| | | |
| | | /** |
| | | * A fluent API for parsing attributes as different types of object. An |
| | |
| | | * The function which should be used to decode the value. |
| | | * @return The first value decoded as a {@code T}. |
| | | */ |
| | | public <T> T as(final Function<ByteString, ? extends T, Void> f) { |
| | | public <T> T as(final Function<ByteString, ? extends T, NeverThrowsException> f) { |
| | | return as(f, null); |
| | | } |
| | | |
| | |
| | | * The default value to return if the attribute is empty. |
| | | * @return The first value decoded as a {@code T}. |
| | | */ |
| | | public <T> T as(final Function<ByteString, ? extends T, Void> f, final T defaultValue) { |
| | | public <T> T as(final Function<ByteString, ? extends T, NeverThrowsException> f, final T defaultValue) { |
| | | if (!isEmpty(attribute)) { |
| | | return f.apply(attribute.firstValue(), null); |
| | | return f.apply(attribute.firstValue()); |
| | | } else { |
| | | return defaultValue; |
| | | } |
| | |
| | | * The default values to return if the attribute is empty. |
| | | * @return The values decoded as a set of {@code T}s. |
| | | */ |
| | | public <T> Set<T> asSetOf(final Function<ByteString, ? extends T, Void> f, |
| | | public <T> Set<T> asSetOf(final Function<ByteString, ? extends T, NeverThrowsException> f, |
| | | final Collection<? extends T> defaultValues) { |
| | | if (!isEmpty(attribute)) { |
| | | final LinkedHashSet<T> result = new LinkedHashSet<T>(attribute.size()); |
| | | for (final ByteString b : attribute) { |
| | | result.add(f.apply(b, null)); |
| | | result.add(f.apply(b)); |
| | | } |
| | | return result; |
| | | } else if (defaultValues != null) { |
| | |
| | | * The default values to return if the attribute is empty. |
| | | * @return The values decoded as a set of {@code T}s. |
| | | */ |
| | | public <T> Set<T> asSetOf(final Function<ByteString, ? extends T, Void> f, |
| | | public <T> Set<T> asSetOf(final Function<ByteString, ? extends T, NeverThrowsException> f, |
| | | final T... defaultValues) { |
| | | return asSetOf(f, Arrays.asList(defaultValues)); |
| | | } |
| | |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | * Portions Copyright 2011-2014 ForgeRock AS |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap; |
| | | |
| | | import static com.forgerock.opendj.ldap.CoreMessages.*; |
| | | import static org.forgerock.opendj.ldap.AttributeDescription.objectClass; |
| | | import static org.forgerock.opendj.ldap.LdapException.newLdapException; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | |
| | | import org.forgerock.opendj.ldap.schema.UnknownSchemaElementException; |
| | | import org.forgerock.opendj.ldif.LDIF; |
| | | import org.forgerock.util.Reject; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import com.forgerock.opendj.util.Iterables; |
| | | |
| | | import static org.forgerock.opendj.ldap.AttributeDescription.*; |
| | | import static org.forgerock.opendj.ldap.LdapException.*; |
| | | |
| | | import static com.forgerock.opendj.ldap.CoreMessages.*; |
| | | |
| | | /** |
| | | * This class contains methods for creating and manipulating entries. |
| | | * |
| | |
| | | private static final AttributeFilter USER_ATTRIBUTES_ONLY_FILTER = new AttributeFilter(); |
| | | private static final DiffOptions DEFAULT_DIFF_OPTIONS = new DiffOptions(); |
| | | |
| | | private static final Function<Attribute, Attribute, Void> UNMODIFIABLE_ATTRIBUTE_FUNCTION = |
| | | new Function<Attribute, Attribute, Void>() { |
| | | |
| | | private static final Function<Attribute, Attribute, NeverThrowsException> UNMODIFIABLE_ATTRIBUTE_FUNCTION = |
| | | new Function<Attribute, Attribute, NeverThrowsException>() { |
| | | @Override |
| | | public Attribute apply(final Attribute value, final Void p) { |
| | | public Attribute apply(final Attribute value) { |
| | | return Attributes.unmodifiableAttribute(value); |
| | | } |
| | | |
| | |
| | | * |
| | | * |
| | | * Copyright 2009-2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012 ForgeRock AS. |
| | | * Portions copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap; |
| | | |
| | | import static com.forgerock.opendj.ldap.CoreMessages.FUNCTIONS_TO_INTEGER_FAIL; |
| | | import static com.forgerock.opendj.ldap.CoreMessages.FUNCTIONS_TO_LONG_FAIL; |
| | | import static com.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_ILLEGAL_BOOLEAN; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.i18n.LocalizedIllegalArgumentException; |
| | | import org.forgerock.opendj.ldap.schema.Schema; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import com.forgerock.opendj.util.StaticUtils; |
| | | |
| | | import static org.forgerock.opendj.ldap.schema.Schema.*; |
| | | |
| | | import static com.forgerock.opendj.ldap.CoreMessages.*; |
| | | |
| | | /** |
| | | * Common {@link Function} implementations which may be used when parsing |
| | | * attributes. |
| | |
| | | */ |
| | | public final class Functions { |
| | | |
| | | private static final class FixedFunction<M, N, P> implements Function<M, N, Void> { |
| | | private final Function<M, N, P> function; |
| | | |
| | | private final P parameter; |
| | | |
| | | private FixedFunction(final Function<M, N, P> function, final P p) { |
| | | this.function = function; |
| | | this.parameter = p; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public N apply(final M value, final Void p) { |
| | | return function.apply(value, parameter); |
| | | } |
| | | |
| | | } |
| | | |
| | | private static final Function<ByteString, String, Void> BYTESTRING_TO_STRING = |
| | | new Function<ByteString, String, Void>() { |
| | | public String apply(final ByteString value, final Void p) { |
| | | private static final Function<ByteString, String, NeverThrowsException> BYTESTRING_TO_STRING = |
| | | new Function<ByteString, String, NeverThrowsException>() { |
| | | public String apply(final ByteString value) { |
| | | return value.toString(); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<Object, Object, Void> IDENTITY = |
| | | new Function<Object, Object, Void>() { |
| | | public Object apply(final Object value, final Void p) { |
| | | private static final Function<Object, Object, NeverThrowsException> IDENTITY = |
| | | new Function<Object, Object, NeverThrowsException>() { |
| | | public Object apply(final Object value) { |
| | | return value; |
| | | } |
| | | }; |
| | | |
| | | private static final Function<String, String, Void> NORMALIZE_STRING = |
| | | new Function<String, String, Void>() { |
| | | public String apply(final String value, final Void p) { |
| | | private static final Function<String, String, NeverThrowsException> NORMALIZE_STRING = |
| | | new Function<String, String, NeverThrowsException>() { |
| | | public String apply(final String value) { |
| | | return StaticUtils.toLowerCase(value).trim(); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<Object, ByteString, Void> OBJECT_TO_BYTESTRING = |
| | | new Function<Object, ByteString, Void>() { |
| | | public ByteString apply(final Object value, final Void p) { |
| | | private static final Function<Object, ByteString, NeverThrowsException> OBJECT_TO_BYTESTRING = |
| | | new Function<Object, ByteString, NeverThrowsException>() { |
| | | public ByteString apply(final Object value) { |
| | | return ByteString.valueOf(value); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<String, AttributeDescription, Schema> STRING_TO_ATTRIBUTE_DESCRIPTION = |
| | | new Function<String, AttributeDescription, Schema>() { |
| | | public AttributeDescription apply(final String value, final Schema p) { |
| | | return AttributeDescription.valueOf(value, p); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<String, Boolean, Void> STRING_TO_BOOLEAN = |
| | | new Function<String, Boolean, Void>() { |
| | | public Boolean apply(final String value, final Void p) { |
| | | private static final Function<String, Boolean, NeverThrowsException> STRING_TO_BOOLEAN = |
| | | new Function<String, Boolean, NeverThrowsException>() { |
| | | public Boolean apply(final String value) { |
| | | final String valueString = StaticUtils.toLowerCase(value); |
| | | |
| | | if (valueString.equals("true") || valueString.equals("yes") |
| | |
| | | } |
| | | }; |
| | | |
| | | private static final Function<String, DN, Schema> STRING_TO_DN = |
| | | new Function<String, DN, Schema>() { |
| | | public DN apply(final String value, final Schema p) { |
| | | return DN.valueOf(value, p); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<String, GeneralizedTime, Void> STRING_TO_GENERALIZED_TIME = |
| | | new Function<String, GeneralizedTime, Void>() { |
| | | public GeneralizedTime apply(final String value, final Void p) { |
| | | private static final Function<String, GeneralizedTime, NeverThrowsException> STRING_TO_GENERALIZED_TIME = |
| | | new Function<String, GeneralizedTime, NeverThrowsException>() { |
| | | public GeneralizedTime apply(final String value) { |
| | | return GeneralizedTime.valueOf(value); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<String, Integer, Void> STRING_TO_INTEGER = |
| | | new Function<String, Integer, Void>() { |
| | | public Integer apply(final String value, final Void p) { |
| | | private static final Function<String, Integer, NeverThrowsException> STRING_TO_INTEGER = |
| | | new Function<String, Integer, NeverThrowsException>() { |
| | | public Integer apply(final String value) { |
| | | try { |
| | | return Integer.valueOf(value); |
| | | } catch (final NumberFormatException e) { |
| | |
| | | } |
| | | }; |
| | | |
| | | private static final Function<String, Long, Void> STRING_TO_LONG = |
| | | new Function<String, Long, Void>() { |
| | | public Long apply(final String value, final Void p) { |
| | | private static final Function<String, Long, NeverThrowsException> STRING_TO_LONG = |
| | | new Function<String, Long, NeverThrowsException>() { |
| | | public Long apply(final String value) { |
| | | try { |
| | | return Long.valueOf(value); |
| | | } catch (final NumberFormatException e) { |
| | |
| | | } |
| | | }; |
| | | |
| | | private static final Function<ByteString, AttributeDescription, Schema> BYTESTRING_TO_ATTRIBUTE_DESCRIPTION = |
| | | composeSecondP(byteStringToString(), STRING_TO_ATTRIBUTE_DESCRIPTION); |
| | | |
| | | private static final Function<ByteString, Boolean, Void> BYTESTRING_TO_BOOLEAN = compose( |
| | | private static final Function<ByteString, Boolean, NeverThrowsException> BYTESTRING_TO_BOOLEAN = compose( |
| | | byteStringToString(), STRING_TO_BOOLEAN); |
| | | |
| | | private static final Function<ByteString, DN, Schema> BYTESTRING_TO_DN = composeSecondP( |
| | | byteStringToString(), STRING_TO_DN); |
| | | |
| | | private static final Function<ByteString, GeneralizedTime, Void> BYTESTRING_TO_GENERALIZED_TIME = |
| | | private static final Function<ByteString, GeneralizedTime, NeverThrowsException> BYTESTRING_TO_GENERALIZED_TIME = |
| | | compose(byteStringToString(), STRING_TO_GENERALIZED_TIME); |
| | | |
| | | private static final Function<ByteString, Integer, Void> BYTESTRING_TO_INTEGER = compose( |
| | | private static final Function<ByteString, Integer, NeverThrowsException> BYTESTRING_TO_INTEGER = compose( |
| | | byteStringToString(), STRING_TO_INTEGER); |
| | | |
| | | private static final Function<ByteString, Long, Void> BYTESTRING_TO_LONG = compose( |
| | | private static final Function<ByteString, Long, NeverThrowsException> BYTESTRING_TO_LONG = compose( |
| | | byteStringToString(), STRING_TO_LONG); |
| | | |
| | | /** |
| | |
| | | * The second function which will produce the result. |
| | | * @return The composition. |
| | | */ |
| | | public static <M, X, N> Function<M, N, Void> compose(final Function<M, X, Void> first, |
| | | final Function<X, N, Void> second) { |
| | | return new Function<M, N, Void>() { |
| | | public N apply(final M value, final Void p) { |
| | | final X tmp = first.apply(value, p); |
| | | return second.apply(tmp, p); |
| | | public static <M, X, N> Function<M, N, NeverThrowsException> compose( |
| | | final Function<M, X, NeverThrowsException> first, final Function<X, N, NeverThrowsException> second) { |
| | | return new Function<M, N, NeverThrowsException>() { |
| | | public N apply(final M value) { |
| | | return second.apply(first.apply(value)); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * Returns the composition of two functions. The result of the first |
| | | * function will be passed to the second. The first function will be passed |
| | | * an additional parameter. |
| | | * |
| | | * @param <M> |
| | | * The type of input values transformed by this function. |
| | | * @param <N> |
| | | * The type of output values returned by this function. |
| | | * @param <X> |
| | | * The type of intermediate values passed between the two |
| | | * functions. |
| | | * @param <P> |
| | | * The type of the additional parameter to the first function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for functions |
| | | * that do not need an additional parameter. |
| | | * @param first |
| | | * The first function which will consume the input. |
| | | * @param second |
| | | * The second function which will produce the result. |
| | | * @return The composition. |
| | | */ |
| | | public static <M, X, N, P> Function<M, N, P> composeFirstP(final Function<M, X, P> first, |
| | | final Function<X, N, Void> second) { |
| | | return new Function<M, N, P>() { |
| | | public N apply(final M value, final P p) { |
| | | final X tmp = first.apply(value, p); |
| | | return second.apply(tmp, null); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * Returns the composition of two functions. The result of the first |
| | | * function will be passed to the second. The second function will be passed |
| | | * an additional parameter. |
| | | * |
| | | * @param <M> |
| | | * The type of input values transformed by this function. |
| | | * @param <N> |
| | | * The type of output values returned by this function. |
| | | * @param <X> |
| | | * The type of intermediate values passed between the two |
| | | * functions. |
| | | * @param <P> |
| | | * The type of the additional parameter to the second function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for functions |
| | | * that do not need an additional parameter. |
| | | * @param first |
| | | * The first function which will consume the input. |
| | | * @param second |
| | | * The second function which will produce the result. |
| | | * @return The composition. |
| | | */ |
| | | public static <M, X, N, P> Function<M, N, P> composeSecondP(final Function<M, X, Void> first, |
| | | final Function<X, N, P> second) { |
| | | return new Function<M, N, P>() { |
| | | public N apply(final M value, final P p) { |
| | | final X tmp = first.apply(value, null); |
| | | return second.apply(tmp, p); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * Returns a function which which always invokes {@code function} with |
| | | * {@code p}. |
| | | * |
| | | * @param <M> |
| | | * The type of input values transformed by this function. |
| | | * @param <N> |
| | | * The type of output values return by this function. |
| | | * @param <P> |
| | | * The type of the additional parameter to this function's |
| | | * {@code apply} method. Use {@link java.lang.Void} for functions |
| | | * that do not need an additional parameter. |
| | | * @param function |
| | | * The function to wrap. |
| | | * @param p |
| | | * The parameter which will always be passed to {@code function}. |
| | | * @return A function which which always invokes {@code function} with |
| | | * {@code p}. |
| | | */ |
| | | public static <M, N, P> Function<M, N, Void> fixedFunction(final Function<M, N, P> function, |
| | | final P p) { |
| | | return new FixedFunction<M, N, P>(function, p); |
| | | } |
| | | |
| | | /** |
| | | * Returns a function which always returns the value that it was provided |
| | | * with. |
| | | * |
| | |
| | | * with. |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <M> Function<M, M, Void> identityFunction() { |
| | | return (Function<M, M, Void>) IDENTITY; |
| | | public static <M> Function<M, M, NeverThrowsException> identityFunction() { |
| | | return (Function<M, M, NeverThrowsException>) IDENTITY; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return A function which converts a {@code String} to lower case using |
| | | * {@link StaticUtils#toLowerCase} and then trims it. |
| | | */ |
| | | public static Function<String, String, Void> normalizeString() { |
| | | public static Function<String, String, NeverThrowsException> normalizeString() { |
| | | return NORMALIZE_STRING; |
| | | } |
| | | |
| | |
| | | * @return A function which converts an {@code Object} to a |
| | | * {@code ByteString} . |
| | | */ |
| | | public static Function<Object, ByteString, Void> objectToByteString() { |
| | | public static Function<Object, ByteString, NeverThrowsException> objectToByteString() { |
| | | return OBJECT_TO_BYTESTRING; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code AttributeDescription}s. |
| | | */ |
| | | public static Function<String, AttributeDescription, Void> stringToAttributeDescription() { |
| | | return fixedFunction(STRING_TO_ATTRIBUTE_DESCRIPTION, Schema.getDefaultSchema()); |
| | | public static Function<String, AttributeDescription, NeverThrowsException> stringToAttributeDescription() { |
| | | return stringToAttributeDescription(getDefaultSchema()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * The schema to use for decoding attribute descriptions. |
| | | * @return A function which parses {@code AttributeDescription}s. |
| | | */ |
| | | public static Function<String, AttributeDescription, Void> stringToAttributeDescription( |
| | | public static Function<String, AttributeDescription, NeverThrowsException> stringToAttributeDescription( |
| | | final Schema schema) { |
| | | return fixedFunction(STRING_TO_ATTRIBUTE_DESCRIPTION, schema); |
| | | return new Function<String, AttributeDescription, NeverThrowsException>() { |
| | | public AttributeDescription apply(final String value) { |
| | | return AttributeDescription.valueOf(value, schema); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return A function which parses {@code Boolean} values. |
| | | */ |
| | | public static Function<String, Boolean, Void> stringToBoolean() { |
| | | public static Function<String, Boolean, NeverThrowsException> stringToBoolean() { |
| | | return STRING_TO_BOOLEAN; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code DN}s. |
| | | */ |
| | | public static Function<String, DN, Void> stringToDN() { |
| | | return fixedFunction(STRING_TO_DN, Schema.getDefaultSchema()); |
| | | public static Function<String, DN, NeverThrowsException> stringToDN() { |
| | | return stringToDN(getDefaultSchema()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * The schema to use for decoding DNs. |
| | | * @return A function which parses {@code DN}s. |
| | | */ |
| | | public static Function<String, DN, Void> stringToDN(final Schema schema) { |
| | | return fixedFunction(STRING_TO_DN, schema); |
| | | public static Function<String, DN, NeverThrowsException> stringToDN(final Schema schema) { |
| | | return new Function<String, DN, NeverThrowsException>() { |
| | | public DN apply(final String value) { |
| | | return DN.valueOf(value, schema); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return A function which parses generalized time strings. |
| | | */ |
| | | public static Function<String, GeneralizedTime, Void> stringToGeneralizedTime() { |
| | | public static Function<String, GeneralizedTime, NeverThrowsException> stringToGeneralizedTime() { |
| | | return STRING_TO_GENERALIZED_TIME; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code Integer} string values. |
| | | */ |
| | | public static Function<String, Integer, Void> stringToInteger() { |
| | | public static Function<String, Integer, NeverThrowsException> stringToInteger() { |
| | | return STRING_TO_INTEGER; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code Long} string values. |
| | | */ |
| | | public static Function<String, Long, Void> stringToLong() { |
| | | public static Function<String, Long, NeverThrowsException> stringToLong() { |
| | | return STRING_TO_LONG; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code AttributeDescription}s. |
| | | */ |
| | | public static Function<ByteString, AttributeDescription, Void> byteStringToAttributeDescription() { |
| | | return fixedFunction(BYTESTRING_TO_ATTRIBUTE_DESCRIPTION, Schema.getDefaultSchema()); |
| | | public static Function<ByteString, AttributeDescription, NeverThrowsException> byteStringToAttributeDescription() { |
| | | return byteStringToAttributeDescription(getDefaultSchema()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * The schema to use for decoding attribute descriptions. |
| | | * @return A function which parses {@code AttributeDescription}s. |
| | | */ |
| | | public static Function<ByteString, AttributeDescription, Void> byteStringToAttributeDescription( |
| | | public static Function<ByteString, AttributeDescription, NeverThrowsException> byteStringToAttributeDescription( |
| | | final Schema schema) { |
| | | return fixedFunction(BYTESTRING_TO_ATTRIBUTE_DESCRIPTION, schema); |
| | | return compose(byteStringToString(), new Function<String, AttributeDescription, NeverThrowsException>() { |
| | | public AttributeDescription apply(final String value) { |
| | | return AttributeDescription.valueOf(value, schema); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return A function which parses {@code Boolean} values. |
| | | */ |
| | | public static Function<ByteString, Boolean, Void> byteStringToBoolean() { |
| | | public static Function<ByteString, Boolean, NeverThrowsException> byteStringToBoolean() { |
| | | return BYTESTRING_TO_BOOLEAN; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code DN}s. |
| | | */ |
| | | public static Function<ByteString, DN, Void> byteStringToDN() { |
| | | return fixedFunction(BYTESTRING_TO_DN, Schema.getDefaultSchema()); |
| | | public static Function<ByteString, DN, NeverThrowsException> byteStringToDN() { |
| | | return byteStringToDN(getDefaultSchema()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * The schema to use for decoding DNs. |
| | | * @return A function which parses {@code DN}s. |
| | | */ |
| | | public static Function<ByteString, DN, Void> byteStringToDN(final Schema schema) { |
| | | return fixedFunction(BYTESTRING_TO_DN, schema); |
| | | public static Function<ByteString, DN, NeverThrowsException> byteStringToDN(final Schema schema) { |
| | | return compose(byteStringToString(), new Function<String, DN, NeverThrowsException>() { |
| | | public DN apply(final String value) { |
| | | return DN.valueOf(value, schema); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @return A function which parses generalized time strings. |
| | | */ |
| | | public static Function<ByteString, GeneralizedTime, Void> byteStringToGeneralizedTime() { |
| | | public static Function<ByteString, GeneralizedTime, NeverThrowsException> byteStringToGeneralizedTime() { |
| | | return BYTESTRING_TO_GENERALIZED_TIME; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code Integer} string values. |
| | | */ |
| | | public static Function<ByteString, Integer, Void> byteStringToInteger() { |
| | | public static Function<ByteString, Integer, NeverThrowsException> byteStringToInteger() { |
| | | return BYTESTRING_TO_INTEGER; |
| | | } |
| | | |
| | |
| | | * |
| | | * @return A function which parses {@code Long} string values. |
| | | */ |
| | | public static Function<ByteString, Long, Void> byteStringToLong() { |
| | | public static Function<ByteString, Long, NeverThrowsException> byteStringToLong() { |
| | | return BYTESTRING_TO_LONG; |
| | | } |
| | | |
| | |
| | | * @return A function which parses the string representation of a |
| | | * {@code ByteString} as a UTF-8 encoded {@code String}. |
| | | */ |
| | | public static Function<ByteString, String, Void> byteStringToString() { |
| | | public static Function<ByteString, String, NeverThrowsException> byteStringToString() { |
| | | return BYTESTRING_TO_STRING; |
| | | } |
| | | |
| | |
| | | import org.forgerock.opendj.ldap.schema.CoreSchema; |
| | | import org.forgerock.util.Reject; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import com.forgerock.opendj.util.Collections2; |
| | | |
| | |
| | | |
| | | private <N> Collection<N> getMultiValuedAttribute( |
| | | final AttributeDescription attributeDescription, |
| | | final org.forgerock.opendj.ldap.Function<ByteString, N, Void> function) { |
| | | final Function<ByteString, N, NeverThrowsException> function) { |
| | | // The returned collection is unmodifiable because we may need to |
| | | // return an empty collection if the attribute does not exist in the |
| | | // underlying entry. If a value is then added to the returned empty |
| | |
| | | } |
| | | |
| | | private <N> N getSingleValuedAttribute(final AttributeDescription attributeDescription, |
| | | final org.forgerock.opendj.ldap.Function<ByteString, N, Void> function) { |
| | | final Function<ByteString, N, NeverThrowsException> function) { |
| | | final Attribute attr = entry.getAttribute(attributeDescription); |
| | | if (attr == null || attr.isEmpty()) { |
| | | return null; |
| | | } else { |
| | | return function.apply(attr.firstValue(), null); |
| | | return function.apply(attr.firstValue()); |
| | | } |
| | | } |
| | | |
| | |
| | | * |
| | | * |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012-2013 ForgeRock AS. |
| | | * Portions copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap.requests; |
| | | |
| | | import java.util.Collections; |
| | |
| | | |
| | | import org.forgerock.opendj.ldap.DecodeException; |
| | | import org.forgerock.opendj.ldap.DecodeOptions; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.Functions; |
| | | import org.forgerock.opendj.ldap.controls.Control; |
| | | import org.forgerock.opendj.ldap.controls.ControlDecoder; |
| | | import org.forgerock.opendj.ldap.controls.GenericControl; |
| | | import org.forgerock.util.Reject; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import com.forgerock.opendj.util.Collections2; |
| | | |
| | |
| | | public final List<Control> getControls() { |
| | | // We need to make all controls unmodifiable as well, which implies |
| | | // making defensive copies where necessary. |
| | | final Function<Control, Control, Void> function = new Function<Control, Control, Void>() { |
| | | |
| | | @Override |
| | | public Control apply(final Control value, final Void p) { |
| | | // Return defensive copy. |
| | | return GenericControl.newControl(value); |
| | | } |
| | | |
| | | }; |
| | | final Function<Control, Control, NeverThrowsException> function = |
| | | new Function<Control, Control, NeverThrowsException>() { |
| | | @Override |
| | | public Control apply(final Control value) { |
| | | // Return defensive copy. |
| | | return GenericControl.newControl(value); |
| | | } |
| | | }; |
| | | |
| | | final List<Control> unmodifiableControls = |
| | | Collections2.transformedList(impl.getControls(), function, Functions |
| | |
| | | * |
| | | * |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012 ForgeRock AS. |
| | | * Portions copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap.requests; |
| | | |
| | | import java.util.Collection; |
| | |
| | | import org.forgerock.opendj.ldap.Attributes; |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldif.ChangeRecordVisitor; |
| | | |
| | | import org.forgerock.opendj.ldif.ChangeRecordVisitor; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | import com.forgerock.opendj.util.Iterables; |
| | | |
| | | /** |
| | |
| | | */ |
| | | final class UnmodifiableAddRequestImpl extends AbstractUnmodifiableRequest<AddRequest> implements |
| | | AddRequest { |
| | | private static final Function<Attribute, Attribute, Void> UNMODIFIABLE_ATTRIBUTE_FUNCTION = |
| | | new Function<Attribute, Attribute, Void>() { |
| | | |
| | | private static final Function<Attribute, Attribute, NeverThrowsException> UNMODIFIABLE_ATTRIBUTE_FUNCTION = |
| | | new Function<Attribute, Attribute, NeverThrowsException>() { |
| | | @Override |
| | | public Attribute apply(final Attribute value, final Void p) { |
| | | public Attribute apply(final Attribute value) { |
| | | return Attributes.unmodifiableAttribute(value); |
| | | } |
| | | |
| | | }; |
| | | |
| | | UnmodifiableAddRequestImpl(final AddRequest impl) { |
| | |
| | | * |
| | | * |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012 ForgeRock AS. |
| | | * Portions copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap.requests; |
| | | |
| | | import java.util.Collections; |
| | |
| | | import org.forgerock.opendj.ldap.Attribute; |
| | | import org.forgerock.opendj.ldap.Attributes; |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.Functions; |
| | | import org.forgerock.opendj.ldap.Modification; |
| | | import org.forgerock.opendj.ldap.ModificationType; |
| | | import org.forgerock.opendj.ldif.ChangeRecordVisitor; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import com.forgerock.opendj.util.Collections2; |
| | | |
| | |
| | | @Override |
| | | public List<Modification> getModifications() { |
| | | // We need to make all attributes unmodifiable as well. |
| | | final Function<Modification, Modification, Void> function = |
| | | new Function<Modification, Modification, Void>() { |
| | | |
| | | final Function<Modification, Modification, NeverThrowsException> function = |
| | | new Function<Modification, Modification, NeverThrowsException>() { |
| | | @Override |
| | | public Modification apply(final Modification value, final Void p) { |
| | | public Modification apply(final Modification value) { |
| | | final ModificationType type = value.getModificationType(); |
| | | final Attribute attribute = |
| | | Attributes.unmodifiableAttribute(value.getAttribute()); |
| | | final Attribute attribute = Attributes.unmodifiableAttribute(value.getAttribute()); |
| | | return new Modification(type, attribute); |
| | | } |
| | | |
| | | }; |
| | | |
| | | final List<Modification> unmodifiableModifications = |
| | |
| | | * |
| | | * |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012-2013 ForgeRock AS. |
| | | * Portions copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap.responses; |
| | | |
| | | import java.util.Collections; |
| | |
| | | |
| | | import org.forgerock.opendj.ldap.DecodeException; |
| | | import org.forgerock.opendj.ldap.DecodeOptions; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.Functions; |
| | | import org.forgerock.opendj.ldap.controls.Control; |
| | | import org.forgerock.opendj.ldap.controls.ControlDecoder; |
| | | import org.forgerock.opendj.ldap.controls.GenericControl; |
| | | import org.forgerock.util.Reject; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import com.forgerock.opendj.util.Collections2; |
| | | |
| | |
| | | * We need to make all controls unmodifiable as well, which implies |
| | | * making defensive copies where necessary. |
| | | */ |
| | | final Function<Control, Control, Void> function = new Function<Control, Control, Void>() { |
| | | |
| | | @Override |
| | | public Control apply(final Control value, final Void p) { |
| | | // Return defensive copy. |
| | | return GenericControl.newControl(value); |
| | | } |
| | | |
| | | }; |
| | | final Function<Control, Control, NeverThrowsException> function = |
| | | new Function<Control, Control, NeverThrowsException>() { |
| | | @Override |
| | | public Control apply(final Control value) { |
| | | // Return defensive copy. |
| | | return GenericControl.newControl(value); |
| | | } |
| | | }; |
| | | |
| | | final List<Control> unmodifiableControls = |
| | | Collections2.transformedList(impl.getControls(), function, Functions |
| | |
| | | * |
| | | * |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012 ForgeRock AS. |
| | | * Portions copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldap.responses; |
| | |
| | | import org.forgerock.opendj.ldap.Attributes; |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import com.forgerock.opendj.util.Iterables; |
| | | |
| | |
| | | */ |
| | | class UnmodifiableSearchResultEntryImpl extends AbstractUnmodifiableResponseImpl<SearchResultEntry> |
| | | implements SearchResultEntry { |
| | | private static final Function<Attribute, Attribute, Void> UNMODIFIABLE_ATTRIBUTE_FUNCTION = |
| | | new Function<Attribute, Attribute, Void>() { |
| | | |
| | | private static final Function<Attribute, Attribute, NeverThrowsException> UNMODIFIABLE_ATTRIBUTE_FUNCTION = |
| | | new Function<Attribute, Attribute, NeverThrowsException>() { |
| | | @Override |
| | | public Attribute apply(final Attribute value, final Void p) { |
| | | public Attribute apply(final Attribute value) { |
| | | return Attributes.unmodifiableAttribute(value); |
| | | } |
| | | |
| | | }; |
| | | |
| | | UnmodifiableSearchResultEntryImpl(final SearchResultEntry impl) { |
| | |
| | | * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2013 ForgeRock AS. |
| | | * Copyright 2013-2014 ForgeRock AS. |
| | | */ |
| | | package org.forgerock.opendj.rest2ldap; |
| | | |
| | |
| | | import org.forgerock.opendj.ldap.Attribute; |
| | | import org.forgerock.opendj.ldap.AttributeDescription; |
| | | import org.forgerock.opendj.ldap.Entry; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.LinkedAttribute; |
| | | import org.forgerock.opendj.ldap.Modification; |
| | | import org.forgerock.opendj.ldap.ModificationType; |
| | | |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | /** |
| | | * An abstract LDAP attribute mapper which provides a simple mapping from a JSON |
| | | * value to a single LDAP attribute. |
| | |
| | | } |
| | | } else { |
| | | getNewLDAPAttributes(c, path, newValues, transform( |
| | | new Function<Attribute, List<Modification>, Void>() { |
| | | new Function<Attribute, List<Modification>, NeverThrowsException>() { |
| | | @Override |
| | | public List<Modification> apply(final Attribute value, final Void p) { |
| | | public List<Modification> apply(final Attribute value) { |
| | | return singletonList(new Modification(modType, value)); |
| | | } |
| | | }, h)); |
| | |
| | | import org.forgerock.opendj.ldap.Entry; |
| | | import org.forgerock.opendj.ldap.LdapException; |
| | | import org.forgerock.opendj.ldap.Filter; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.Modification; |
| | | import org.forgerock.opendj.ldap.SearchResultHandler; |
| | | import org.forgerock.opendj.ldap.SearchScope; |
| | |
| | | import org.forgerock.opendj.ldap.responses.SearchResultReference; |
| | | import org.forgerock.opendj.ldif.ChangeRecord; |
| | | import org.forgerock.util.promise.FailureHandler; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | import org.forgerock.util.promise.Promise; |
| | | import org.forgerock.util.promise.PromiseImpl; |
| | | import org.forgerock.util.promise.Promises; |
| | |
| | | final String actualResourceId = nameStrategy.getResourceId(c, entry); |
| | | final String revision = getRevisionFromEntry(entry); |
| | | attributeMapper.read(c, new JsonPointer(), entry, transform( |
| | | new Function<JsonValue, Resource, Void>() { |
| | | new Function<JsonValue, Resource, NeverThrowsException>() { |
| | | @Override |
| | | public Resource apply(final JsonValue value, final Void p) { |
| | | public Resource apply(final JsonValue value) { |
| | | return new Resource(actualResourceId, revision, new JsonValue(value)); |
| | | } |
| | | }, handler)); |
| | |
| | | @Override |
| | | public Void visitNotFilter(final ResultHandler<Filter> p, |
| | | final QueryFilter subFilter) { |
| | | subFilter.accept(this, transform(new Function<Filter, Filter, Void>() { |
| | | subFilter.accept(this, transform(new Function<Filter, Filter, NeverThrowsException>() { |
| | | @Override |
| | | public Filter apply(final Filter value, final Void p) { |
| | | public Filter apply(final Filter value) { |
| | | if (value == null || value == alwaysFalse()) { |
| | | return alwaysTrue(); |
| | | } else if (value == alwaysTrue()) { |
| | |
| | | * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2012-2013 ForgeRock AS. |
| | | * Copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | package org.forgerock.opendj.rest2ldap; |
| | | |
| | | import static org.forgerock.json.resource.PatchOperation.operation; |
| | | import static org.forgerock.opendj.ldap.Filter.alwaysFalse; |
| | | import static org.forgerock.opendj.rest2ldap.Rest2LDAP.asResourceException; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.accumulate; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.i18n; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.toLowerCase; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.transform; |
| | | |
| | | import java.util.AbstractMap.SimpleImmutableEntry; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | |
| | | import org.forgerock.opendj.ldap.Attribute; |
| | | import org.forgerock.opendj.ldap.Entry; |
| | | import org.forgerock.opendj.ldap.Filter; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.Modification; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import static org.forgerock.json.resource.PatchOperation.*; |
| | | import static org.forgerock.opendj.ldap.Filter.*; |
| | | import static org.forgerock.opendj.rest2ldap.Rest2LDAP.*; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.*; |
| | | |
| | | /** |
| | | * An attribute mapper which maps JSON objects to LDAP attributes. |
| | |
| | | */ |
| | | final ResultHandler<Map.Entry<String, JsonValue>> handler = |
| | | accumulate(mappings.size(), transform( |
| | | new Function<List<Map.Entry<String, JsonValue>>, JsonValue, Void>() { |
| | | new Function<List<Map.Entry<String, JsonValue>>, JsonValue, NeverThrowsException>() { |
| | | @Override |
| | | public JsonValue apply(final List<Map.Entry<String, JsonValue>> value, |
| | | final Void p) { |
| | | public JsonValue apply(final List<Map.Entry<String, JsonValue>> value) { |
| | | if (value.isEmpty()) { |
| | | /* |
| | | * No subordinate attributes, so omit the |
| | |
| | | |
| | | for (final Mapping mapping : mappings.values()) { |
| | | mapping.mapper.read(c, path.child(mapping.name), e, transform( |
| | | new Function<JsonValue, Map.Entry<String, JsonValue>, Void>() { |
| | | new Function<JsonValue, Map.Entry<String, JsonValue>, NeverThrowsException>() { |
| | | @Override |
| | | public Map.Entry<String, JsonValue> apply(final JsonValue value, |
| | | final Void p) { |
| | | public Map.Entry<String, JsonValue> apply(final JsonValue value) { |
| | | return value != null ? new SimpleImmutableEntry<String, JsonValue>( |
| | | mapping.name, value) : null; |
| | | } |
| | |
| | | } |
| | | |
| | | private <T> ResultHandler<List<T>> accumulator(final int size, final ResultHandler<List<T>> h) { |
| | | return accumulate(size, transform(new Function<List<List<T>>, List<T>, Void>() { |
| | | return accumulate(size, transform(new Function<List<List<T>>, List<T>, NeverThrowsException>() { |
| | | @Override |
| | | public List<T> apply(final List<List<T>> value, final Void p) { |
| | | public List<T> apply(final List<List<T>> value) { |
| | | switch (value.size()) { |
| | | case 0: |
| | | return Collections.emptyList(); |
| | |
| | | import org.forgerock.opendj.ldap.EntryNotFoundException; |
| | | import org.forgerock.opendj.ldap.LdapException; |
| | | import org.forgerock.opendj.ldap.Filter; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.LinkedAttribute; |
| | | import org.forgerock.opendj.ldap.MultipleEntriesFoundException; |
| | | import org.forgerock.opendj.ldap.ResultCode; |
| | |
| | | import org.forgerock.opendj.ldap.responses.Result; |
| | | import org.forgerock.opendj.ldap.responses.SearchResultEntry; |
| | | import org.forgerock.opendj.ldap.responses.SearchResultReference; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | import org.forgerock.util.promise.FailureHandler; |
| | | import org.forgerock.util.promise.SuccessHandler; |
| | | |
| | |
| | | try { |
| | | final Set<DN> dns = attribute.parse().usingSchema(c.getConfig().schema()).asSetOfDN(); |
| | | final ResultHandler<JsonValue> handler = |
| | | accumulate(dns.size(), transform(new Function<List<JsonValue>, JsonValue, Void>() { |
| | | accumulate(dns.size(), transform(new Function<List<JsonValue>, JsonValue, NeverThrowsException>() { |
| | | @Override |
| | | public JsonValue apply(final List<JsonValue> value, final Void p) { |
| | | public JsonValue apply(final List<JsonValue> value) { |
| | | if (value.isEmpty()) { |
| | | /* |
| | | * No values, so omit the entire JSON object |
| | |
| | | * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2012-2013 ForgeRock AS. |
| | | * Copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | package org.forgerock.opendj.rest2ldap; |
| | | |
| | | import static java.util.Collections.emptyList; |
| | | import static java.util.Collections.singletonList; |
| | | import static org.forgerock.opendj.ldap.Filter.alwaysFalse; |
| | | import static org.forgerock.opendj.ldap.Functions.fixedFunction; |
| | | import static org.forgerock.opendj.rest2ldap.Rest2LDAP.asResourceException; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.base64ToByteString; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.byteStringToBase64; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.byteStringToJson; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.i18n; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.jsonToAttribute; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.jsonToByteString; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.toFilter; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.Entry; |
| | | import org.forgerock.opendj.ldap.Filter; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | import static java.util.Collections.*; |
| | | |
| | | import static org.forgerock.opendj.ldap.Filter.*; |
| | | import static org.forgerock.opendj.rest2ldap.Rest2LDAP.*; |
| | | import static org.forgerock.opendj.rest2ldap.Utils.*; |
| | | |
| | | /** |
| | | * An attribute mapper which provides a simple mapping from a JSON value to a |
| | | * single LDAP attribute. |
| | | */ |
| | | public final class SimpleAttributeMapper extends AbstractLDAPAttributeMapper<SimpleAttributeMapper> { |
| | | private Function<ByteString, ?, Void> decoder = null; |
| | | private Function<Object, ByteString, Void> encoder = null; |
| | | private Function<ByteString, ?, NeverThrowsException> decoder; |
| | | private Function<Object, ByteString, NeverThrowsException> encoder; |
| | | |
| | | SimpleAttributeMapper(final AttributeDescription ldapAttributeName) { |
| | | super(ldapAttributeName); |
| | |
| | | * The function to use for decoding LDAP attribute values. |
| | | * @return This attribute mapper. |
| | | */ |
| | | public SimpleAttributeMapper decoder(final Function<ByteString, ?, Void> f) { |
| | | public SimpleAttributeMapper decoder(final Function<ByteString, ?, NeverThrowsException> f) { |
| | | this.decoder = f; |
| | | return this; |
| | | } |
| | |
| | | * The function to use for encoding LDAP attribute values. |
| | | * @return This attribute mapper. |
| | | */ |
| | | public SimpleAttributeMapper encoder(final Function<Object, ByteString, Void> f) { |
| | | public SimpleAttributeMapper encoder(final Function<Object, ByteString, NeverThrowsException> f) { |
| | | this.encoder = f; |
| | | return this; |
| | | } |
| | |
| | | if (subPath.isEmpty()) { |
| | | try { |
| | | final ByteString va = |
| | | valueAssertion != null ? encoder().apply(valueAssertion, null) : null; |
| | | valueAssertion != null ? encoder().apply(valueAssertion) : null; |
| | | h.handleResult(toFilter(c, type, ldapAttributeName.toString(), va)); |
| | | } catch (final Exception e) { |
| | | // Invalid assertion value - bad request. |
| | |
| | | } |
| | | } |
| | | |
| | | private Function<ByteString, ? extends Object, Void> decoder() { |
| | | return decoder == null ? fixedFunction(byteStringToJson(), ldapAttributeName) : decoder; |
| | | private Function<ByteString, ? extends Object, NeverThrowsException> decoder() { |
| | | return decoder == null ? byteStringToJson(ldapAttributeName) : decoder; |
| | | } |
| | | |
| | | private Function<Object, ByteString, Void> encoder() { |
| | | return encoder == null ? fixedFunction(jsonToByteString(), ldapAttributeName) : encoder; |
| | | private Function<Object, ByteString, NeverThrowsException> encoder() { |
| | | return encoder == null ? jsonToByteString(ldapAttributeName) : encoder; |
| | | } |
| | | |
| | | } |
| | |
| | | * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2012-2013 ForgeRock AS. |
| | | * Copyright 2012-2014 ForgeRock AS. |
| | | */ |
| | | package org.forgerock.opendj.rest2ldap; |
| | | |
| | |
| | | import static org.forgerock.opendj.ldap.Functions.byteStringToGeneralizedTime; |
| | | import static org.forgerock.opendj.ldap.Functions.byteStringToLong; |
| | | import static org.forgerock.opendj.ldap.Functions.byteStringToString; |
| | | import static org.forgerock.opendj.ldap.Functions.fixedFunction; |
| | | import static org.forgerock.opendj.ldap.schema.CoreSchema.getBooleanSyntax; |
| | | import static org.forgerock.opendj.ldap.schema.CoreSchema.getGeneralizedTimeSyntax; |
| | | import static org.forgerock.opendj.ldap.schema.CoreSchema.getIntegerSyntax; |
| | |
| | | import org.forgerock.opendj.ldap.AttributeDescription; |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.Filter; |
| | | import org.forgerock.opendj.ldap.Function; |
| | | import org.forgerock.opendj.ldap.GeneralizedTime; |
| | | import org.forgerock.opendj.ldap.LinkedAttribute; |
| | | import org.forgerock.opendj.ldap.schema.Syntax; |
| | | import org.forgerock.util.promise.Function; |
| | | import org.forgerock.util.promise.NeverThrowsException; |
| | | |
| | | /** |
| | | * Internal utility methods. |
| | |
| | | } |
| | | } |
| | | |
| | | private static final Function<Object, ByteString, Void> BASE64_TO_BYTESTRING = |
| | | new Function<Object, ByteString, Void>() { |
| | | private static final Function<Object, ByteString, NeverThrowsException> BASE64_TO_BYTESTRING = |
| | | new Function<Object, ByteString, NeverThrowsException>() { |
| | | @Override |
| | | public ByteString apply(final Object value, final Void p) { |
| | | public ByteString apply(final Object value) { |
| | | return ByteString.valueOfBase64(String.valueOf(value)); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<ByteString, String, Void> BYTESTRING_TO_BASE64 = |
| | | new Function<ByteString, String, Void>() { |
| | | private static final Function<ByteString, String, NeverThrowsException> BYTESTRING_TO_BASE64 = |
| | | new Function<ByteString, String, NeverThrowsException>() { |
| | | @Override |
| | | public String apply(final ByteString value, final Void p) { |
| | | public String apply(final ByteString value) { |
| | | return value.toBase64String(); |
| | | } |
| | | }; |
| | | |
| | | private static final Function<ByteString, Object, AttributeDescription> BYTESTRING_TO_JSON = |
| | | new Function<ByteString, Object, AttributeDescription>() { |
| | | @Override |
| | | public Object apply(final ByteString value, final AttributeDescription ad) { |
| | | final Syntax syntax = ad.getAttributeType().getSyntax(); |
| | | if (syntax.equals(getBooleanSyntax())) { |
| | | return byteStringToBoolean().apply(value, null); |
| | | } else if (syntax.equals(getIntegerSyntax())) { |
| | | return byteStringToLong().apply(value, null); |
| | | } else if (syntax.equals(getGeneralizedTimeSyntax())) { |
| | | return printDateTime(byteStringToGeneralizedTime().apply(value, null) |
| | | .toCalendar()); |
| | | } else { |
| | | return byteStringToString().apply(value, null); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | private static final Function<Object, ByteString, AttributeDescription> JSON_TO_BYTESTRING = |
| | | new Function<Object, ByteString, AttributeDescription>() { |
| | | @Override |
| | | public ByteString apply(final Object value, final AttributeDescription ad) { |
| | | if (isJSONPrimitive(value)) { |
| | | final Syntax syntax = ad.getAttributeType().getSyntax(); |
| | | if (syntax.equals(getGeneralizedTimeSyntax())) { |
| | | return ByteString.valueOf(GeneralizedTime.valueOf(parseDateTime(value |
| | | .toString()))); |
| | | } else { |
| | | return ByteString.valueOf(value); |
| | | } |
| | | } else { |
| | | throw new IllegalArgumentException("Unrecognized type of JSON value: " |
| | | + value.getClass().getName()); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | /** |
| | | * Returns a result handler which can be used to collect the results of |
| | | * {@code size} asynchronous operations. Once all results have been received |
| | |
| | | } |
| | | |
| | | static Object attributeToJson(final Attribute a) { |
| | | final Function<ByteString, Object, Void> f = |
| | | fixedFunction(BYTESTRING_TO_JSON, a.getAttributeDescription()); |
| | | final boolean isSingleValued = |
| | | a.getAttributeDescription().getAttributeType().isSingleValue(); |
| | | final Function<ByteString, Object, NeverThrowsException> f = byteStringToJson(a.getAttributeDescription()); |
| | | final boolean isSingleValued = a.getAttributeDescription().getAttributeType().isSingleValue(); |
| | | return isSingleValued ? a.parse().as(f) : asList(a.parse().asSetOf(f)); |
| | | } |
| | | |
| | | static Function<Object, ByteString, Void> base64ToByteString() { |
| | | static Function<Object, ByteString, NeverThrowsException> base64ToByteString() { |
| | | return BASE64_TO_BYTESTRING; |
| | | } |
| | | |
| | | static Function<ByteString, String, Void> byteStringToBase64() { |
| | | static Function<ByteString, String, NeverThrowsException> byteStringToBase64() { |
| | | return BYTESTRING_TO_BASE64; |
| | | } |
| | | |
| | | static Function<ByteString, Object, AttributeDescription> byteStringToJson() { |
| | | return BYTESTRING_TO_JSON; |
| | | static Function<ByteString, Object, NeverThrowsException> byteStringToJson(final AttributeDescription ad) { |
| | | return new Function<ByteString, Object, NeverThrowsException>() { |
| | | @Override |
| | | public Object apply(final ByteString value) { |
| | | final Syntax syntax = ad.getAttributeType().getSyntax(); |
| | | if (syntax.equals(getBooleanSyntax())) { |
| | | return byteStringToBoolean().apply(value); |
| | | } else if (syntax.equals(getIntegerSyntax())) { |
| | | return byteStringToLong().apply(value); |
| | | } else if (syntax.equals(getGeneralizedTimeSyntax())) { |
| | | return printDateTime(byteStringToGeneralizedTime().apply(value) |
| | | .toCalendar()); |
| | | } else { |
| | | return byteStringToString().apply(value); |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | static <T> T ensureNotNull(final T object) { |
| | |
| | | } |
| | | |
| | | static Attribute jsonToAttribute(final Object value, final AttributeDescription ad) { |
| | | return jsonToAttribute(value, ad, fixedFunction(jsonToByteString(), ad)); |
| | | return jsonToAttribute(value, ad, jsonToByteString(ad)); |
| | | } |
| | | |
| | | static Attribute jsonToAttribute(final Object value, final AttributeDescription ad, |
| | | final Function<Object, ByteString, Void> f) { |
| | | final Function<Object, ByteString, NeverThrowsException> f) { |
| | | if (isJSONPrimitive(value)) { |
| | | return new LinkedAttribute(ad, f.apply(value, null)); |
| | | return new LinkedAttribute(ad, f.apply(value)); |
| | | } else if (value instanceof Collection<?>) { |
| | | final Attribute a = new LinkedAttribute(ad); |
| | | for (final Object o : (Collection<?>) value) { |
| | | a.add(f.apply(o, null)); |
| | | a.add(f.apply(o)); |
| | | } |
| | | return a; |
| | | } else { |
| | |
| | | } |
| | | } |
| | | |
| | | static Function<Object, ByteString, AttributeDescription> jsonToByteString() { |
| | | return JSON_TO_BYTESTRING; |
| | | static Function<Object, ByteString, NeverThrowsException> jsonToByteString(final AttributeDescription ad) { |
| | | return new Function<Object, ByteString, NeverThrowsException>() { |
| | | @Override |
| | | public ByteString apply(final Object value) { |
| | | if (isJSONPrimitive(value)) { |
| | | final Syntax syntax = ad.getAttributeType().getSyntax(); |
| | | if (syntax.equals(getGeneralizedTimeSyntax())) { |
| | | return ByteString.valueOf(GeneralizedTime.valueOf(parseDateTime(value.toString()))); |
| | | } else { |
| | | return ByteString.valueOf(value); |
| | | } |
| | | } else { |
| | | throw new IllegalArgumentException("Unrecognized type of JSON value: " |
| | | + value.getClass().getName()); |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | static Filter toFilter(final boolean value) { |
| | |
| | | * A result handler which accepts results of type {@code N}. |
| | | * @return A result handler which accepts results of type {@code M}. |
| | | */ |
| | | static <M, N> ResultHandler<M> transform(final Function<M, N, Void> f, |
| | | static <M, N> ResultHandler<M> transform(final Function<M, N, NeverThrowsException> f, |
| | | final ResultHandler<N> handler) { |
| | | return new ResultHandler<M>() { |
| | | @Override |
| | |
| | | @Override |
| | | public void handleResult(final M result) { |
| | | try { |
| | | handler.handleResult(f.apply(result, null)); |
| | | handler.handleResult(f.apply(result)); |
| | | } catch (final Throwable t) { |
| | | handler.handleError(asResourceException(t)); |
| | | } |