AbstractCaverphone.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.codec.language;

  18. import org.apache.commons.codec.EncoderException;
  19. import org.apache.commons.codec.StringEncoder;

  20. /**
  21.  * Encodes a string into a Caverphone value.
  22.  *
  23.  * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
  24.  * algorithm:
  25.  *
  26.  * <p>This class is immutable and thread-safe.</p>
  27.  *
  28.  * @see <a href="https://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
  29.  * @since 1.5
  30.  */
  31. public abstract class AbstractCaverphone implements StringEncoder {

  32.     /**
  33.      * Constructs a new instance for subclasses.
  34.      */
  35.     public AbstractCaverphone() {
  36.         // empty
  37.     }

  38.     /**
  39.      * Encodes an Object using the Caverphone algorithm. This method is provided in order to satisfy the requirements of
  40.      * the Encoder interface, and will throw an EncoderException if the supplied object is not of type {@link String}.
  41.      *
  42.      * @param source
  43.      *            Object to encode
  44.      * @return An object (or type {@link String}) containing the Caverphone code which corresponds to the String
  45.      *         supplied.
  46.      * @throws EncoderException
  47.      *             if the parameter supplied is not of type {@link String}.
  48.      */
  49.     @Override
  50.     public Object encode(final Object source) throws EncoderException {
  51.         if (!(source instanceof String)) {
  52.             throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
  53.         }
  54.         return this.encode((String) source);
  55.     }

  56.     /**
  57.      * Tests if the encodings of two strings are equal.
  58.      *
  59.      * This method might be promoted to a new AbstractStringEncoder superclass.
  60.      *
  61.      * @param str1
  62.      *            First of two strings to compare
  63.      * @param str2
  64.      *            Second of two strings to compare
  65.      * @return {@code true} if the encodings of these strings are identical, {@code false} otherwise.
  66.      * @throws EncoderException
  67.      *             thrown if there is an error condition during the encoding process.
  68.      */
  69.     public boolean isEncodeEqual(final String str1, final String str2) throws EncoderException {
  70.         return this.encode(str1).equals(this.encode(str2));
  71.     }

  72. }