Skip to content

Commit

Permalink
set generates random name for unnamed sets
Browse files Browse the repository at this point in the history
  • Loading branch information
verhas committed Jun 25, 2019
1 parent 6605a22 commit 53a4b0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
43 changes: 28 additions & 15 deletions javageci-api/src/main/java/javax0/geci/api/Source.java
Expand Up @@ -19,7 +19,7 @@
* //</editor-fold>
* }
* </pre>
*
* <p>
* contain a segment (excluding the surrounding lines that signal the
* start and the end of the segment. The {@code Source} object can be
* asked to provide a {@link Segment} object that represents those
Expand All @@ -41,11 +41,11 @@ public interface Source {
*
* @param root the directory to the root module where the top level
* {@code pom.xml} containing the
* <pre>
* {@code <modules>
* <module>...</module>
* </modules>}
* </pre>
* <pre>
* {@code <modules>
* <module>...</module>
* </modules>}
* </pre>
* declaration is.
* @return a new Maven source directory configuration object.
*/
Expand Down Expand Up @@ -227,22 +227,31 @@ static Maven maven() {
Logger getLogger();

/**
* Set serves as an identifier class for a source set. This is used when a source is asked to open a new
* source that does not exist yet in another source set. When a source set is not identified
* calling {@link Geci#source(String...)} it will have a {@code new Set("")} object as identifier. When a
* generator needs to name a set it has to be identified and a specific first argument has to be passed to the
* {@link Geci#source(Set, String...)} usually naming the source set typically as {@code set("java")} or
* {@code set("resources")}.
* Set serves as an identifier class for a source set. This is used
* when a source is asked to open a new source that does not exist
* yet in another source set. When a source set is not identified
* calling {@link Geci#source(String...)} it will have a {@code new
* Set("xxx")} object as identifier. The identifier in this case is
* a decimal number and it is randomly created. When a generator
* needs to name a set it has to be identified and a specific first
* argument has to be passed to the {@link Geci#source(Set,
* String...)} usually naming the source set typically as {@code
* set("java")} or {@code set("resources")}.
*/
class Set {
private final String name;

private Set(String name) {
if (name == null)
throw new IllegalArgumentException("Name can not be null");
if (name == null) {
name = randomUniqueName();
}
this.name = name;
}

private String randomUniqueName() {
return "" + super.hashCode();
}

/**
* Import this method as a static import into the generators and into the test code that invokes the code
* generator.
Expand All @@ -254,6 +263,10 @@ public static Set set(String name) {
return new Set(name);
}

public static Set set() {
return set(null);
}

@Override
public String toString() {
return name;
Expand Down Expand Up @@ -398,7 +411,7 @@ public NamedSourceSet testSource() {
* @return the array of directories where the test resource files could be found.
*/
public NamedSourceSet testResources() {
return source(Geci.TEST_RESOURCES,"test", "resources");
return source(Geci.TEST_RESOURCES, "test", "resources");
}

/**
Expand Down
5 changes: 4 additions & 1 deletion javageci-engine/src/main/java/javax0/geci/engine/Geci.java
Expand Up @@ -37,7 +37,7 @@ public class Geci implements javax0.geci.api.Geci {

@Override
public Geci source(String... directory) {
directories.put(set(""), directory);
directories.put(set(), directory);
return this;
}

Expand Down Expand Up @@ -121,6 +121,9 @@ public String failed() {

@Override
public Geci source(Source.Set set, String... directory) {
if (directories.containsKey(set)) {
throw new GeciException("The set '" + set + "' is defined more than once.");
}
directories.put(set, directory);
return this;
}
Expand Down
Expand Up @@ -14,7 +14,7 @@ public class TestFileCollector {
@Test
@DisplayName("Collect the file in this test directory and find the class for it.")
void collectAllFiles() {
final var sources = Map.of(set(""),new String[]{"src/test/java/javax0/geci/util"});
final var sources = Map.of(set(),new String[]{"src/test/java/javax0/geci/util"});
var collector = new FileCollector(sources);
collector.collect(null);
assertEquals(1, collector.sources.size());
Expand Down

0 comments on commit 53a4b0a

Please sign in to comment.