Unverified Commit 51fab3f7 authored by Mathieu Féry (Mathius)'s avatar Mathieu Féry (Mathius)
Browse files

Add match method for name generation

parent 6ad38bf0
Pipeline #28830 failed with stages
in 3 minutes and 46 seconds
...@@ -78,6 +78,32 @@ public class Name implements Serializable { ...@@ -78,6 +78,32 @@ public class Name implements Serializable {
return false; return false;
} }
/**
* For check if this Name Generator match with keywords given
* @param keywords : String represent keyword used
* @return matched
*/
public boolean matchWith(String... keywords) {
return matchWith(Arrays.asList(keywords));
}
/**
* For check if this Name Generator match with list of keywords given
* @param keywords : List of String represent keyword used
* @return matched
*/
public boolean matchWith(List<String> keywords) {
boolean[] matched = {false, false};
for (String keyword: keywords) {
String word = keyword.trim();
if (!matched[0] && specie.equalsIgnoreCase(word))
matched[0] = true;
if (type != null && !matched[1] && type.equalsIgnoreCase(word))
matched[1] = true;
}
return matched[0] && (type == null || matched[1]);
}
/** /**
* Method for obtaining a random name from this name generator * Method for obtaining a random name from this name generator
* @param ctx : Context for make request for name * @param ctx : Context for make request for name
...@@ -222,6 +248,7 @@ public class Name implements Serializable { ...@@ -222,6 +248,7 @@ public class Name implements Serializable {
public static final List<Name> allFantasyNames = joinList(Arrays.asList( public static final List<Name> allFantasyNames = joinList(Arrays.asList(
commonFantasyNames, monstrousFantasyNames, outsiderFantasyNames, elementalFantasyNames)); commonFantasyNames, monstrousFantasyNames, outsiderFantasyNames, elementalFantasyNames));
public static final List<Name> allNames = allFantasyNames; public static final List<Name> allNames = allFantasyNames;
public static final Name defaultName = commonFantasyNames.get(0);
/** /**
* For get random name from the API. * For get random name from the API.
...@@ -241,4 +268,102 @@ public class Name implements Serializable { ...@@ -241,4 +268,102 @@ public class Name implements Serializable {
public static Name getRandomName(List<Name> names) { public static Name getRandomName(List<Name> names) {
return names.get(new Random().nextInt(names.size())); return names.get(new Random().nextInt(names.size()));
} }
/**
* For get random name from keywords given (If keywords match with no generator default is used)
* @param ctx : Context for make request for name
* @param keywords : List of String represent keyword used
* @return nameGenerated
*/
@NonNull
public static String getRandomName(Context ctx, String... keywords) {
return getRandomName(ctx, Arrays.asList(keywords));
}
/**
* For get random name from keywords given (If keywords match with no generator default is used)
* @param ctx : Context for make request for name
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static String getRandomName(Context ctx, List<String> keywords) {
return getRandomName(ctx, allNames, keywords);
}
/**
* For get random name from keywords given (If keywords match with no generator default is used)
* @param ctx : Context for make request for name
* @param names : List of Names used for generation if match
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static String getRandomName(Context ctx, List<Name> names, String... keywords) {
return getRandomName(ctx, allNames, Arrays.asList(keywords));
}
/**
* For get random name from keywords given (If keywords match with no generator default is used)
* @param ctx : Context for make request for name
* @param names : List of Names used for generation if match
* @param keywords : List of String represent keyword used
* @return nameGenerated
*/
@NonNull
public static String getRandomName(Context ctx, List<Name> names, List<String> keywords) {
List<Name> baseNames = getNamesMatches(names, keywords);
Name nameUsed;
if (baseNames.size() == 0)
nameUsed = defaultName;
else
nameUsed = getRandomName(baseNames);
return nameUsed.getRandomName(ctx);
}
/**
* For get name generator from keywords given
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesMatches(String... keywords) {
return getNamesMatches(Arrays.asList(keywords));
}
/**
* For get name generator from keywords given
* @param keywords : List of String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesMatches(List<String> keywords) {
return getNamesMatches(allNames, keywords);
}
/**
* For get name generator from keywords given
* @param names : List of Names used
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesMatches(List<Name> names, String... keywords) {
return getNamesMatches(names, Arrays.asList(keywords));
}
/**
* For get name generator from keywords given
* @param names : List of Names used
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesMatches(List<Name> names, List<String> keywords) {
List<Name> out = new ArrayList<>();
for (Name name: names)
if (name.matchWith(keywords))
out.add(name);
return out;
}
} }
...@@ -7,21 +7,30 @@ import java.util.Collections; ...@@ -7,21 +7,30 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import static fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh.Name.createAllCombination; import static fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh.Name.createAllCombination;
import static fr.iutnantes.miar.dndgenerator.api.utils.ListUtils.joinList;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class NameTest { public class NameTest {
Name example = new Name("example", null);
Name secondExample = new Name("2ndExample", null);
List<Name> twoExamples = Arrays.asList(example, secondExample);
Name exampleTruc = new Name("example", "truc");
Name secondExampleTruc = new Name("2ndExample", "truc");
List<Name> twoExamplesTruc = Arrays.asList(exampleTruc, secondExampleTruc);
Name exampleBidule = new Name("example", "bidule");
Name secondExampleBidule = new Name("2ndExample", "bidule");
List<Name> fourExamples = Arrays.asList(
exampleTruc, exampleBidule, secondExampleTruc, secondExampleBidule);
List<Name> allExamples = joinList(Arrays.asList(twoExamples, twoExamplesTruc,
Arrays.asList(exampleBidule, secondExampleBidule)));
List<Name> allExampleArg = Arrays.asList(example, exampleTruc, exampleBidule);
List<Name> allSecondExampleArg = Arrays.asList(secondExample, secondExampleTruc,
secondExampleBidule);
@Test @Test
public void createAllCombinationTest() { public void createAllCombinationTest() {
Name example = new Name("example", null);
Name secondExample = new Name("2ndExample", null);
List<Name> twoExamples = Arrays.asList(example, secondExample);
Name exampleTruc = new Name("example", "truc");
Name secondExampleTruc = new Name("2ndExample", "truc");
List<Name> twoExamplesTruc = Arrays.asList(exampleTruc, secondExampleTruc);
Name exampleBidule = new Name("example", "bidule");
Name secondExampleBidule = new Name("2ndExample", "bidule");
List<Name> fourExamples = Arrays.asList(
exampleTruc, exampleBidule, secondExampleTruc, secondExampleBidule);
assertEquals( assertEquals(
Collections.singletonList(example), Collections.singletonList(example),
createAllCombination("example") createAllCombination("example")
...@@ -45,4 +54,21 @@ public class NameTest { ...@@ -45,4 +54,21 @@ public class NameTest {
Arrays.asList("truc", "bidule")) Arrays.asList("truc", "bidule"))
); );
} }
@Test
public void matchWithTest() {
assertTrue(example.matchWith("ExAmple", "Truc", "Bidule"));
assertTrue(example.matchWith(" Example", "Truc", "Bidule"));
assertFalse(example.matchWith("Truc", "Bidule"));
assertTrue(exampleTruc.matchWith("ExAmple", "Truc", "Bidule"));
assertTrue(exampleTruc.matchWith(" Example", "truc ", "Bidule"));
assertFalse(exampleTruc.matchWith("Truc", "Bidule"));
assertFalse(exampleTruc.matchWith("Example", "Bidule"));
}
@Test
public void getNamesMatchesTest() {
assertEquals(allExampleArg,
Name.getNamesMatches(allExamples, "example", "truc", "bidule"));
assertEquals(allSecondExampleArg,
Name.getNamesMatches(allExamples, "2nDexample", "truc", "bidule"));
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment