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 2010 Sun Microsystems, Inc.
025 *      Portions copyright 2012 ForgeRock AS.
026 */
027
028package org.forgerock.opendj.ldap.responses;
029
030import org.forgerock.opendj.ldap.ByteString;
031import org.forgerock.opendj.ldap.ResultCode;
032
033/**
034 * An abstract Extended result which can be used as the basis for implementing
035 * new Extended operations.
036 *
037 * @param <S>
038 *            The type of Extended result.
039 */
040public abstract class AbstractExtendedResult<S extends ExtendedResult> extends
041        AbstractResultImpl<S> implements ExtendedResult {
042
043    /**
044     * Creates a new extended result that is an exact copy of the provided
045     * result.
046     *
047     * @param extendedResult
048     *            The extended result to be copied.
049     * @throws NullPointerException
050     *             If {@code extendedResult} was {@code null} .
051     */
052    protected AbstractExtendedResult(final ExtendedResult extendedResult) {
053        super(extendedResult);
054    }
055
056    /**
057     * Creates a new extended result using the provided result code.
058     *
059     * @param resultCode
060     *            The result code.
061     * @throws NullPointerException
062     *             If {@code resultCode} was {@code null}.
063     */
064    protected AbstractExtendedResult(final ResultCode resultCode) {
065        super(resultCode);
066    }
067
068    @Override
069    public abstract String getOID();
070
071    @Override
072    public abstract ByteString getValue();
073
074    @Override
075    public abstract boolean hasValue();
076
077    @Override
078    public String toString() {
079        final StringBuilder builder = new StringBuilder();
080        builder.append("ExtendedResult(resultCode=");
081        builder.append(getResultCode());
082        builder.append(", matchedDN=");
083        builder.append(getMatchedDN());
084        builder.append(", diagnosticMessage=");
085        builder.append(getDiagnosticMessage());
086        builder.append(", referrals=");
087        builder.append(getReferralURIs());
088        builder.append(", responseName=");
089        builder.append(getOID() == null ? "" : getOID());
090        if (hasValue()) {
091            builder.append(", responseValue=");
092            builder.append(getValue().toHexPlusAsciiString(4));
093        }
094        builder.append(", controls=");
095        builder.append(getControls());
096        builder.append(")");
097        return builder.toString();
098    }
099
100    @Override
101    @SuppressWarnings("unchecked")
102    final S getThis() {
103        return (S) this;
104    }
105}