commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1063346 - /commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/ColognePhonetic.java
Date Tue, 25 Jan 2011 16:52:08 GMT
Author: ggregory
Date: Tue Jan 25 16:52:08 2011
New Revision: 1063346

URL: http://svn.apache.org/viewvc?rev=1063346&view=rev
Log:
Refactor a lot common code in the left and right buffer classes into a new common superclass.

Modified:
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/ColognePhonetic.java

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/ColognePhonetic.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/ColognePhonetic.java?rev=1063346&r1=1063345&r2=1063346&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/ColognePhonetic.java
(original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/ColognePhonetic.java
Tue Jan 25 16:52:08 2011
@@ -180,22 +180,18 @@ import org.apache.commons.codec.StringEn
  */
 public class ColognePhonetic implements StringEncoder {
 
-    private class CologneLeftBuffer implements CharSequence {
+    private abstract class CologneBuffer implements CharSequence {
 
-        private final char[] data;
-        private int length = 0;
+        protected final char[] data;
+        protected int length = 0;
 
-        public CologneLeftBuffer(int buffSize) {
-            data = new char[buffSize];
-        }
-
-        public CologneLeftBuffer(char[] data) {
+        public CologneBuffer(char[] data) {
             this.data = data;
             this.length = data.length;
         }
 
-        public int length() {
-            return length;
+        public CologneBuffer(int buffSize) {
+            data = new char[buffSize];
         }
 
         public char charAt(int index) {
@@ -206,33 +202,18 @@ public class ColognePhonetic implements 
             }
         }
 
-        public CharSequence subSequence(int start, int end) {
-            final int length = end - start;
-
-            char[] retData = copyData(start, length);
-
-            return new CologneLeftBuffer(retData);
-        }
-
-        private char[] copyData(int start, final int length) {
-            char[] retData = new char[length];
-
-            System.arraycopy(data, start, retData, 0, length);
-            return retData;
-        }
+        protected abstract char[] copyData(int start, final int length);
 
-        public char getLast() {
-            return data[length - 1];
-        }
+        protected abstract CologneBuffer createCologneBuffer(char[] data);
 
-        public void putRight(char chr) {
-            data[length] = chr;
-            length++;
+        public int length() {
+            return length;
         }
 
-        public char dropLast() {
-            length--;
-            return data[length];
+        public CharSequence subSequence(int start, int end) {
+            final int length = end - start;
+            char[] newData = copyData(start, length);
+            return createCologneBuffer(newData);
         }
 
         public String toString() {
@@ -240,67 +221,62 @@ public class ColognePhonetic implements 
         }
     }
 
-    private class CologneRightBuffer implements CharSequence {
+    private class CologneLeftBuffer extends CologneBuffer {
 
-        private int length = 0;
-        private final char[] data;
+        public CologneLeftBuffer(char[] data) {
+            super(data);
+        }
 
-        public CologneRightBuffer(int buffSize) {
-            data = new char[buffSize];
+        public CologneLeftBuffer(int buffSize) {
+            super(buffSize);
         }
 
-        public CologneRightBuffer(char[] data) {
-            this.data = data;
-            this.length = data.length;
+        protected char[] copyData(int start, final int length) {
+            char[] retData = new char[length];
+            System.arraycopy(data, start, retData, 0, length);
+            return retData;
         }
 
-        public int length() {
-            return length;
+        protected CologneBuffer createCologneBuffer(char[] data) {
+            return new CologneLeftBuffer(data);
         }
 
-        public char charAt(int index) {
-            if (index < length) {
-                return data[data.length - length + index];
-            } else {
-                throw new IndexOutOfBoundsException();
-            }
+        public void putRight(char chr) {
+            data[length] = chr;
+            length++;
         }
+    }
 
-        public CharSequence subSequence(int start, int end) {
-            final int length = end - start;
-            char[] newData = copyData(start, length);
+    private class CologneRightBuffer extends CologneBuffer {
 
-            return new CologneRightBuffer(newData);
+        public CologneRightBuffer(char[] data) {
+            super(data);
         }
 
-        private char[] copyData(int start, final int length) {
+        protected char[] copyData(int start, final int length) {
             char[] newData = new char[length];
-
-            System.arraycopy(data, data.length - this.length + start, newData,
-                    0, length);
+            System.arraycopy(data, data.length - this.length + start, newData, 0, length);
             return newData;
         }
 
-        public void putLeft(char chr) {
-            length++;
-            data[data.length - length] = chr;
-        }
-
-        public char getNext() {
-            return data[data.length - length];
+        protected CologneBuffer createCologneBuffer(char[] data) {
+            return new CologneRightBuffer(data);
         }
 
         public char dropNext() {
-            char ret = data[data.length - length];
+            char ch = data[data.length - length];
             length--;
-
-            return ret;
+            return ch;
         }
 
-        public String toString() {
-            return new String(copyData(0, length));
+        public char getNext() {
+            return data[data.length - length];
+        }
+        
+        public void putLeft(char ch) {
+            length++;
+            data[data.length - length] = ch;
         }
-
     }
 
     private static final char[][] PRE_REPLACEMENTS = new char[][] {
@@ -340,7 +316,6 @@ public class ColognePhonetic implements 
      * @return the corresponding encoding according to the <i>Kölner
      *         Phonetik</i> algorithm
      */
-
     public String colognePhonetic(String text) {
         if (text == null) {
             return null;
@@ -430,7 +405,6 @@ public class ColognePhonetic implements 
             lastChar = chr;
             lastCode = code;
         }
-
         return left.toString();
     }
 
@@ -443,7 +417,6 @@ public class ColognePhonetic implements 
                 return true;
             }
         }
-
         return false;
     }
 



Mime
View raw message