    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.codec.language;
    019
    020import org.apache.commons.codec.EncoderException;
    021import org.apache.commons.codec.StringEncoder;
    022
    023/**
    024 * Encodes a string into a Caverphone 2.0 value. Delegate to a {@link Caverphone2} instance.
    025 *
    026 * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
    027 * algorithm:
    028 *
    029 * @see <a href="https://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
    030 * @see <a href="https://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
    031 * @since 1.4
    032 * @deprecated 1.5 Replaced by {@link Caverphone2}, will be removed in 2.0.
    033 */
    034@Deprecated
    035public class Caverphone implements StringEncoder {
    036
    037    /**
    038     * Delegate to a {@link Caverphone2} instance to avoid code duplication.
    039     */
    040    final private Caverphone2 encoder = new Caverphone2();
    041
    042    /**
    043     * Encodes the given String into a Caverphone value.
    044     *
    045     * @param source
    046     *            String the source string
    047     * @return A caverphone code for the given String
    048     */
    049    public String caverphone(final String source) {
    050        return this.encoder.encode(source);
    051    }
    052
    053    /**
    054     * Encodes an Object using the caverphone algorithm. This method is provided in order to satisfy the requirements of
    055     * the Encoder interface, and will throw an EncoderException if the supplied object is not of type {@link String}.
    056     *
    057     * @param obj
    058     *            Object to encode
    059     * @return An object (or type {@link String}) containing the caverphone code which corresponds to the String
    060     *         supplied.
    061     * @throws EncoderException
    062     *             if the parameter supplied is not of type {@link String}.
    063     */
    064    @Override
    065    public Object encode(final Object obj) throws EncoderException {
    066        if (!(obj instanceof String)) {
    067            throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
    068        }
    069        return this.caverphone((String) obj);
    070    }
    071
    072    /**
    073     * Encodes a String using the Caverphone algorithm.
    074     *
    075     * @param str
    076     *            String object to encode
    077     * @return The caverphone code corresponding to the String supplied
    078     */
    079    @Override
    080    public String encode(final String str) {
    081        return this.caverphone(str);
    082    }
    083
    084    /**
    085     * Tests if the caverphones of two strings are identical.
    086     *
    087     * @param str1
    088     *            First of two strings to compare
    089     * @param str2
    090     *            Second of two strings to compare
    091     * @return {@code true} if the caverphones of these strings are identical, {@code false} otherwise.
    092     */
    093    public boolean isCaverphoneEqual(final String str1, final String str2) {
    094        return this.caverphone(str1).equals(this.caverphone(str2));
    095    }
    096
    097}