Source code
Description: source: package: org.apache.commons.codec.language, class: Caverphone

    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 *      https://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    private final Caverphone2 encoder = new Caverphone2();
    041
    042    /**
    043     * Constructs a new instance.
    044     */
    045    public Caverphone() {
    046        // empty
    047    }
    048
    049    /**
    050     * Encodes the given String into a Caverphone value.
    051     *
    052     * @param source
    053     *            String the source string.
    054     * @return A Caverphone code for the given String.
    055     */
    056    public String caverphone(final String source) {
    057        return this.encoder.encode(source);
    058    }
    059
    060    /**
    061     * Encodes an Object using the Caverphone algorithm. This method is provided in order to satisfy the requirements of
    062     * the Encoder interface, and will throw an EncoderException if the supplied object is not of type {@link String}.
    063     *
    064     * @param obj
    065     *            Object to encode.
    066     * @return An object (or type {@link String}) containing the Caverphone code which corresponds to the String
    067     *         supplied.
    068     * @throws EncoderException
    069     *             if the parameter supplied is not of type {@link String}.
    070     */
    071    @Override
    072    public Object encode(final Object obj) throws EncoderException {
    073        if (!(obj instanceof String)) {
    074            throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
    075        }
    076        return caverphone((String) obj);
    077    }
    078
    079    /**
    080     * Encodes a String using the Caverphone algorithm.
    081     *
    082     * @param str
    083     *            String object to encode.
    084     * @return The Caverphone code corresponding to the String supplied.
    085     */
    086    @Override
    087    public String encode(final String str) {
    088        return caverphone(str);
    089    }
    090
    091    /**
    092     * Tests if the Caverphones of two strings are identical.
    093     *
    094     * @param str1
    095     *            First of two strings to compare.
    096     * @param str2
    097     *            Second of two strings to compare.
    098     * @return {@code true} if the Caverphones of these strings are identical, {@code false} otherwise.
    099     */
    100    public boolean isCaverphoneEqual(final String str1, final String str2) {
    101        return caverphone(str1).equals(caverphone(str2));
    102    }
    103
    104}