Caverphone.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.  *      http://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 2.0 value. Delegate to a {@link Caverphone2} instance.
  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.  * @version $Id: Caverphone.java 1079535 2011-03-08 20:54:37Z ggregory $
  27.  * @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
  28.  * @see <a href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
  29.  * @since 1.4
  30.  * @deprecated 1.5 Replaced by {@link Caverphone2}, will be removed in 2.0.
  31.  */
  32. @Deprecated
  33. public class Caverphone implements StringEncoder {

  34.     /**
  35.      * Delegate to a {@link Caverphone2} instance to avoid code duplication.
  36.      */
  37.     final private Caverphone2 encoder = new Caverphone2();

  38.     /**
  39.      * Creates an instance of the Caverphone encoder
  40.      */
  41.     public Caverphone() {
  42.         super();
  43.     }

  44.     /**
  45.      * Encodes the given String into a Caverphone value.
  46.      *
  47.      * @param source
  48.      *            String the source string
  49.      * @return A caverphone code for the given String
  50.      */
  51.     public String caverphone(final String source) {
  52.         return this.encoder.encode(source);
  53.     }

  54.     /**
  55.      * Encodes an Object using the caverphone algorithm. This method is provided in order to satisfy the requirements of
  56.      * the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
  57.      *
  58.      * @param obj
  59.      *            Object to encode
  60.      * @return An object (or type java.lang.String) containing the caverphone code which corresponds to the String
  61.      *         supplied.
  62.      * @throws EncoderException
  63.      *             if the parameter supplied is not of type java.lang.String
  64.      */
  65.     @Override
  66.     public Object encode(final Object obj) throws EncoderException {
  67.         if (!(obj instanceof String)) {
  68.             throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
  69.         }
  70.         return this.caverphone((String) obj);
  71.     }

  72.     /**
  73.      * Encodes a String using the Caverphone algorithm.
  74.      *
  75.      * @param str
  76.      *            String object to encode
  77.      * @return The caverphone code corresponding to the String supplied
  78.      */
  79.     @Override
  80.     public String encode(final String str) {
  81.         return this.caverphone(str);
  82.     }

  83.     /**
  84.      * Tests if the caverphones of two strings are identical.
  85.      *
  86.      * @param str1
  87.      *            First of two strings to compare
  88.      * @param str2
  89.      *            Second of two strings to compare
  90.      * @return <code>true</code> if the caverphones of these strings are identical, <code>false</code> otherwise.
  91.      */
  92.     public boolean isCaverphoneEqual(final String str1, final String str2) {
  93.         return this.caverphone(str1).equals(this.caverphone(str2));
  94.     }

  95. }