diff --git a/P/Sd1/Timeperiod/.gitignore b/P/Sd1/Timeperiod/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..2275479e375a621edeb852701cba9ea53b67ee7e --- /dev/null +++ b/P/Sd1/Timeperiod/.gitignore @@ -0,0 +1,56 @@ +/target/ +/.settings/ +.classpath +.project +dependency-reduced-pom.xml +*.log + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/P/Sd1/Timeperiod/pom.xml b/P/Sd1/Timeperiod/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..82d2709c6932cf400b039b6d2a0731d1dc107e16 --- /dev/null +++ b/P/Sd1/Timeperiod/pom.xml @@ -0,0 +1,82 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>de.hdm_stuttgart.mi.sd1</groupId> + <artifactId>timeperiod</artifactId> + <version>0.9</version> + <packaging>jar</packaging> + + <name>timeperiod</name> + + <url>https://freedocs.mi.hdm-stuttgart.de/sd1SectToolsOfTheTrade2.html</url> + + <description>Basic Java project providing Junit 4 testing and log4j2 logging.</description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.11.0</version> + </dependency> + + </dependencies> + + <build> + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.7.0</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>3.0.0</version> + <configuration/> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>3.1.0</version> + <configuration> + <transformers> + <transformer + implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <manifestEntries> + <Main-Class>de.hdm_stuttgart.mi.sd1.App</Main-Class> + </manifestEntries> + </transformer> + </transformers> + </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> + </plugin> + + </plugins> + </build> +</project> \ No newline at end of file diff --git a/P/Sd1/Timeperiod/src/main/java/de/hdm_stuttgart/mi/sd1/App.java b/P/Sd1/Timeperiod/src/main/java/de/hdm_stuttgart/mi/sd1/App.java new file mode 100644 index 0000000000000000000000000000000000000000..1138b122c97fca1bffc0d9384c277b5365fce423 --- /dev/null +++ b/P/Sd1/Timeperiod/src/main/java/de/hdm_stuttgart/mi/sd1/App.java @@ -0,0 +1,31 @@ +package de.hdm_stuttgart.mi.sd1; + +/** + * A demo using {@link Timeperiod} instances. + */ + +public class App { + /** + * Main entry point. + * + * @param args Yet unused + */ + public static void main(String[] args) { + test1(); + test2(); + } + + + static private void test1() { + final Timeperiod t = new Timeperiod(112223); + System.out.println(92 + " seconds are equal to " + t); + } + static private void test2() { + final Timeperiod t = new Timeperiod(2310983); + System.out.print("weeks = " + t.weeks + + "\ndays=" + t.days + + "\nhours=" + t.hours + + "\nminutes=" + t.minutes + + "\nseconds=" + t.seconds + "\n"); + } +} \ No newline at end of file diff --git a/P/Sd1/Timeperiod/src/main/java/de/hdm_stuttgart/mi/sd1/Timeperiod.java b/P/Sd1/Timeperiod/src/main/java/de/hdm_stuttgart/mi/sd1/Timeperiod.java new file mode 100644 index 0000000000000000000000000000000000000000..47af9c58a8c7d07b2c555feaa1b89d169fa9e017 --- /dev/null +++ b/P/Sd1/Timeperiod/src/main/java/de/hdm_stuttgart/mi/sd1/Timeperiod.java @@ -0,0 +1,133 @@ +package de.hdm_stuttgart.mi.sd1; + +/** + * <p>Representing time periods measured in seconds by decomposing them into + * seconds, minutes hours, days and weeks. See {@link #Timeperiod(int)} + * for details.</p> + */ +public class Timeperiod { + + // Pre-caculated internal values + static private final int + SECONDS_PER_MINUTE = 60, // 60 minutes per hour + SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE, // 3600 seconds per hour + SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR, // 86400 seconds per day + SECONDS_PER_WEEK = 7 * SECONDS_PER_DAY; // 604800 seconds per week + + // Tedious calculation, will happen only on-demand + private String toStringValue = null; + + private String toStringImplement() { + final int highestValue; + + if (0 < weeks) { + highestValue = 5; // weeks, days, hours, minutes and seconds + } else if (0 < days) { + highestValue = 4; // days, hours, minutes and seconds + } else if (0 < hours) { + highestValue = 3; // hours, minutes and seconds + } else if (0 < minutes) { + highestValue = 2; // minutes and seconds + } else { + highestValue = 1; // only seconds, potentially zero as well + } + + final StringBuffer buffer = new StringBuffer(); + switch (highestValue) { + case 5: addSingularOrPlural(buffer, weeks, "week", ", "); + case 4: addSingularOrPlural(buffer, days, "day", ", "); + case 3: addSingularOrPlural(buffer, hours, "hour", ", "); + case 2: addSingularOrPlural(buffer, minutes, "minute", ", "); + case 1: addSingularOrPlural(buffer, seconds, "second", " and "); + } + return buffer.toString(); + } + + static private void addSingularOrPlural(final StringBuffer results, final int count, + final String base, String separator) { + + if (0 < results.toString().length()) { // Result buffer not empty? Separator required. + results.append(separator); + } + results.append(count).append(" ").append(base); + if (1 != count) { + results.append("s"); // 0 or plural, e.g. "3 seconds" + } + } + + /** + * <p>Turning a given number of seconds into weeks, days, hours, minutes and seconds. + * * Example: 3729781 seconds are equivalent to the sum of 1 second, 3 minutes, 4 hours, + * * 1 day and 6 weeks.</p> + * + * @param seconds A time period provided in seconds. + */ + public Timeperiod(int seconds) { + + weeks = seconds / SECONDS_PER_WEEK; + seconds = seconds % SECONDS_PER_WEEK; // remaining seconds without weeks + + days = seconds / SECONDS_PER_DAY; + seconds %= SECONDS_PER_DAY; // remaining seconds without days + + hours = seconds / SECONDS_PER_HOUR; + seconds %= SECONDS_PER_HOUR; // remaining seconds without minutes + + minutes = seconds / SECONDS_PER_MINUTE; + this.seconds = seconds % SECONDS_PER_MINUTE; // remaining seconds + } + + /** + * Copy a given instance. + * + * @param timeperiod Instance to be cloned. + */ + public Timeperiod(final Timeperiod timeperiod) { + seconds = timeperiod.seconds; + minutes = timeperiod.minutes; + hours = timeperiod.hours; + days = timeperiod.days; + weeks = timeperiod.weeks; + toStringValue = timeperiod.toStringValue; + } + + /** + * <p>providing a human readable form. The output corresponding to e.g. 112223 seconds will read:</p> + * + * <pre>1 day, 7 hours, 10 minutes and 23 seconds</pre> + * + * <p>Notice the absence of a "0 weeks" clause: Output starts with the »highest« non-zero component.</p> + * + * @return Informal description e.g. "1 day, 7 hours, 10 minutes and 23 seconds". + */ + @Override + public String toString() { + // Value calculation is tedious, thus doing it only on-demand. + // + if (null == toStringValue) { // Called for the first time, yet uninitialized? + toStringValue = toStringImplement(); + } + return toStringValue; + } + + /** + * Time period's seconds portion. + */ + public final int seconds; + /** + * Time period's minutes portion. + */ + public final int minutes; + /** + * Time period's hours portion. + */ + public final int hours; + /** + * Time period's days portion. + */ + public final int days; + /** + * Time period's weeksportion. + */ + public final int weeks; +} \ No newline at end of file diff --git a/P/Sd1/Timeperiod/src/main/resources/log4j2.xml b/P/Sd1/Timeperiod/src/main/resources/log4j2.xml new file mode 100644 index 0000000000000000000000000000000000000000..130f87a144c4eb0107a846e580c8fa7f5e819fc1 --- /dev/null +++ b/P/Sd1/Timeperiod/src/main/resources/log4j2.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration> + <Appenders> + <File name="A1" fileName="A1.log" append="false"> + <PatternLayout pattern="%t %-5p %c{2} - %m%n"/> + </File> + <Console name="STDOUT" target="SYSTEM_OUT"> + <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> + </Console> + </Appenders> + <Loggers> + + <!-- You my want to define class or package level per-logger rules --> + <Logger name="de.hdm_stuttgart.mi.sd1.App" level="debug"> + <AppenderRef ref="A1"/> + </Logger> + <Root level="info"> + <AppenderRef ref="STDOUT"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file diff --git a/P/Sd1/Timeperiod/src/test/java/de/hdm_stuttgart/mi/sd1/TimeperiodTest.java b/P/Sd1/Timeperiod/src/test/java/de/hdm_stuttgart/mi/sd1/TimeperiodTest.java new file mode 100644 index 0000000000000000000000000000000000000000..719507b17b42bb22542554183145923b69f9adb5 --- /dev/null +++ b/P/Sd1/Timeperiod/src/test/java/de/hdm_stuttgart/mi/sd1/TimeperiodTest.java @@ -0,0 +1,98 @@ +package de.hdm_stuttgart.mi.sd1; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test of {@link Timeperiod}. + */ +public class TimeperiodTest { + + // Helper methods for real tests. + static private void assertPeriodEqualImplement(final int expectedSeconds, + final int expectedMinutes, + final int expectedHours, + final int expectedDays, + final int expectedWeeks, + final String expectedToString, + final Timeperiod period) { + Assert.assertEquals(expectedSeconds, period.seconds); + Assert.assertEquals(expectedMinutes, period.minutes); + Assert.assertEquals(expectedHours, period.hours); + Assert.assertEquals(expectedDays, period.days); + Assert.assertEquals(expectedWeeks, period.weeks); + + Assert.assertEquals(expectedToString, period.toString()); + + } + + static private void assertPeriodEqual(final int expectedSeconds, + final int expectedMinutes, + final int expectedHours, + final int expectedDays, + final int expectedWeeks, + final String expectedToString, + final Timeperiod period) { + + // Testing period in question + assertPeriodEqualImplement( + expectedSeconds, expectedMinutes, expectedHours, expectedDays, expectedWeeks, expectedToString, + period); + + // Also testing copy constructor + assertPeriodEqualImplement( + expectedSeconds, expectedMinutes, expectedHours, expectedDays, expectedWeeks, expectedToString, + new Timeperiod(period)); + } + + /** + * Test constructor zero seconds. + */ + @Test + public void testZero() { + assertPeriodEqual( + 0,0,0,0,0, + "0 seconds", + new Timeperiod(0)); + } + + @Test + public void testMinute() { + assertPeriodEqual(0,1,0,0,0, + "1 minute and 0 seconds", + new Timeperiod(60)); + assertPeriodEqual(50,0,0,0,0, + "50 seconds", + new Timeperiod(50)); + assertPeriodEqual(12,1,0,0,0, + "1 minute and 12 seconds", + new Timeperiod(72)); + assertPeriodEqual(2,5,0,0,0, + "5 minutes and 2 seconds", + new Timeperiod(302)); + } + + @Test + public void testHour() { + assertPeriodEqual(0,0,2,0,0, + "2 hours, 0 minutes and 0 seconds", + new Timeperiod(7200)); + assertPeriodEqual(59,59,0,0,0, + "59 minutes and 59 seconds", + new Timeperiod(3599)); + assertPeriodEqual(40,1,1,0,0, + "1 hour, 1 minute and 40 seconds", + new Timeperiod(3700)); + } + + @Test + public void testVarious() { + assertPeriodEqual(1,3,4,1,6, + "6 weeks, 1 day, 4 hours, 3 minutes and 1 second", + new Timeperiod(3729781)); + + assertPeriodEqual(23,56,17,5,3, + "3 weeks, 5 days, 17 hours, 56 minutes and 23 seconds", + new Timeperiod(2310983)); + } +} \ No newline at end of file