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

    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 value.
    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 * <p>This class is immutable and thread-safe.</p>
    030 *
    031 * @see <a href="https://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
    032 * @since 1.5
    033 */
    034public abstract class AbstractCaverphone implements StringEncoder {
    035
    036    /**
    037     * Constructs a new instance for subclasses.
    038     */
    039    public AbstractCaverphone() {
    040        // empty
    041    }
    042
    043    /**
    044     * Encodes an Object using the Caverphone algorithm. This method is provided in order to satisfy the requirements of
    045     * the Encoder interface, and will throw an EncoderException if the supplied object is not of type {@link String}.
    046     *
    047     * @param source
    048     *            Object to encode.
    049     * @return An object (or type {@link String}) containing the Caverphone code which corresponds to the String
    050     *         supplied.
    051     * @throws EncoderException
    052     *             if the parameter supplied is not of type {@link String}.
    053     */
    054    @Override
    055    public Object encode(final Object source) throws EncoderException {
    056        if (!(source instanceof String)) {
    057            throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
    058        }
    059        return this.encode((String) source);
    060    }
    061
    062    /**
    063     * Tests if the encodings of two strings are equal.
    064     *
    065     * This method might be promoted to a new AbstractStringEncoder superclass.
    066     *
    067     * @param str1
    068     *            First of two strings to compare.
    069     * @param str2
    070     *            Second of two strings to compare.
    071     * @return {@code true} if the encodings of these strings are identical, {@code false} otherwise.
    072     * @throws EncoderException
    073     *             thrown if there is an error condition during the encoding process.
    074     */
    075    public boolean isEncodeEqual(final String str1, final String str2) throws EncoderException {
    076        return this.encode(str1).equals(this.encode(str2));
    077    }
    078
    079}