Skip to content

Commit 6b2395b

Browse files
committed
Architekturdemo Interfaces
1 parent 6a43b3d commit 6b2395b

File tree

10 files changed

+371
-0
lines changed

10 files changed

+371
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package edu.hm.hafner.util;
2+
3+
import java.io.Serial;
4+
import java.io.Serializable;
5+
import java.util.Objects;
6+
7+
import com.google.errorprone.annotations.Immutable;
8+
9+
/**
10+
* An option is a key value pair.
11+
*
12+
* @author Ullrich Hafner
13+
*/
14+
@Immutable
15+
public final class Option implements Serializable, Comparable<Option> {
16+
@Serial
17+
private static final long serialVersionUID = -1L;
18+
19+
private final String key;
20+
private final String value;
21+
private final int wert = 0;
22+
23+
/**
24+
* Creates an entry representing a mapping from the specified key to the specified value.
25+
*
26+
* @param key
27+
* the key represented by this entry
28+
* @param value
29+
* the value represented by this entry
30+
*/
31+
public Option(final String key, final String value) {
32+
this.key = key;
33+
this.value = value;
34+
}
35+
36+
/**
37+
* Returns the key corresponding to this entry.
38+
*
39+
* @return the key corresponding to this entry
40+
*/
41+
public String getKey() {
42+
return key;
43+
}
44+
45+
/**
46+
* Returns the value corresponding to this entry.
47+
*
48+
* @return the value corresponding to this entry
49+
*/
50+
public String getValue() {
51+
return value;
52+
}
53+
54+
@Override
55+
public boolean equals(final Object o) {
56+
if (this == o) {
57+
return true;
58+
}
59+
if (o == null || getClass() != o.getClass()) {
60+
return false;
61+
}
62+
Option option = (Option) o;
63+
return Objects.equals(key, option.key) && Objects.equals(value, option.value);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(key, value);
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "Option{key='" + key + '\'' + ", value='" + value + '\'' + '}';
74+
}
75+
76+
/**
77+
* Compares this object with the specified object for order. Returns a negative integer, zero, or a positive
78+
* integer as this object is less than, equal to, or greater than the specified object.
79+
*
80+
* <p>The implementor must ensure {@link Integer#signum
81+
* signum}{@code (x.compareTo(y)) == -signum(y.compareTo(x))} for all {@code x} and {@code y}. (This implies that
82+
* {@code x.compareTo(y)} must throw an exception if and only if {@code y.compareTo(x)} throws an exception.)
83+
*
84+
* <p>The implementor must also ensure that the relation is transitive:
85+
* {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies {@code x.compareTo(z) > 0}.
86+
*
87+
* <p>Finally, the implementor must ensure that {@code
88+
* x.compareTo(y)==0} implies that {@code signum(x.compareTo(z)) == signum(y.compareTo(z))}, for all {@code z}.
89+
*
90+
* @param other
91+
* the object to be compared.
92+
*
93+
* @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than
94+
* the specified object.
95+
* @throws NullPointerException
96+
* if the specified object is null
97+
* @throws ClassCastException
98+
* if the specified object's type prevents it from being compared to this object.
99+
*/
100+
@Override
101+
public int compareTo(final Option other) {
102+
if (other == null) {
103+
throw new IllegalArgumentException("Cannot compare to null");
104+
}
105+
return getKey().compareTo(other.getKey());
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package edu.hm.hafner.util.internal;
2+
3+
import java.io.Serial;
4+
import java.io.Serializable;
5+
import java.util.Objects;
6+
7+
import com.google.errorprone.annotations.Immutable;
8+
9+
/**
10+
* An option is a key value pair.
11+
*
12+
* @author Ullrich Hafner
13+
*/
14+
@Immutable
15+
public final class Option implements Serializable {
16+
@Serial
17+
private static final long serialVersionUID = -6416888680799872630L;
18+
19+
private final String key;
20+
private final String value;
21+
22+
/**
23+
* Creates an entry representing a mapping from the specified key to the specified value.
24+
*
25+
* @param key
26+
* the key represented by this entry
27+
* @param value
28+
* the value represented by this entry
29+
*/
30+
public Option(final String key, final String value) {
31+
this.key = key;
32+
this.value = value;
33+
}
34+
35+
/**
36+
* Returns the key corresponding to this entry.
37+
*
38+
* @return the key corresponding to this entry
39+
*/
40+
public String getKey() {
41+
return key;
42+
}
43+
44+
/**
45+
* Returns the value corresponding to this entry.
46+
*
47+
* @return the value corresponding to this entry
48+
*/
49+
public String getValue() {
50+
return value;
51+
}
52+
53+
@Override
54+
public boolean equals(final Object o) {
55+
if (this == o) {
56+
return true;
57+
}
58+
if (o == null || getClass() != o.getClass()) {
59+
return false;
60+
}
61+
Option option = (Option) o;
62+
return Objects.equals(key, option.key) && Objects.equals(value, option.value);
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return Objects.hash(key, value);
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "Option{key='" + key + '\'' + ", value='" + value + '\'' + '}';
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package edu.hm.hafner.util.internal;
2+
3+
import edu.hm.hafner.util.Option;
4+
5+
public class OtherMain {
6+
public static void main(final String[] args) {
7+
var option = new Option("key", "value");
8+
9+
var key = option.getKey();
10+
}
11+
}

src/main/java/module-info.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
requires com.google.errorprone.annotations;
77

88
exports edu.hm.hafner.util;
9+
exports edu.hm.hafner.util.internal;
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package edu.hm.hafner.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import nl.jqno.equalsverifier.EqualsVerifier;
6+
7+
import static org.assertj.core.api.Assertions.*;
8+
9+
class OptionComparableTest extends AbstractComparableTest<Option> {
10+
/**
11+
* Creates a subject under test. The SUT must be smaller than the SUT of the opposite method
12+
* {@link #createGreaterSut()}.
13+
*
14+
* @return the SUT
15+
*/
16+
@Override
17+
protected Option createSmallerSut() {
18+
return new Option("key-1", "value");
19+
}
20+
21+
/**
22+
* Creates a subject under test. The SUT must be greater than the SUT of the opposite method
23+
* {@link #createSmallerSut()}.
24+
*
25+
* @return the SUT
26+
*/
27+
@Override
28+
protected Option createGreaterSut() {
29+
return new Option("key-2", "value");
30+
}
31+
32+
@Test
33+
void shouldThrowNpe() {
34+
assertThatNullPointerException().isThrownBy(
35+
() -> createSmallerSut().compareTo(null));
36+
assertThatNullPointerException().isThrownBy(
37+
() -> createGreaterSut().compareTo(null));
38+
}
39+
40+
@Test
41+
void shouldAdhereToEquals() {
42+
EqualsVerifier.forClass(Option.class).withIgnoredFields("wert").verify();
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package edu.hm.hafner.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.*;
6+
7+
/**
8+
* Tests the class {@link Option}.
9+
*
10+
* @author Ullrich Hafner
11+
*/
12+
@SuppressWarnings("PMD")
13+
class OptionFullTest extends SerializableTest<Option> {
14+
private static final String KEY = "key";
15+
private static final String VALUE = "value";
16+
17+
@Test
18+
void shouldCreateOption() {
19+
Option option = createObject();
20+
21+
assertThat(option.getKey()).isEqualTo(KEY);
22+
assertThat(option.getValue()).isEqualTo(VALUE);
23+
}
24+
25+
@Test
26+
void shouldEnsureEqualsContracts() {
27+
Option option = createObject();
28+
29+
assertThat(option.equals(option)).isTrue();
30+
assertThat(option.equals(null)).isFalse();
31+
}
32+
33+
@Test
34+
void shouldEnsureEqualsContractsAbstractTest() {
35+
Object object = createObject();
36+
37+
assertThat(object.equals(object)).isTrue();
38+
assertThat(object.equals(null)).isFalse();
39+
}
40+
41+
@Test
42+
void shouldEnsureEqualsWithEqualsVerifier() {
43+
// EqualsVerifier.simple().forClass(Option.class).verify();
44+
}
45+
46+
private Option createObject() {
47+
return new Option(KEY, VALUE);
48+
}
49+
50+
/**
51+
* Verifies that saved serialized format (from a previous release) still can be resolved with the current
52+
* implementation of {@link Option}.
53+
*/
54+
@Test
55+
void shouldReadIssueFromOldSerialization() {
56+
byte[] restored = readAllBytes("/option.ser");
57+
58+
assertThatSerializableCanBeRestoredFrom(restored);
59+
}
60+
61+
@Override
62+
protected Option createSerializable() {
63+
return createObject();
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package edu.hm.hafner.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.*;
6+
7+
/**
8+
* Tests the class {@link Option}.
9+
*
10+
* @author Ullrich Hafner
11+
*/
12+
class OptionTest extends SerializableTest<Option> {
13+
private static final String KEY = "key";
14+
private static final String VALUE = "value";
15+
16+
@Test
17+
void shouldCreateOption() {
18+
Option option = new Option(KEY, VALUE);
19+
20+
assertThat(option.getKey()).isEqualTo(KEY);
21+
assertThat(option.getValue()).isEqualTo(VALUE);
22+
}
23+
24+
/**
25+
* Verifies that saved serialized format (from a previous release) still can be resolved with the current
26+
* implementation of {@link Option}.
27+
*/
28+
@Test
29+
void shouldReadOldInstance() {
30+
byte[] restored = readAllBytes("/option.ser");
31+
32+
assertThatSerializableCanBeRestoredFrom(restored);
33+
}
34+
35+
@Override
36+
protected Option createSerializable() {
37+
return new Option(KEY, VALUE);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package edu.hm.hafner.util;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Creates a serialization file for an {@link Option} instance.
7+
*
8+
* @author Ullrich Hafner
9+
*/
10+
final class OptionWriter {
11+
/**
12+
* Serializes an issue to a file. Use this method in case the issue properties have been changed and the readResolve
13+
* method has been adapted accordingly so that the old serialization still can be read.
14+
*
15+
* @param args
16+
* not used
17+
*
18+
* @throws IOException
19+
* if the file could not be written
20+
*/
21+
public static void main(final String... args) throws IOException {
22+
new OptionTest().createSerializationFile();
23+
}
24+
25+
private OptionWriter() {
26+
// prevents instantiation
27+
}
28+
}

src/test/resources/design.puml

+2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ skinparam component {
77
}
88

99
[Utilities] <<..util>>
10+
[Internal] <<..util.internal>>
11+
[Internal] -> [Utilities]
1012

1113
@enduml

src/test/resources/option.ser

100 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)