Commit 22c97800 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Enumeration chapter

parent 46ad97b5
/target/
/.settings/
.classpath
.project
dependency-reduced-pom.xml
*.log
<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>enumdevelop</artifactId>
<version>0.9</version>
<packaging>jar</packaging>
<name>enumdevelop</name>
<url>https://freedocs.mi.hdm-stuttgart.de/sd1SectToolsOfTheTrade2.html</url>
<description>Motivatiog enums.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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.1</version>
<configuration>
<linksource>true</linksource>
<additionalOptions>
<additionalOption>-html5</additionalOption>
</additionalOptions>
</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.integer_representation.Day</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
</project>
package de.hdm_stuttgart.mi.sd1.class_wrapper;
/**
* Representing day values
*/
public class Day {
static public final Day
MONDAY = new Day(),
TUESDAY = new Day(),
WEDNESDAY = new Day(),
THURSDAY = new Day(),
FRIDAY = new Day(),
SATURDAY = new Day(),
SUNDAY = new Day();
public static String getDaysName(final Day day) {
if (MONDAY == day) { // Switch no longer possible, sigh!
return "Monday";
} else if (TUESDAY == day) {
return "Tuesday";
} else if (WEDNESDAY == day) {
return "Wednesday";
} else if (THURSDAY == day) {
return "Thursday";
} else if (FRIDAY == day) {
return "Friday";
} else if (SATURDAY == day) {
return "Saturday";
} else if (SUNDAY == day) {
return "Sunday";
} else {
return "Illegal day instance: " + day;
}
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.class_wrapper;
public class Driver {
public static void main(String[] args) {
final Lecture phpIntro = new Lecture(Day.MONDAY, "PHP introduction"),
advancedJava = new Lecture(Day.FRIDAY, "Advanced Java");
System.out.println(phpIntro);
System.out.println(advancedJava);
}
}
package de.hdm_stuttgart.mi.sd1.class_wrapper;
public class Lecture {
public final Day dayHeld; /* e.g. to be held on Tuesdays */
public final String title; /* e.g. «PHP introduction» */
public Lecture(final Day dayHeld, final String title) {
this.dayHeld = dayHeld;
this.title = title;
}
@Override
public String toString() {
return "Lecture «" + title + "» being held each " + Day.getDaysName(dayHeld);
}
}
package de.hdm_stuttgart.mi.sd1.class_wrapper;
public class Screwed {
public static void main(String[] args) {
final Day PAST_SUNDAY = new Day();
final Lecture phpIntro = new Lecture(PAST_SUNDAY, "PHP introduction");
System.out.println(phpIntro.toString());
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.class_wrapper_daysname;
/**
* Representing day values
*/
public class Day {
public final String name;
private Day(final String name) {
this.name = name;
}
static public final Day
MONDAY = new Day("Monday"),
TUESDAY = new Day("Tuesday"),
WEDNESDAY = new Day("Wednesday"),
THURSDAY = new Day("Thursday"),
FRIDAY = new Day("Friday"),
SATURDAY = new Day("Saturday"),
SUNDAY = new Day("Sunday");
@Override
public String toString() {
return name;
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.class_wrapper_daysname;
public class Driver {
public static void main(String[] args) {
final Lecture phpIntro = new Lecture(Day.MONDAY, "PHP introduction"),
advancedJava = new Lecture(Day.FRIDAY, "Advanced Java");
System.out.println(phpIntro.toString());
System.out.println(advancedJava.toString());
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.class_wrapper_daysname;
public class Lecture {
public final Day dayHeld; /* e.g. to be held on Tuesdays */
public final String title; /* e.g. «PHP introduction» */
public Lecture(final Day dayHeld, final String title) {
this.dayHeld = dayHeld;
this.title = title;
}
@Override
public String toString() {
return "Lecture «" + title +
"» being held each " + dayHeld.toString();
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.class_wrapper_private;
/**
* Representing day values
*/
public class Day {
private Day() {} // Disallow object creation outside this class
static public final Day
MONDAY = new Day(),
TUESDAY = new Day(),
WEDNESDAY = new Day(),
THURSDAY = new Day(),
FRIDAY = new Day(),
SATURDAY = new Day(),
SUNDAY = new Day();
public static String getDaysName(final Day day) {
if (MONDAY == day) { // Switch no longer possible, sigh!
return "Monday";
} else if (TUESDAY == day) {
return "Tuesday";
} else if (WEDNESDAY == day) {
return "Wednesday";
} else if (THURSDAY == day) {
return "Thursday";
} else if (FRIDAY == day) {
return "Friday";
} else if (SATURDAY == day) {
return "Saturday";
} else if (SUNDAY == day) {
return "Sunday";
} else {
return "Illegal day instance: " + day;
}
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.class_wrapper_private;
public class Driver {
public static void main(String[] args) {
final Lecture phpIntro = new Lecture(Day.MONDAY, "PHP introduction"),
advancedJava = new Lecture(Day.FRIDAY, "Advanced Java");
System.out.println(phpIntro);
System.out.println(advancedJava);
}
}
package de.hdm_stuttgart.mi.sd1.class_wrapper_private;
public class Lecture {
public final Day dayHeld; /* e.g. to be held on Tuesdays */
public final String title; /* e.g. «PHP introduction» */
public Lecture(final Day dayHeld, final String title) {
this.dayHeld = dayHeld;
this.title = title;
}
@Override
public String toString() {
return "Lecture «" + title + "» being held each " + Day.getDaysName(dayHeld);
}
}
package de.hdm_stuttgart.mi.sd1.class_wrapper_private;
public class Screwed {
public static void main(String[] args) {
// No longer possible:
// 'Day()' has private access in 'de.hdm_stuttgart.mi.sd1.class_wrapper_private.Day'
//final Day PAST_SUNDAY = new Day();
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.integer_representation;
/**
* Representing day values
*/
public class Day {
static public final int
MONDAY = 1,
TUESDAY = 2,
WEDNESDAY = 3,
THURSDAY = 4,
FRIDAY = 5,
SATURDAY = 6,
SUNDAY = 7;
public static String getDaysName(final int day) {
switch (day) {
case MONDAY: return "Monday";
case TUESDAY: return "Tuesday";
case WEDNESDAY: return "Wednesday";
case THURSDAY: return "Thursday";
case FRIDAY: return "Friday";
case SATURDAY: return "Saturday";
case SUNDAY: return "Sunday";
default: return "Illegal day's code: " + day;
}
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.integer_representation;
public class Driver {
public static void main(String[] args) {
final Lecture phpIntro = new Lecture(Day.MONDAY, "PHP introduction"),
advancedJava = new Lecture(Day.FRIDAY, "Advanced Java");
System.out.println(phpIntro);
System.out.println(advancedJava);
System.out.println(Screwed2.getPrice(Day.SUNDAY, 2));
System.out.println(Screwed2.getPrice(2, Day.SUNDAY));
}
}
package de.hdm_stuttgart.mi.sd1.integer_representation;
public class Lecture {
public final int dayHeld; /* e.g. to be held on Tuesdays */
public final String title; /* e.g. «PHP introduction» */
public Lecture(final int dayHeld, final String title) {
this.dayHeld = dayHeld;
this.title = title;
}
@Override
public String toString() {
return "Lecture «" + title + "» being held each " + Day.getDaysName(dayHeld);
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.integer_representation;
public class Screwed {
public static void main(String[] args) {
final Lecture phpIntro = new Lecture(88, "PHP introduction");
System.out.println(phpIntro);
}
}
package de.hdm_stuttgart.mi.sd1.integer_representation;
public class Screwed2 {
/**
* Charge double prices on weekends
* @param day Day of week
* @param amount
* @return the effective amount depending on day of week.
*/
static public int getPrice(final int day, final int amount) {
switch (day) {
case Day.SATURDAY:
case Day.SUNDAY:
return 2 * amount;
default:
return amount;
}
}
}
package de.hdm_stuttgart.mi.sd1.use_enum;
/**
* Representing day values
*/
public enum Day {
MONDAY("Monday"),
TUESDAY("Tuesday"),
WEDNESDAY("Wednesday"),
THURSDAY("Thursday"),
FRIDAY("Friday"),
SATURDAY("Saturday"),
SUNDAY("Sunday");
final String name;
Day(final String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
public static String getItalianDayname(final Day day) {
switch (day) {
case MONDAY: return "Lunedì";
case TUESDAY: return "Martedì";
case WEDNESDAY: return "Mercoledì";
case THURSDAY: return "Giovedì ";
case FRIDAY: return "Venerdì";
case SATURDAY: return "Sabato";
case SUNDAY: return "Domenica";
}
return null; // Actually unreachable, but static
// code analysis is limited
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.use_enum;
public class Driver {
public static void main(String[] args) {
final Lecture phpIntro = new Lecture(Day.MONDAY, "PHP introduction"),
advancedJava = new Lecture(Day.FRIDAY, "Advanced Java");
System.out.println(phpIntro);
System.out.println(advancedJava);
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.use_enum;
public class Lecture {
public final Day dayHeld; /* e.g. to be held on Tuesdays */
public final String title; /* e.g. «PHP introduction» */
public Lecture(final Day dayHeld, final String title) {
this.dayHeld = dayHeld;
this.title = title;
}
@Override
public String toString() {
return "Lecture »" + title + "« being held each " + dayHeld;
}
}
\ No newline at end of file
......@@ -9768,6 +9768,258 @@ After call: <emphasis role="red">6</emphasis></screen></td>
</figure>
</section>
<section xml:id="sd1_enum">
<title>Enumerations (<code language="java">enum</code>).</title>
<figure xml:id="sd1_enumMotivation">
<title>Motivation</title>
<itemizedlist>
<listitem>
<para>Modeling finite sets of discrete states.</para>
</listitem>
<listitem>
<para>No dynamic change of state set.</para>
</listitem>
<listitem>
<para>Examples:</para>
<para>Door: {<code>OPEN</code>, <code>CLOSED</code>}</para>
<para>State of matter: {<code>SOLID</code>, <code>LIQUID</code>,
<code>GASEOUS</code>}</para>
</listitem>
</itemizedlist>
</figure>
<section xml:id="sd1_sect_enumIntegerRepresent">
<title>Enumeration by integer representation</title>
<figure xml:id="sd1_fig_weekdaysByInt">
<title>Weekdays <code language="java">int</code>
representation</title>
<programlisting language="java">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/integer_representation/Day.java">Day</link> {
static public final int
MONDAY = 1,
TUESDAY = 2,
WEDNESDAY = 3,
THURSDAY = 4,
FRIDAY = 5,
SATURDAY = 6,
SUNDAY = 7;
}</programlisting>
</figure>
<figure xml:id="sd1_fig_lectureHeldOnWeekday">
<title>Weekly offered lectures</title>
<programlisting language="java">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/integer_representation/Lecture.java">Lecture</link> {
public final int dayHeld; /* e.g. to be held on Tuesdays */
public final String title; /* e.g. «PHP introduction» */
public Lecture(final int dayHeld, final String title) {
this.dayHeld = dayHeld;
this.title = title;
}
}</programlisting>
</figure>
<figure xml:id="sd1_fig_createLectureByIntDay">
<title>Weekly offered lectures</title>
<programlisting language="none"><link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/integer_representation/Driver.java">Driver</link>.java:
final Lecture
phpIntro = new Lecture(<emphasis role="red">Day.MONDAY</emphasis>, "PHP introduction"),
advancedJava = new Lecture(<emphasis role="red">Day.FRIDAY</emphasis>, "Advanced Java");</programlisting>
</figure>
<figure xml:id="sd1_fig_intToDaysName">
<title>Integer values to day names</title>
<programlisting language="java">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/integer_representation/Day.java">Day</link> {
...
public static String getDaysName(final int day) {
switch (day) {
case MONDAY: return "Monday";
case TUESDAY: return "Tuesday";
case WEDNESDAY: return "Wednesday";
case THURSDAY: return "Thursday";
case FRIDAY: return "Friday";
case SATURDAY: return "Saturday";
case SUNDAY: return "Sunday";
default: return "Illegal day's code: " + day;
}
}
}</programlisting>
</figure>
<figure xml:id="sd1_fig_daysToString">
<title>Providing lecture info</title>
<programlisting language="java">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/integer_representation/Lecture.java">Lecture</link> {
...
public String toString() {
return "Lecture «" + title + "» being held each " +
Day.getDaysName(dayHeld);
}
}</programlisting>
</figure>
<figure xml:id="sd1_fig_printLectureInfo">
<title>Printing lecture info</title>
<programlisting language="java">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/integer_representation/Lecture.java">Lecture</link> {
...
public String toString() {
return "Lecture «" + title + "» being held each " + Day.getDaysName(dayHeld);
}
}</programlisting>
</figure>
<figure xml:id="sd1_fig_printLectureInfoSample">
<title>Sample lectures</title>
<informaltable border="0">
<colgroup width="36%"/>
<colgroup width="64%"/>
<tr>
<td valign="top"><programlisting language="java">// Class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/integer_representation/Driver.java">Driver</link>
final Lecture
phpIntro = new Lecture(
Day.MONDAY, "PHP introduction"),
advancedJava = new Lecture(
Day.FRIDAY, "Advanced Java");
System.out.println(phpIntro);
System.out.println(advancedJava);</programlisting></td>
<td valign="top"><screen>Lecture «PHP introduction»
being held each Monday