Skip to content

Commit

Permalink
#2328 - default ctor + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Aug 1, 2023
1 parent 45966fa commit 65296e9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
29 changes: 11 additions & 18 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ProbeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.eolang.maven.name.OnCached;
import org.eolang.maven.name.OnDefault;
import org.eolang.maven.name.OnSwap;
import org.eolang.maven.name.OnUnversioned;
import org.eolang.maven.objectionary.Objectionaries;
import org.eolang.maven.objectionary.ObjsDefault;
import org.eolang.maven.tojos.ForeignTojo;
Expand Down Expand Up @@ -106,7 +105,7 @@ public void exec() throws IOException {
)
);
}
final Collection<String> probed = new HashSet<>(1);
final Collection<ObjectName> probed = new HashSet<>(1);
final Collection<ForeignTojo> tojos = this.scopedTojos().unprobed();
for (final ForeignTojo tojo : tojos) {
final Path src = tojo.optimized();
Expand All @@ -116,22 +115,20 @@ public void exec() throws IOException {
}
int count = 0;
for (final ObjectName object : objects) {
if (!this.objectionaries.contains(object.hash(), object.value())) {
if (!this.objectionaries.contains(object)) {
continue;
}
++count;
this.scopedTojos()
.add(object.asString())
.add(object)
.withDiscoveredAt(src);
probed.add(object.asString());
probed.add(object);
}
final ObjectName def = new OnDefault(tojo.identifier(), this.hsh);
tojo.withHash(
new ChNarrow(
new OnSwap(
this.withVersions,
def,
new OnUnversioned(def)
new OnDefault(tojo.identifier(), this.hsh)
).hash()
)
).withProbed(count);
Expand Down Expand Up @@ -166,16 +163,12 @@ private Collection<ObjectName> probes(final Path file)
throws FileNotFoundException {
final Collection<ObjectName> objects = new ListOf<>(
new Mapped<>(
obj -> {
final ObjectName def = new OnDefault(ProbeMojo.noPrefix(obj), this.hsh);
return new OnCached(
new OnSwap(
this.withVersions,
def,
new OnUnversioned(def)
)
);
},
obj -> new OnCached(
new OnSwap(
this.withVersions,
new OnDefault(ProbeMojo.noPrefix(obj), this.hsh)
)
),
new Filtered<>(
obj -> !obj.isEmpty(),
new XMLDocument(file).xpath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ public interface ObjectName extends Text {

@Override
String asString();

@Override
String toString();
}
10 changes: 10 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/name/OnSwap.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
/**
* Swapped object name.
* Depends on encapsulated condition behaves like one of the encapsulated object names.
* If second object is not provided - behaves like {@link OnUnversioned}
*
* @since 0.29.6
*/
Expand All @@ -39,6 +40,15 @@ public final class OnSwap implements ObjectName {
*/
private final Unchecked<ObjectName> swapped;

/**
* Ctor.
* @param condition Condition.
* @param def Default object name.
*/
public OnSwap(final boolean condition, final ObjectName def) {
this(condition, def, new OnUnversioned(def));
}

/**
* Ctor.
* @param condition Condition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import org.cactoos.Input;
import org.eolang.maven.hash.CommitHash;
import org.eolang.maven.name.ObjectName;

/**
* Many objectionaries for different hashes.
Expand All @@ -44,12 +45,12 @@ public interface Objectionaries {

/**
* Check if object exists.
* @param hash Commit hash
*
* @param name Object name
* @return True if object exists, false otherwise
* @throws IOException If some I/O problem happens.
*/
boolean contains(CommitHash hash, String name) throws IOException;
boolean contains(ObjectName name) throws IOException;

/**
* Fake objectionaries.
Expand Down Expand Up @@ -84,8 +85,8 @@ public Input object(final CommitHash hash, final String name) throws IOException
}

@Override
public boolean contains(final CommitHash hash, final String name) throws IOException {
return this.objectionary.contains(name);
public boolean contains(final ObjectName name) throws IOException {
return this.objectionary.contains(name.value());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eolang.maven.hash.ChCached;
import org.eolang.maven.hash.ChNarrow;
import org.eolang.maven.hash.CommitHash;
import org.eolang.maven.name.ObjectName;

/**
* Default objectionaries.
Expand Down Expand Up @@ -117,8 +118,8 @@ public Input object(final CommitHash hash, final String name) throws IOException
}

@Override
public boolean contains(final CommitHash hash, final String name) throws IOException {
return this.objectionary(hash).contains(name);
public boolean contains(final ObjectName name) throws IOException {
return this.objectionary(name.hash()).contains(name.value());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.cactoos.Scalar;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;
import org.eolang.maven.name.ObjectName;

/**
* Foreign tojos.
Expand Down Expand Up @@ -114,6 +115,15 @@ public ForeignTojo add(final String name) {
return new ForeignTojo(tojo);
}

/**
* Add a foreign tojo.
* @param name The name of the tojo as {@link ObjectName}.
* @return The tojo.
*/
public ForeignTojo add(final ObjectName name) {
return this.add(name.asString());
}

/**
* Get the tojos that are not discovered yet.
* @return The tojos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class OnSwapTest {
@Test
void behavesLikeFirst() {
MatcherAssert.assertThat(
String.format(
"Swap object name should have been equal to %s, but it didn't",
OnSwapTest.FIRST
),
new OnSwap(
true,
new OnDefault(OnSwapTest.FIRST, OnSwapTest.FAKE),
Expand All @@ -64,6 +68,10 @@ void behavesLikeFirst() {
@Test
void behavesLikeSecond() {
MatcherAssert.assertThat(
String.format(
"Swap object name should have been equal to %s, but it didn't",
OnSwapTest.SECOND
),
new OnSwap(
false,
new OnDefault(OnSwapTest.FIRST, OnSwapTest.FAKE),
Expand All @@ -72,4 +80,19 @@ void behavesLikeSecond() {
Matchers.equalTo(OnSwapTest.SECOND)
);
}

void behavesLikeUnversioned() {
final String stdout = "stdout";
MatcherAssert.assertThat(
String.format(
"Default swap object name should have been equal to %s, but it didn't",
stdout
),
new OnSwap(
false,
new OnDefault(OnSwapTest.FIRST, OnSwapTest.FAKE)
).asString(),
Matchers.equalTo(stdout)
);
}
}

0 comments on commit 65296e9

Please sign in to comment.