001/*
002 * Copyright 2007-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2007-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2007-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk;
037
038
039
040import com.unboundid.asn1.ASN1StreamReader;
041import com.unboundid.asn1.ASN1StreamReaderSequence;
042import com.unboundid.util.NotMutable;
043import com.unboundid.util.NotNull;
044import com.unboundid.util.Nullable;
045import com.unboundid.util.ThreadSafety;
046import com.unboundid.util.ThreadSafetyLevel;
047
048
049
050/**
051 * This class provides a data structure for holding information about the result
052 * of processing a compare operation.  It provides generic response elements as
053 * described in the {@link LDAPResult} class, and also includes a
054 * {@link CompareResult#compareMatched} method for determining whether the
055 * compare operation matched the target entry.
056 */
057@NotMutable()
058@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
059public final class CompareResult
060       extends LDAPResult
061{
062  /**
063   * The serial version UID for this serializable class.
064   */
065  private static final long serialVersionUID = -6061844770039020617L;
066
067
068
069  /**
070   * Creates a new compare result based on the provided LDAP result.
071   *
072   * @param  ldapResult  The LDAP result object to use to create this compare
073   *                     response.
074   */
075  public CompareResult(@NotNull final LDAPResult ldapResult)
076  {
077    super(ldapResult);
078  }
079
080
081
082  /**
083   * Creates a new compare result from the provided {@code LDAPException}.
084   *
085   * @param  exception  The {@code LDAPException} to use to create this compare
086   *                    result.
087   */
088  public CompareResult(@NotNull final LDAPException exception)
089  {
090    super(exception.toLDAPResult());
091  }
092
093
094
095  /**
096   * Creates a new compare result with the provided information.
097   *
098   * @param  messageID          The message ID for the LDAP message that is
099   *                            associated with this LDAP result.
100   * @param  resultCode         The result code from the response.
101   * @param  diagnosticMessage  The diagnostic message from the response, if
102   *                            available.
103   * @param  matchedDN          The matched DN from the response, if available.
104   * @param  referralURLs       The set of referral URLs from the response, if
105   *                            available.
106   * @param  responseControls   The set of controls from the response, if
107   *                            available.
108   */
109  public CompareResult(final int messageID,
110                       @NotNull final ResultCode resultCode,
111                       @Nullable final String diagnosticMessage,
112                       @Nullable final String matchedDN,
113                       @Nullable final String[] referralURLs,
114                       @Nullable final Control[] responseControls)
115  {
116    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
117          responseControls);
118  }
119
120
121
122  /**
123   * Creates a new compare result object with the provided message ID and with
124   * the protocol op and controls read from the given ASN.1 stream reader.
125   *
126   * @param  messageID        The LDAP message ID for the LDAP message that is
127   *                          associated with this LDAP result.
128   * @param  messageSequence  The ASN.1 stream reader sequence used in the
129   *                          course of reading the LDAP message elements.
130   * @param  reader           The ASN.1 stream reader from which to read the
131   *                          protocol op and controls.
132   *
133   * @return  The decoded compare result.
134   *
135   * @throws  LDAPException  If a problem occurs while reading or decoding data
136   *                         from the ASN.1 stream reader.
137   */
138  @NotNull()
139  static CompareResult readCompareResultFrom(final int messageID,
140              @NotNull final ASN1StreamReaderSequence messageSequence,
141              @NotNull final ASN1StreamReader reader)
142         throws LDAPException
143  {
144    return new CompareResult(LDAPResult.readLDAPResultFrom(messageID,
145                    messageSequence, reader));
146  }
147
148
149
150  /**
151   * Indicates whether the compare operation matched the target entry.
152   *
153   * @return  {@code true} if the compare operation matched the target entry,
154   *          or {@code false} if not.
155   */
156  public boolean compareMatched()
157  {
158    return (getResultCode().equals(ResultCode.COMPARE_TRUE));
159  }
160}