Skip to content

Commit

Permalink
#26 StFast and TrFast
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 3, 2022
1 parent de8d193 commit 48d7b2d
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 4 deletions.
99 changes: 99 additions & 0 deletions src/main/java/com/yegor256/xsline/StFast.java
@@ -0,0 +1,99 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.yegor256.xsline;

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;

/**
* A shift that logs an error if transformation took too long.
*
* @since 0.12.0
*/
public final class StFast implements Shift {

/**
* The original shift.
*/
private final Shift origin;

/**
* Threshold in milliseconds.
*/
private final long threshold;

/**
* Logging target.
*/
private final Object target;

/**
* Ctor.
* @param shift The shift
*/
public StFast(final Shift shift) {
this(shift, StFast.class);
}

/**
* Ctor.
* @param shift The shift
* @param tgt The target to log against
*/
public StFast(final Shift shift, final Object tgt) {
this(shift, tgt, 100L);
}

/**
* Ctor.
* @param shift The shift
* @param tgt The target to log against
* @param msec Threshold in msec
*/
public StFast(final Shift shift, final Object tgt, final long msec) {
this.origin = shift;
this.threshold = msec;
this.target = tgt;
}

@Override
public String uid() {
return this.origin.uid();
}

@Override
public XML apply(final int position, final XML xml) {
final long start = System.currentTimeMillis();
final XML out = this.origin.apply(position, xml);
final long msec = System.currentTimeMillis() - start;
if (msec > this.threshold) {
Logger.error(
this.target,
"Transformation %s took too long %[ms]s (over %[ms]s)",
msec, this.threshold
);
}
return out;
}
}
69 changes: 69 additions & 0 deletions src/main/java/com/yegor256/xsline/TrFast.java
@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.yegor256.xsline;

/**
* Train that wraps all shifts in {@link StFast}.
*
* @since 0.12.0
*/
public final class TrFast extends TrEnvelope {

/**
* Ctor.
* @param train Original
*/
public TrFast(final Train<Shift> train) {
this(train, TrFast.class);
}

/**
* Ctor.
* @param train Original
* @param target The target
*/
public TrFast(final Train<Shift> train, final Object target) {
super(
new TrLambda(
train,
x -> new StFast(x, target)
)
);
}

/**
* Ctor.
* @param train Original
* @param target The target
* @param msec Threshold in msec
*/
public TrFast(final Train<Shift> train, final Object target, final long msec) {
super(
new TrLambda(
train,
x -> new StFast(x, target, msec)
)
);
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/yegor256/xsline/TrBulkTest.java
Expand Up @@ -38,7 +38,7 @@ final class TrBulkTest {
@Test
void simpleScenario() {
MatcherAssert.assertThat(
new TrBulk<>(new TrClasspath<>(new TrDefault<>()))
new TrBulk<>(new TrClasspath<>(new TrFast(new TrDefault<>())))
.with(Arrays.asList("add-brackets.xsl", "void.xsl"))
.back()
.back()
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/yegor256/xsline/TrDefaultTest.java
Expand Up @@ -37,9 +37,11 @@ final class TrDefaultTest {
@Test
void startWithMany() {
MatcherAssert.assertThat(
new TrDefault<>(
new StClasspath("add-id.xsl"),
new StClasspath("add-brackets.xsl")
new TrFast(
new TrDefault<>(
new StClasspath("add-id.xsl"),
new StClasspath("add-brackets.xsl")
)
),
Matchers.iterableWithSize(2)
);
Expand Down

0 comments on commit 48d7b2d

Please sign in to comment.