-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shortname for preachers (wip), Statistic one year
- Loading branch information
1 parent
821739c
commit d34dbed
Showing
9 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
server/src/main/java/de/pietro/lusso/territory/utils/PreacherUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package de.pietro.lusso.territory.utils; | ||
|
||
import de.pietro.lusso.territory.domain.Congregation; | ||
import de.pietro.lusso.territory.domain.Preacher; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class PreacherUtils { | ||
|
||
private PreacherUtils(){} | ||
|
||
public static void setShortNames(final List<Preacher> preacherList) { | ||
|
||
Set<String> names = new HashSet<>(); | ||
int i = 0; | ||
preacherList.sort((o1, o2) -> o1.getName().compareTo(o2.getName())); | ||
|
||
for (Preacher preacher : preacherList) { | ||
if (StringUtils.isNotEmpty(preacher.getShortName())) { | ||
names.add(preacher.getShortName()); | ||
} | ||
} | ||
|
||
String previousShortName = null; | ||
|
||
for (Preacher preacher : preacherList) { | ||
|
||
String shortName = getShortName(preacher); | ||
|
||
if (!shortName.equals(previousShortName)) { | ||
i = 0; | ||
previousShortName = shortName; | ||
} | ||
|
||
if (StringUtils.isNotEmpty(preacher.getShortName())) { | ||
i = 0; | ||
continue; | ||
} | ||
|
||
if (names.contains(shortName)) { | ||
i++; | ||
names.add(shortName + i); | ||
preacher.setShortName(shortName + i); | ||
} else { | ||
i = 0; | ||
names.add(shortName); | ||
preacher.setShortName(shortName); | ||
} | ||
} | ||
} | ||
|
||
private static String getShortName(Preacher preacher) { | ||
|
||
String [] parts = preacher.getName().split(" "); | ||
StringBuilder shortName = new StringBuilder(); | ||
|
||
for (String part : parts) { | ||
try { | ||
int maxLength = 2; | ||
if (part.length() < maxLength) continue; | ||
shortName.append(part.substring(0,maxLength)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
return shortName.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
server/src/test/java/de/pietro/lusso/territory/utils/PreacherUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package de.pietro.lusso.territory.utils; | ||
|
||
import de.pietro.lusso.territory.domain.Preacher; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PreacherUtilsTest { | ||
|
||
@Test | ||
public void testSimilarNames() { | ||
List<Preacher> list = new ArrayList<>(); | ||
Preacher p0 = new Preacher(); | ||
p0.setName("Jim Maria"); | ||
Preacher p1 = new Preacher(); | ||
p1.setName("John Dude"); | ||
Preacher p2 = new Preacher(); | ||
p2.setName("John Duo"); | ||
Preacher p3 = new Preacher(); | ||
p3.setName("John Dual"); | ||
Preacher p4 = new Preacher(); | ||
p4.setName("John Maria"); | ||
Preacher p5 = new Preacher(); | ||
p5.setName("John Maria"); | ||
p5.setShortName("JoMa"); | ||
list.add(p0); | ||
list.add(p1); | ||
list.add(p2); | ||
list.add(p3); | ||
list.add(p4); | ||
list.add(p5); | ||
|
||
PreacherUtils.setShortNames(list); | ||
|
||
for (Preacher preacher : list) { | ||
System.out.println(preacher.getShortName()); | ||
} | ||
|
||
Assert.assertEquals("JiMa", list.get(0).getShortName()); | ||
Assert.assertEquals("JoDu", list.get(1).getShortName()); | ||
Assert.assertEquals("JoDu1", list.get(2).getShortName()); | ||
Assert.assertEquals("JoDu2", list.get(3).getShortName()); | ||
Assert.assertEquals("JoMa1", list.get(4).getShortName()); | ||
Assert.assertEquals("JoMa", list.get(5).getShortName()); | ||
} | ||
} |