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