-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implemented Equalizer, which implements a type check, which allows BeanMapper#map(Collection, Collection) and BeanMapper#map(Map, Map) to only map compatible collections. - Implemented BeanMapper#map(Collection, Collection), which maps a source collection, to a target collection, if both the elements of the source and target implement Equalizer and use the same class for their ID. - Implemented BeanMapper#map(Map, Map), which maps a source map, to a target map, if both the values of the source and target implement Equalizer and use the same class for their key/ID. - Both methods also map elements to the target that do not have a counterpart, simply mapping those elements to the target class. - Updated CHANGELOG.md - Added tests
- Loading branch information
1 parent
74fde61
commit 2311660
Showing
7 changed files
with
276 additions
and
0 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
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
24 changes: 24 additions & 0 deletions
24
src/main/java/io/beanmapper/core/collections/Equalizer.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,24 @@ | ||
package io.beanmapper.core.collections; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* An interface that may be implemented by a class, to make the class compatible with BeanMapper#map(Collection, Collection) and BeanMapper#map(Map, Map). | ||
* @param <ID> the type of the ID within the subclass | ||
*/ | ||
public interface Equalizer<ID> { | ||
|
||
/** | ||
* Tests whether the two classes are equal, for the purposes of mapping the calling object, to the target object. Default implementation should be | ||
* overridden for every implementing class. | ||
* @param target the target instance, to which the calling instance is looking to be mapped. | ||
* @return whether the caller and target are equal. | ||
* @param <T> the class of the target | ||
*/ | ||
default <T extends Equalizer<ID>> boolean isEqual(T target) { | ||
return Objects.equals(this, target); | ||
} | ||
|
||
ID getId(); | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
src/test/java/io/beanmapper/map_to_target_collection/DissimilarEqualizeableEntity.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,39 @@ | ||
package io.beanmapper.map_to_target_collection; | ||
|
||
import io.beanmapper.core.collections.Equalizer; | ||
|
||
public class DissimilarEqualizeableEntity implements Equalizer<Long> { | ||
|
||
private Long id; | ||
|
||
private String name; | ||
|
||
public DissimilarEqualizeableEntity(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public DissimilarEqualizeableEntity() { | ||
} | ||
|
||
@Override | ||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public <T extends Equalizer<Long>> boolean isEqual(T target) { | ||
return Equalizer.super.isEqual(target); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/io/beanmapper/map_to_target_collection/EqualizeableEntity.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,46 @@ | ||
package io.beanmapper.map_to_target_collection; | ||
|
||
import java.util.Objects; | ||
|
||
import io.beanmapper.core.collections.Equalizer; | ||
|
||
public class EqualizeableEntity implements Equalizer<Long> { | ||
private Long id; | ||
private String name; | ||
|
||
private Long age; | ||
|
||
public EqualizeableEntity(Long id, String name, Long age) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setAge(Long age) { | ||
this.age = age; | ||
} | ||
|
||
public Long getAge() { | ||
return age; | ||
} | ||
|
||
@Override | ||
public <T extends Equalizer<Long>> boolean isEqual(T target) { | ||
return Objects.equals(this.id, target.getId()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/io/beanmapper/map_to_target_collection/OtherEqualizeableEntity.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,48 @@ | ||
package io.beanmapper.map_to_target_collection; | ||
|
||
import io.beanmapper.core.collections.Equalizer; | ||
|
||
public class OtherEqualizeableEntity implements Equalizer<Long> { | ||
|
||
private Long id; | ||
|
||
private String name; | ||
|
||
private Long age; | ||
|
||
public OtherEqualizeableEntity(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public OtherEqualizeableEntity() {} | ||
|
||
@Override | ||
public <T extends Equalizer<Long>> boolean isEqual(T target) { | ||
return this.id.equals(target.getId()); | ||
} | ||
|
||
@Override | ||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(Long age) { | ||
this.age = age; | ||
} | ||
} |