001/*
002 * Copyright 2014-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2014-2020 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) 2015-2020 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.unboundidds.controls;
037
038
039
040import com.unboundid.util.StaticUtils;
041import com.unboundid.util.ThreadSafety;
042import com.unboundid.util.ThreadSafetyLevel;
043
044
045
046/**
047 * This enum defines the set of count types that may be used in a matching entry
048 * count response control.
049 * <BR>
050 * <BLOCKQUOTE>
051 *   <B>NOTE:</B>  This class, and other classes within the
052 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
053 *   supported for use against Ping Identity, UnboundID, and
054 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
055 *   for proprietary functionality or for external specifications that are not
056 *   considered stable or mature enough to be guaranteed to work in an
057 *   interoperable way with other types of LDAP servers.
058 * </BLOCKQUOTE>
059 */
060@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
061public enum MatchingEntryCountType
062{
063  /**
064   * The count type that indicates that the server was able to determine the
065   * exact number of entries matching the search criteria and examined them to
066   * exclude any entries that would not be returned to the client in the course
067   * of processing a normal search with the same criteria.
068   */
069  EXAMINED_COUNT((byte) 0x80),
070
071
072
073  /**
074   * The count type that indicates that the server was able to determine the
075   * exact number of entries matching the search criteria, but did not examine
076   * them to exclude any entries that might not actually be returned to the
077   * client in the course of processing a normal search with the same criteria
078   * (e.g., entries that the requester doesn't have permission to access, or
079   * entries like LDAP subentries, replication conflict entries, or soft-deleted
080   * entries that are returned only for special types of requests).
081   */
082  UNEXAMINED_COUNT((byte) 0x81),
083
084
085
086  /**
087   * The count type that indicates that the server was unable to determine the
088   * exact number of entries matching the search criteria, but was able to
089   * determine an upper bound for the number of matching entries.
090   */
091  UPPER_BOUND((byte) 0x82),
092
093
094
095  /**
096   * The count type that indicates that the server was unable to make any
097   * meaningful determination about the number of entries matching the search
098   * criteria.
099   */
100  UNKNOWN((byte) 0x83);
101
102
103
104  // The BER type that corresponds to this enum value.
105  private final byte berType;
106
107
108
109  /**
110   * Creates a new count type value with the provided information.
111   *
112   * @param  berType  The BER type that corresponds to this enum value.
113   */
114  MatchingEntryCountType(final byte berType)
115  {
116    this.berType = berType;
117  }
118
119
120
121  /**
122   * Retrieves the BER type for this count type value.
123   *
124   * @return  The BER type for this count type value.
125   */
126  public byte getBERType()
127  {
128    return berType;
129  }
130
131
132
133  /**
134   * Indicates whether this matching entry count type is considered more
135   * specific than the provided count type.  The following order of precedence,
136   * from most specific to least specific, will be used:
137   * <OL>
138   *   <LI>EXAMINED_COUNT</LI>
139   *   <LI>UNEXAMINED_COUNT</LI>
140   *   <LI>UPPER_BOUND</LI>
141   *   <LI>UNKNOWN</LI>
142   * </OL>
143   *
144   * @param  t  The matching entry count type value to compare against this
145   *            matching entry count type.  It must not be {@code null}.
146   *
147   * @return  {@code true} if the provided matching entry count type value is
148   *          considered more specific than this matching entry count type, or
149   *          {@code false} if the provided count type is the same as or less
150   *          specific than this count type.
151   */
152  public boolean isMoreSpecificThan(final MatchingEntryCountType t)
153  {
154    switch (this)
155    {
156      case EXAMINED_COUNT:
157        return (t != EXAMINED_COUNT);
158
159      case UNEXAMINED_COUNT:
160        return ((t != EXAMINED_COUNT) && (t != UNEXAMINED_COUNT));
161
162      case UPPER_BOUND:
163        return ((t != EXAMINED_COUNT) && (t != UNEXAMINED_COUNT) &&
164                (t != UPPER_BOUND));
165
166      case UNKNOWN:
167      default:
168        return false;
169    }
170  }
171
172
173
174  /**
175   * Indicates whether this matching entry count type is considered less
176   * specific than the provided count type.  The following order of precedence,
177   * from most specific to least specific, will be used:
178   * <OL>
179   *   <LI>EXAMINED_COUNT</LI>
180   *   <LI>UNEXAMINED_COUNT</LI>
181   *   <LI>UPPER_BOUND</LI>
182   *   <LI>UNKNOWN</LI>
183   * </OL>
184   *
185   * @param  t  The matching entry count type value to compare against this
186   *            matching entry count type.  It must not be {@code null}.
187   *
188   * @return  {@code true} if the provided matching entry count type value is
189   *          considered less specific than this matching entry count type, or
190   *          {@code false} if the provided count type is the same as or more
191   *          specific than this count type.
192   */
193  public boolean isLessSpecificThan(final MatchingEntryCountType t)
194  {
195    switch (this)
196    {
197      case UNKNOWN:
198        return (t != UNKNOWN);
199
200      case UPPER_BOUND:
201        return ((t != UNKNOWN) && (t != UPPER_BOUND));
202
203      case UNEXAMINED_COUNT:
204        return ((t != UNKNOWN) && (t != UPPER_BOUND) &&
205                (t != UNEXAMINED_COUNT));
206
207      case EXAMINED_COUNT:
208      default:
209        return false;
210    }
211  }
212
213
214
215  /**
216   * Retrieves the count type value for the provided BER type.
217   *
218   * @param  berType  The BER type for the count type value to retrieve.
219   *
220   * @return  The count type value that corresponds to the provided BER type, or
221   *          {@code null} if there is no corresponding count type value.
222   */
223  public static MatchingEntryCountType valueOf(final byte berType)
224  {
225    for (final MatchingEntryCountType t : values())
226    {
227      if (t.berType == berType)
228      {
229        return t;
230      }
231    }
232
233    return null;
234  }
235
236
237
238  /**
239   * Retrieves the matching entry count type with the specified name.
240   *
241   * @param  name  The name of the matching entry count type to retrieve.  It
242   *               must not be {@code null}.
243   *
244   * @return  The requested matching entry count type, or {@code null} if no
245   *          such type is defined.
246   */
247  public static MatchingEntryCountType forName(final String name)
248  {
249    switch (StaticUtils.toLowerCase(name))
250    {
251      case "examinedcount":
252      case "examined-count":
253      case "examined_count":
254        return EXAMINED_COUNT;
255      case "unexaminedcount":
256      case "unexamined-count":
257      case "unexamined_count":
258        return UNEXAMINED_COUNT;
259      case "upperbound":
260      case "upper-bound":
261      case "upper_bound":
262        return UPPER_BOUND;
263      case "unknown":
264        return UNKNOWN;
265      default:
266        return null;
267    }
268  }
269}