From: Jérôme Benoit Date: Wed, 9 Jan 2019 11:10:09 +0000 (+0100) Subject: * Add Levenshtein distance to Firstname class Comparable. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ac6c3ea2922a7d424c1244809f68026c5a77bcdf;p=Persons_Comparator.git * Add Levenshtein distance to Firstname class Comparable. * Fix PersonView setter and contructor. * Add some javadoc. Signed-off-by: Jérôme Benoit --- diff --git a/src/Eye.java b/src/Eye.java index 7d5d804..c3b836d 100644 --- a/src/Eye.java +++ b/src/Eye.java @@ -6,14 +6,26 @@ public class Eye implements Comparable { private Color color; private List 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 { } } + /** + * + * @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(); diff --git a/src/EyeView.java b/src/EyeView.java index 06de469..3866493 100644 --- a/src/EyeView.java +++ b/src/EyeView.java @@ -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; } diff --git a/src/Firstname.java b/src/Firstname.java index a813cd3..27362c2 100644 --- a/src/Firstname.java +++ b/src/Firstname.java @@ -11,7 +11,6 @@ public class Firstname implements Comparable { @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 index 0000000..6a56650 --- /dev/null +++ b/src/LevenshteinDistance.java @@ -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()]; + } +} diff --git a/src/Person.java b/src/Person.java index 9c30e56..d1ceb35 100644 --- a/src/Person.java +++ b/src/Person.java @@ -63,7 +63,7 @@ public class Person implements Comparable, Comparator { } @Override - public int compare(Person person, Person t1) { - return 0; + public int compare(Person p1, Person p2) { + return p1.compareTo(p2); } } diff --git a/src/PersonView.java b/src/PersonView.java index 9743d39..ba03889 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -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; }