* Add Levenshtein distance to Firstname class Comparable.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 9 Jan 2019 11:10:09 +0000 (12:10 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 9 Jan 2019 11:10:09 +0000 (12:10 +0100)
* Fix PersonView setter and contructor.
* Add some javadoc.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Eye.java
src/EyeView.java
src/Firstname.java
src/LevenshteinDistance.java [new file with mode: 0644]
src/Person.java
src/PersonView.java

index 7d5d80401879470faa13c37560b705f4d1fedd63..c3b836d0046eccc52afc27217ef72b379df51ec9 100644 (file)
@@ -6,14 +6,26 @@ public class Eye implements Comparable<Eye> {
     private Color color;
     private List<String> colorList = Arrays.asList("black", "green", "blue", "brown");
 
+    /**
+     *
+     * @param color
+     */
     Eye(String color) {
         setColor(color);
     }
 
+    /**
+     *
+     * @return
+     */
     public Color getColor() {
         return color;
     }
 
+    /**
+     *
+     * @param color
+     */
     public void setColor(String color) {
         if (validateColor(color)) {
             if (color.equals("brown"))
@@ -26,10 +38,20 @@ public class Eye implements Comparable<Eye> {
         }
     }
 
+    /**
+     *
+     * @param color
+     * @return
+     */
     private boolean validateColor(String color) {
         return colorList.contains(color);
     }
 
+    /**
+     *
+     * @param eye
+     * @return
+     */
     @Override
     public int compareTo(Eye eye) {
         double r_diff = this.getColor().getRed() - eye.getColor().getRed();
index 06de4693fc24ab2af6c9b4ed3310f7e77df44435..38664936de7d52df01382c8e7fab7ff980a3a969 100644 (file)
@@ -1,14 +1,26 @@
 public class EyeView {
     private Eye eyeObj;
 
+    /**
+     *
+     * @param eyeObj
+     */
     EyeView(Eye eyeObj) {
         setEyeObj(eyeObj);
     }
 
+    /**
+     *
+     * @return
+     */
     public Eye getEyeObj() {
         return eyeObj;
     }
 
+    /**
+     *
+     * @param eyeObj
+     */
     public void setEyeObj(Eye eyeObj) {
         this.eyeObj = eyeObj;
     }
index a813cd3555a4ea0d6bdc3e2da2ed8ee94ff9e660..27362c2d6f203f53c207641f98cce619dd1d0b6d 100644 (file)
@@ -11,7 +11,6 @@ public class Firstname implements Comparable<Firstname> {
 
     @Override
     public int compareTo(Firstname firstname) {
-        //return StringUtils.getLevenshteinDistance(this.getFirstname(), firstname.getFirstname());
-        return 0;
+        return LevenshteinDistance.computeLevenshteinDistance(this.getFirstname(), firstname.getFirstname());
     }
-}
+}
\ No newline at end of file
diff --git a/src/LevenshteinDistance.java b/src/LevenshteinDistance.java
new file mode 100644 (file)
index 0000000..6a56650
--- /dev/null
@@ -0,0 +1,37 @@
+
+public class LevenshteinDistance {
+    /**
+     *
+     * @param a
+     * @param b
+     * @param c
+     * @return
+     */
+    private static int minimum(int a, int b, int c) {
+        return Math.min(Math.min(a, b), c);
+    }
+
+    /**
+     *
+     * @param lhs
+     * @param rhs
+     * @return
+     */
+    public static int computeLevenshteinDistance(CharSequence lhs, CharSequence rhs) {
+        int[][] distance = new int[lhs.length() + 1][rhs.length() + 1];
+
+        for (int i = 0; i <= lhs.length(); i++)
+            distance[i][0] = i;
+        for (int j = 1; j <= rhs.length(); j++)
+            distance[0][j] = j;
+
+        for (int i = 1; i <= lhs.length(); i++)
+            for (int j = 1; j <= rhs.length(); j++)
+                distance[i][j] = minimum(
+                        distance[i - 1][j] + 1,
+                        distance[i][j - 1] + 1,
+                        distance[i - 1][j - 1] + ((lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1));
+
+        return distance[lhs.length()][rhs.length()];
+    }
+}
index 9c30e56c9e4e5d3055f7e1bea97466520a86597a..d1ceb350724cbae919c3642f77b51fa8548278f2 100644 (file)
@@ -63,7 +63,7 @@ public class Person implements Comparable<Person>, Comparator<Person> {
     }
 
     @Override
-    public int compare(Person person, Person t1) {
-        return 0;
+    public int compare(Person p1, Person p2) {
+        return p1.compareTo(p2);
     }
 }
index 9743d391456788478ab08cfe35f0d7afd34aeaf3..ba0388950231ec42c2002aa73d0ff0f3a161d094 100644 (file)
@@ -7,42 +7,90 @@ public class PersonView extends JPanel {
     private WeightView weightView;
     private EyeView eyeView;
 
+    PersonView(FirstnameView firstnameView, OriginView originView, SizeView sizeView, WeightView weightView, EyeView eyeView) {
+        setFirstnameView(firstnameView);
+        setOriginView(originView);
+        setSizeView(sizeView);
+        setWeightView(weightView);
+        setEyeView(eyeView);
+    }
+
+    /**
+     *
+     * @return
+     */
     public FirstnameView getFirstnameView() {
         return firstnameView;
     }
 
+    /**
+     *
+     * @param firstnameView
+     */
     public void setFirstnameView(FirstnameView firstnameView) {
         this.firstnameView = firstnameView;
     }
 
+    /**
+     *
+     * @return
+     */
     public OriginView getOriginView() {
         return originView;
     }
 
+    /**
+     *
+     * @param originView
+     */
     public void setOriginView(OriginView originView) {
         this.originView = originView;
     }
 
+    /**
+     *
+     * @return
+     */
     public SizeView getSizeView() {
         return sizeView;
     }
 
+    /**
+     *
+     * @param sizeView
+     */
     public void setSizeView(SizeView sizeView) {
         this.sizeView = sizeView;
     }
 
+    /**
+     *
+     * @return
+     */
     public WeightView getWeightView() {
         return weightView;
     }
 
-    public void setWeightView(EyeView eyeView) {
-        this.eyeView = eyeView;
+    /**
+     *
+     * @param weightView
+     */
+    public void setWeightView(WeightView weightView) {
+        this.weightView = weightView;
     }
 
+    /**
+     *
+     * @param eyeView
+     */
     public void setEyeView(EyeView eyeView) {
         this.eyeView = eyeView;
     }
 
+    /**
+     *
+     * @return
+     */
     public EyeView getEyeView() {
         return eyeView;
     }