|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2016-2025 Objectionary.com |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | +package org.eolang.sodg; |
| 6 | + |
| 7 | +import com.jcabi.log.Logger; |
| 8 | +import com.yegor256.xsline.Shift; |
| 9 | +import com.yegor256.xsline.TrLambda; |
| 10 | +import com.yegor256.xsline.Train; |
| 11 | +import java.io.File; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.logging.Level; |
| 14 | +import org.cactoos.map.MapEntry; |
| 15 | +import org.cactoos.map.MapOf; |
| 16 | + |
| 17 | +/** |
| 18 | + * The depot that produces trains. |
| 19 | + * @since 0.0.3 |
| 20 | + * @todo #9:35min Add unit tests for `Depot`. |
| 21 | + * We should test the trains inside of it, and method `train()`, that supposed |
| 22 | + * to return proper train by it's name. Don't forget to remove this puzzle. |
| 23 | + */ |
| 24 | +final class Depot { |
| 25 | + |
| 26 | + /** |
| 27 | + * The trains. |
| 28 | + */ |
| 29 | + private final Map<String, Train<Shift>> trains; |
| 30 | + |
| 31 | + /** |
| 32 | + * Ctor. |
| 33 | + * @param measures Measures |
| 34 | + */ |
| 35 | + Depot(final File measures) { |
| 36 | + this( |
| 37 | + new MapOf<>( |
| 38 | + new MapEntry<>( |
| 39 | + "sodg", Depot.measured(new TrSodg(Depot.loggingLevel()), measures) |
| 40 | + ), |
| 41 | + new MapEntry<>( |
| 42 | + "dot", Depot.measured(new TrDot(Depot.loggingLevel()), measures) |
| 43 | + ), |
| 44 | + new MapEntry<>("xembly", Depot.measured(new TrXembly(), measures)), |
| 45 | + new MapEntry<>("text", Depot.measured(new TrText(), measures)), |
| 46 | + new MapEntry<>( |
| 47 | + "finish", Depot.measured(new TrFinish(Depot.loggingLevel()), measures) |
| 48 | + ) |
| 49 | + ) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Ctor. |
| 55 | + * @param trns The trains |
| 56 | + */ |
| 57 | + Depot(final Map<String, Train<Shift>> trns) { |
| 58 | + this.trains = trns; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Train. |
| 63 | + * @param name Name |
| 64 | + * @return Train |
| 65 | + */ |
| 66 | + Train<Shift> train(final String name) { |
| 67 | + return this.trains.get(name); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * We are in IntelliJ IDEA at the moment? |
| 72 | + * <p> |
| 73 | + * This is for testing purposes, to enable higher visibility of logs inside |
| 74 | + * tests being executed interactively in the IDE. |
| 75 | + * |
| 76 | + * @return TRUE if inside IDE |
| 77 | + */ |
| 78 | + private static Level loggingLevel() { |
| 79 | + Level lvl = Level.FINEST; |
| 80 | + if (System.getProperty("java.class.path").contains("idea_rt.jar")) { |
| 81 | + lvl = Level.INFO; |
| 82 | + } |
| 83 | + return lvl; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Measured train. |
| 88 | + * @param train Train to measure |
| 89 | + * @param measures Measures |
| 90 | + * @return Measured train. |
| 91 | + */ |
| 92 | + private static Train<Shift> measured(final Train<Shift> train, final File measures) { |
| 93 | + if (measures.getParentFile().mkdirs()) { |
| 94 | + Logger.debug(Depot.class, "Directory created for %[file]s", measures); |
| 95 | + } |
| 96 | + if (!measures.getParentFile().exists()) { |
| 97 | + throw new IllegalArgumentException( |
| 98 | + String.format( |
| 99 | + "For some reason, the directory %s is absent, can't write measures to %s", |
| 100 | + measures.getParentFile(), |
| 101 | + measures |
| 102 | + ) |
| 103 | + ); |
| 104 | + } |
| 105 | + if (measures.isDirectory()) { |
| 106 | + throw new IllegalArgumentException( |
| 107 | + String.format( |
| 108 | + "This is not a file but a directory, can't write to it: %s", |
| 109 | + measures |
| 110 | + ) |
| 111 | + ); |
| 112 | + } |
| 113 | + return new TrLambda( |
| 114 | + train, |
| 115 | + shift -> new StMeasured( |
| 116 | + shift, |
| 117 | + measures.toPath() |
| 118 | + ) |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments