-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-48 - Static factory methods for Association.
Introduced Association.forAggregate(…) and ….forId(…) to easily create Association instances without having to explicitly implement it. Implementations return a SimpleAssociation that implements equals(…) and hashCode() by verifying the equality of both the actual association type and the target identifier. Introduced pointsToSameAggregateAs(…) for identifier only comparison.
- Loading branch information
Showing
4 changed files
with
258 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
jmolecules-ddd/src/main/java/org/jmolecules/ddd/types/SimpleAssociation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jmolecules.ddd.types; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* @author Oliver Drotbohm | ||
*/ | ||
class SimpleAssociation<T extends AggregateRoot<T, ID>, ID extends Identifier> implements Association<T, ID> { | ||
|
||
private final Supplier<ID> identifier; | ||
|
||
SimpleAssociation(Supplier<ID> identifier) { | ||
this.identifier = identifier; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.jmolecules.ddd.types.Identifiable#getId() | ||
*/ | ||
@Override | ||
public ID getId() { | ||
return identifier.get(); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see java.lang.Object#toString() | ||
*/ | ||
@Override | ||
public String toString() { | ||
return getId().toString(); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see java.lang.Object#equals(java.lang.Object) | ||
*/ | ||
@Override | ||
public boolean equals(Object obj) { | ||
|
||
if (obj == this) { | ||
return true; | ||
} | ||
|
||
if (obj == null || !obj.getClass().equals(this.getClass())) { | ||
return false; | ||
} | ||
|
||
return ((Association<?, ?>) obj).getId().equals(identifier.get()); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see java.lang.Object#hashCode() | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
return getId().hashCode(); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
jmolecules-ddd/src/test/java/org/jmolecules/ddd/types/AssociationUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jmolecules.ddd.types; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link Association}. | ||
* | ||
* @author Oliver Drotbohm | ||
*/ | ||
public class AssociationUnitTests { | ||
|
||
@Test // #48 | ||
void createsAssociationFromIdentifier() { | ||
|
||
SampleIdentifier id = new SampleIdentifier(); | ||
|
||
assertThat(Association.forId(id).getId()).isEqualTo(id); | ||
} | ||
|
||
@Test // #48 | ||
void createsAssoctionFromAggregate() { | ||
|
||
SampleIdentifier identifier = new SampleIdentifier(); | ||
SampleAggregate aggregate = new SampleAggregate(identifier); | ||
|
||
assertThat(Association.forAggregate(aggregate).getId()).isEqualTo(identifier); | ||
} | ||
|
||
@Test // #48 | ||
void rejectsNullIdentifier() { | ||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> Association.forId(null)); | ||
} | ||
|
||
@Test // #48 | ||
void rejectsNullAggregate() { | ||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> Association.forAggregate(null)); | ||
} | ||
|
||
@Test // #48 | ||
void associationsEqualIfTheyPointToTheSameId() { | ||
|
||
SampleIdentifier identifier = new SampleIdentifier(); | ||
SampleAggregate aggregate = new SampleAggregate(identifier); | ||
|
||
assertThat(Association.forId(identifier)).isEqualTo(Association.forAggregate(aggregate)); | ||
} | ||
|
||
@Test // #48 | ||
void pointsToSameAggregate() { | ||
|
||
SampleIdentifier identifier = new SampleIdentifier(); | ||
Association<?, SampleIdentifier> simpleAssociation = Association.forId(identifier); | ||
SampleAssociation sampleAssociation = new SampleAssociation(identifier); | ||
|
||
assertThat(simpleAssociation.pointsToSameAggregateAs(sampleAssociation)); | ||
assertThat(sampleAssociation.pointsToSameAggregateAs(simpleAssociation)); | ||
} | ||
|
||
static class SampleIdentifier implements Identifier {} | ||
|
||
static class SampleAggregate implements AggregateRoot<SampleAggregate, SampleIdentifier> { | ||
|
||
private final SampleIdentifier id; | ||
|
||
SampleAggregate(SampleIdentifier id) { | ||
this.id = id; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.jmolecules.ddd.types.Identifiable#getId() | ||
*/ | ||
@Override | ||
public SampleIdentifier getId() { | ||
return id; | ||
} | ||
} | ||
|
||
static class SampleAssociation implements Association<SampleAggregate, SampleIdentifier> { | ||
|
||
SampleIdentifier id; | ||
|
||
public SampleAssociation(SampleIdentifier id) { | ||
this.id = id; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.jmolecules.ddd.types.Identifiable#getId() | ||
*/ | ||
@Override | ||
public SampleIdentifier getId() { | ||
return id; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters