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    
018    package org.apache.commons.codec.language;
019    
020    import org.apache.commons.codec.EncoderException;
021    import 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     * @version $Id: Caverphone.html 889935 2013-12-11 05:05:13Z ggregory $
030     * @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
031     * @see <a href="http://caversham.otago.ac.nz/files/working/ctp150804.pdf">Caverphone 2.0 specification</a>
032     * @since 1.4
033     * @deprecated 1.5 Replaced by {@link Caverphone2}, will be removed in 2.0.
034     */
035    @Deprecated
036    public class Caverphone implements StringEncoder {
037    
038        /**
039         * Delegate to a {@link Caverphone2} instance to avoid code duplication.
040         */
041        final private Caverphone2 encoder = new Caverphone2();
042    
043        /**
044         * Creates an instance of the Caverphone encoder
045         */
046        public Caverphone() {
047            super();
048        }
049    
050        /**
051         * Encodes the given String into a Caverphone value.
052         *
053         * @param source
054         *            String the source string
055         * @return A caverphone code for the given String
056         */
057        public String caverphone(String source) {
058            return this.encoder.encode(source);
059        }
060    
061        /**
062         * Encodes an Object using the caverphone algorithm. This method is provided in order to satisfy the requirements of
063         * the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
064         *
065         * @param obj
066         *            Object to encode
067         * @return An object (or type java.lang.String) containing the caverphone code which corresponds to the String
068         *         supplied.
069         * @throws EncoderException
070         *             if the parameter supplied is not of type java.lang.String
071         */
072        @Override
073        public Object encode(Object obj) throws EncoderException {
074            if (!(obj instanceof String)) {
075                throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
076            }
077            return this.caverphone((String) obj);
078        }
079    
080        /**
081         * Encodes a String using the Caverphone algorithm.
082         *
083         * @param str
084         *            String object to encode
085         * @return The caverphone code corresponding to the String supplied
086         */
087        @Override
088        public String encode(String str) {
089            return this.caverphone(str);
090        }
091    
092        /**
093         * Tests if the caverphones of two strings are identical.
094         *
095         * @param str1
096         *            First of two strings to compare
097         * @param str2
098         *            Second of two strings to compare
099         * @return {@code true} if the caverphones of these strings are identical, {@code false} otherwise.
100         */
101        public boolean isCaverphoneEqual(String str1, String str2) {
102            return this.caverphone(str1).equals(this.caverphone(str2));
103        }
104    
105    }