001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2014-2015 ForgeRock AS.
025 */
026package org.forgerock.opendj.ldap.spi;
027
028import org.forgerock.opendj.ldap.LdapException;
029import org.forgerock.opendj.ldap.LdapPromise;
030import org.forgerock.opendj.ldap.LdapResultHandler;
031import org.forgerock.util.promise.Promise;
032import org.forgerock.util.promise.PromiseImpl;
033import org.forgerock.util.promise.Promises;
034
035/**
036 * This class provides an implementation of the {@link LdapPromise}.
037 *
038 * @param <S>
039 *            The type of result returned by this promise.
040 * @see Promise
041 * @see Promises
042 * @see LdapPromise
043 */
044public class LdapPromiseImpl<S> extends LdapPromiseWrapper<S, PromiseImpl<S, LdapException>> implements
045        LdapPromise<S>, LdapResultHandler<S> {
046
047    /**
048     * Creates a new {@link LdapPromiseImpl} from a wrapped existing {@link PromiseImpl}.
049     *
050     * @param wrappedPromise
051     *      The {@link Promise} to wrap.
052     * @param requestID
053     *      Identifier of the request.
054     */
055    protected LdapPromiseImpl(PromiseImpl<S, LdapException> wrappedPromise, int requestID) {
056        super(wrappedPromise, requestID);
057    }
058
059    /**
060     * Creates a new {@link LdapPromiseImpl}.
061     *
062     * @param <S>
063     *            The type of result of the promise.
064     * @return a new {@link LdapPromiseImpl}
065     */
066    public static <S> LdapPromiseImpl<S> newLdapPromiseImpl() {
067        return newLdapPromiseImpl(-1);
068    }
069
070    /**
071     * Creates a new {@link LdapPromiseImpl} with a requestID.
072     *
073     * @param <S>
074     *            The type of result of the promise.
075     * @param requestID
076     *            Identifier of the request.
077     * @return a new {@link LdapPromiseImpl}
078     */
079    public static <S> LdapPromiseImpl<S> newLdapPromiseImpl(int requestID) {
080        return new LdapPromiseImpl<>(PromiseImpl.<S, LdapException> create(), requestID);
081    }
082
083    @Override
084    public void handleException(LdapException exception) {
085        getWrappedPromise().handleException(exception);
086    }
087
088    @Override
089    public void handleResult(S result) {
090        getWrappedPromise().handleResult(result);
091    }
092}