-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support container parameters overrides (#17)
- Loading branch information
1 parent
e358549
commit c7eeb4e
Showing
4 changed files
with
86 additions
and
57 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
70 changes: 37 additions & 33 deletions
70
src/main/java/com/epam/aidial/util/mapping/ListMapper.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 |
---|---|---|
@@ -1,33 +1,37 @@ | ||
package com.epam.aidial.util.mapping; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
public class ListMapper<T> { | ||
private final List<T> list; | ||
private final Supplier<T> itemFactory; | ||
private final BiConsumer<T, String> nameSetter; | ||
private final Map<String, T> map; | ||
|
||
public ListMapper(List<T> list, NamedItemMapper<T> itemMapper) { | ||
this.list = list; | ||
this.itemFactory = itemMapper.factory(); | ||
this.nameSetter = itemMapper.setter(); | ||
this.map = list.stream() | ||
.collect(Collectors.toMap(itemMapper.getter(), Function.identity())); | ||
} | ||
|
||
public MappingChain<T> get(String name) { | ||
return new MappingChain<>(map.computeIfAbsent(name, n -> { | ||
T newItem = itemFactory.get(); | ||
nameSetter.accept(newItem, n); | ||
list.add(newItem); | ||
|
||
return newItem; | ||
})); | ||
} | ||
} | ||
package com.epam.aidial.util.mapping; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
public class ListMapper<T> { | ||
private final List<T> list; | ||
private final Supplier<T> itemFactory; | ||
private final BiConsumer<T, String> nameSetter; | ||
private final Map<String, T> map; | ||
|
||
public ListMapper(List<T> list, NamedItemMapper<T> itemMapper) { | ||
this.list = list; | ||
this.itemFactory = itemMapper.factory(); | ||
this.nameSetter = itemMapper.setter(); | ||
this.map = list.stream() | ||
.collect(Collectors.toMap(itemMapper.getter(), Function.identity())); | ||
} | ||
|
||
public MappingChain<T> get(String name) { | ||
return getOrDefault(name, itemFactory); | ||
} | ||
|
||
public MappingChain<T> getOrDefault(String name, Supplier<T> itemFactory) { | ||
return new MappingChain<>(map.computeIfAbsent(name, n -> { | ||
T newItem = itemFactory.get(); | ||
nameSetter.accept(newItem, n); | ||
list.add(newItem); | ||
|
||
return newItem; | ||
})); | ||
} | ||
} |
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