Skip to content

Commit

Permalink
skip if name
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 30, 2024
1 parent e81235f commit 15d413b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/com/rultor/agents/Agents.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public SuperAgent closer() throws IOException {
* @param profile Profile
* @return The agent
* @throws IOException If fails
* @checkstyle MethodLengthCheck (500 lines)
*/
@SuppressWarnings("PMD.ExcessiveMethodLength")
public Agent agent(final Talk talk, final Profile profile)
Expand Down Expand Up @@ -327,7 +328,11 @@ public Agent agent(final Talk talk, final Profile profile)
new Agent.Quiet(new MkdirDaemon()),
new TimedAgent(new StartsDaemon(profile)),
// @checkstyle MagicNumber (1 line)
new Agent.Quiet(new KillsDaemon(TimeUnit.HOURS.toMinutes(3L))),
new Agent.SkipIfName(
new Agent.Quiet(new KillsDaemon(TimeUnit.HOURS.toMinutes(1L))),
"^(objectionary|yegor256|zerocracy)/.*$"
),
new Agent.Quiet(new KillsDaemon(TimeUnit.HOURS.toMinutes(5L))),
new TimedAgent(new StopsDaemon()),
new TimedAgent(new Agent.Quiet(new EndsDaemon())),
new EndsRequest(),
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/rultor/spi/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.jcabi.log.Logger;
import java.io.IOException;
import java.util.Arrays;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import lombok.ToString;

Expand Down Expand Up @@ -174,4 +175,42 @@ public void execute(final Talk talk) throws IOException {
}
}
}

/**
* Only if the name of the talk DOESN'T match the regular expression.
*
* @since 1.0
*/
@Immutable
@ToString
@EqualsAndHashCode(of = {"agent", "pattern"})
final class SkipIfName implements Agent {
/**
* Agent to defend.
*/
private final transient Agent agent;

/**
* The regular expression.
*/
private final transient Pattern pattern;

/**
* Ctor.
* @param agt Agent
* @param ptn Pattern to match
*/
public SkipIfName(final Agent agt, final String ptn) {
this.agent = agt;
this.pattern = Pattern.compile(ptn);
}

@Override
public void execute(final Talk talk) throws IOException {
final String name = talk.read().xpath("/talk/@name").get(0);
if (!this.pattern.matcher(name).matches()) {
this.agent.execute(talk);
}
}
}
}

0 comments on commit 15d413b

Please sign in to comment.