001/*
002 * Copyright 2022-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2022-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) 2022-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.matchingrules;
037
038
039
040import com.unboundid.asn1.ASN1OctetString;
041import com.unboundid.util.ByteStringBuffer;
042import com.unboundid.util.NotNull;
043import com.unboundid.util.StaticUtils;
044import com.unboundid.util.ThreadSafety;
045import com.unboundid.util.ThreadSafetyLevel;
046
047
048
049/**
050 * This enum defines the policy that the {@link TelephoneNumberMatchingRule}
051 * should use when comparing two values in accordance with this syntax.
052 */
053@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
054public enum TelephoneNumberComparisonPolicy
055{
056  /**
057   * A policy that indicates that all non-numeric characters should be ignored
058   * when comparing values.
059   */
060  IGNORE_ALL_NON_NUMERIC_CHARACTERS,
061
062
063
064  /**
065   * A policy that indicates that only spaces and hyphens should be ignored when
066   * comparing values.
067   */
068  IGNORE_ONLY_SPACES_AND_DASHES;
069
070
071
072  /**
073   * Normalizes the provided value in accordance with this policy.  This method
074   * does not perform any validation on the provided value.
075   *
076   * @param  value  The value to be normalized.  It must not be {@code null}.
077   *
078   * @return  The normalized representation of the provided value.
079   */
080  @NotNull()
081  public ASN1OctetString normalizeValue(@NotNull final ASN1OctetString value)
082  {
083    final String valueString = StaticUtils.toLowerCase(value.stringValue());
084    final ByteStringBuffer buffer = new ByteStringBuffer(valueString.length());
085    for (int i=0; i < valueString.length(); i++)
086    {
087      final char c = valueString.charAt(i);
088      switch (c)
089      {
090        case ' ':
091        case '-':
092          // These will always be excluded from the normalized representation.
093          break;
094
095        case '+':
096        case '0':
097        case '1':
098        case '2':
099        case '3':
100        case '4':
101        case '5':
102        case '6':
103        case '7':
104        case '8':
105        case '9':
106          // These will always be included in the normalized representation.
107          buffer.append(c);
108          break;
109
110        default:
111          // If we only ignore dashes and spaces, then all other characters will
112          // be included in the normalized representation.  Otherwise, they will
113          // be excluded.
114          if (this == IGNORE_ONLY_SPACES_AND_DASHES)
115          {
116            buffer.append(c);
117          }
118          break;
119      }
120    }
121
122    return new ASN1OctetString(buffer.toByteArray());
123  }
124}