1 // Mozilla has modified this file - see https://hg.mozilla.org/ for details.
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.mozilla.apache.commons.codec.language;
20 
21 import org.mozilla.apache.commons.codec.EncoderException;
22 import org.mozilla.apache.commons.codec.StringEncoder;
23 
24 /**
25  * Encodes a string into a Caverphone value.
26  *
27  * This is an algorithm created by the Caversham Project at the University of Otago. It implements the Caverphone 2.0
28  * algorithm:
29  *
30  * @author Apache Software Foundation
31  * @version $Id: Caverphone.java 1075947 2011-03-01 17:56:14Z ggregory $
32  * @see <a href="http://en.wikipedia.org/wiki/Caverphone">Wikipedia - Caverphone</a>
33  * @since 1.5
34  */
35 public abstract class AbstractCaverphone implements StringEncoder {
36 
37     /**
38      * Creates an instance of the Caverphone encoder
39      */
AbstractCaverphone()40     public AbstractCaverphone() {
41         super();
42     }
43 
44     /**
45      * Encodes an Object using the caverphone algorithm. This method is provided in order to satisfy the requirements of
46      * the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
47      *
48      * @param source
49      *            Object to encode
50      * @return An object (or type java.lang.String) containing the caverphone code which corresponds to the String
51      *         supplied.
52      * @throws EncoderException
53      *             if the parameter supplied is not of type java.lang.String
54      */
encode(Object source)55     public Object encode(Object source) throws EncoderException {
56         if (!(source instanceof String)) {
57             throw new EncoderException("Parameter supplied to Caverphone encode is not of type java.lang.String");
58         }
59         return this.encode((String) source);
60     }
61 
62     /**
63      * Tests if the encodings of two strings are equal.
64      *
65      * This method might be promoted to a new AbstractStringEncoder superclass.
66      *
67      * @param str1
68      *            First of two strings to compare
69      * @param str2
70      *            Second of two strings to compare
71      * @return <code>true</code> if the encodings of these strings are identical, <code>false</code> otherwise.
72      * @throws EncoderException
73      */
isEncodeEqual(String str1, String str2)74     public boolean isEncodeEqual(String str1, String str2) throws EncoderException {
75         return this.encode(str1).equals(this.encode(str2));
76     }
77 
78 }
79