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