Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 71 additions & 6 deletions eo-parser/src/main/java/org/eolang/parser/TrStepped.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import com.yegor256.xsline.TrEnvelope;
import com.yegor256.xsline.TrLambda;
import com.yegor256.xsline.Train;
import java.util.concurrent.CountDownLatch;
import org.cactoos.Scalar;
import org.cactoos.io.ResourceOf;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Synced;
import org.cactoos.text.TextOf;

/**
* Trains that adds sheet names that were processed.
* Train that adds sheet names that were processed.
*
* @since 0.1
*/
Expand All @@ -47,26 +49,39 @@ final class TrStepped extends TrEnvelope {
* Apply changes to each XML after processing.
*/
private static final Scalar<XSL> STEPPED = new Sticky<>(
() -> new XSLDocument(
new TextOf(
new ResourceOf("org/eolang/parser/_stepped.xsl")
).asString()
new Once<XSL>(
() -> new XSLDocument(
new TextOf(
new ResourceOf("org/eolang/parser/_stepped.xsl")
).asString()
)
)
);

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

/**
* Ctor.
*
* @param train Original train
* @param stepped XSL to apply
*/
TrStepped(final Train<Shift> train, final Scalar<XSL> stepped) {
super(
new TrLambda(
train,
shift -> new StAfter(
shift,
new StLambda(
shift::uid,
(pos, xml) -> TrStepped.STEPPED.value()
(pos, xml) -> new Synced<>(stepped).value()
.with("step", pos)
.with("sheet", shift.uid())
.transform(xml)
Expand All @@ -75,4 +90,54 @@ final class TrStepped extends TrEnvelope {
)
);
}

/**
* Scalar that loads the value only once.
*
* @param <T> Type of the value
* @since 0.51
*/
static final class Once<T> implements Scalar<T> {

/**
* Origin scalar.
*/
private final Scalar<T> origin;

/**
* Latch to count down.
*/
private final CountDownLatch latch;

/**
* Ctor.
*
* @param origin Origin scalar
*/
Once(final Scalar<T> origin) {
this(origin, new CountDownLatch(1));
}

/**
* Ctor.
*
* @param origin Origin scalar
* @param latch Latch to count down
*/
private Once(final Scalar<T> origin, final CountDownLatch latch) {
this.origin = origin;
this.latch = latch;
}

@Override
public T value() throws Exception {
if (this.latch.getCount() < 1) {
throw new IllegalStateException(
String.format("Resource '%s' should be loaded only once", this.origin)
);
}
this.latch.countDown();
return this.origin.value();
}
}
}
92 changes: 92 additions & 0 deletions eo-parser/src/test/java/org/eolang/parser/TrSteppedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2025 Objectionary.com
*
* 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 org.eolang.parser;

import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSL;
import com.jcabi.xml.XSLDocument;
import com.yegor256.Together;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Xsline;
import org.cactoos.io.ResourceOf;
import org.cactoos.scalar.Sticky;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

/**
* Test cases for {@link TrStepped}.
*
* @since 0.51
*/
final class TrSteppedTest {

@Test
void addsSheetName() {
MatcherAssert.assertThat(
"We expect the sheet name to be added",
new Xsline(
new TrStepped(
new TrDefault<Shift>().with(
new StClasspath("/org/eolang/parser/print/wrap-data.xsl")
)
)
).pass(new XMLDocument("<program><concurrency>no</concurrency></program>")).toString(),
XhtmlMatchers.hasXPath("/program/sheets/sheet[text()='wrap-data']")
);
}

@RepeatedTest(10)
void addsSheetNameConcurrently() {
final XML doc = new XMLDocument("<program><concurrency>yes</concurrency></program>");
final Sticky<XSL> loading = new Sticky<>(
new TrStepped.Once<XSL>(
() -> new XSLDocument(
new TextOf(
() -> new ResourceOf("org/eolang/parser/_stepped.xsl").stream()
).asString()
)
)
);
MatcherAssert.assertThat(
"We expect the sheet name to be added successfully in concurrent environment",
new Together<>(
i -> new Xsline(
new TrStepped(
new TrDefault<Shift>().with(
new StClasspath("/org/eolang/parser/print/wrap-data.xsl")
),
loading
)
).pass(doc).toString()
).iterator().next(),
XhtmlMatchers.hasXPath("/program/sheets/sheet[text()='wrap-data']")
);
}
}