-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break recursion when a factory calls Wiring again to construct an ins…
…tance Super of isn't necessarily going to return an instance of a class derived from the class it is registered with - for example (registering super of Class1, but a call to the factory can request construction of an interface of Class1, for which the factory returns Class2, which implements the interface but is not a sub-class of Class1) Add additional unit tests Fixed bug so that the the singleton scope respects qualifiers Added lots of documentation
- Loading branch information
Showing
16 changed files
with
313 additions
and
73 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
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
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
40 changes: 34 additions & 6 deletions
40
core/src/main/java/foundation/stack/datamill/configuration/SingletonScope.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,29 +1,57 @@ | ||
package foundation.stack.datamill.configuration; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.*; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class SingletonScope implements Scope { | ||
private Map<Class<?>, Object> constructed = new HashMap<>(); | ||
private Map<QualifiedType, Object> constructed = new HashMap<>(); | ||
|
||
@Override | ||
public <T, R extends T> R resolve( | ||
Wiring wiring, | ||
QualifyingFactory<T, R> factory, | ||
Class<? extends T> type, | ||
Collection<String> qualifiers) { | ||
Object instance = constructed.get(type); | ||
Object instance = constructed.get(new QualifiedType(type, qualifiers)); | ||
if (instance == null) { | ||
instance = factory.call(wiring, type, qualifiers); | ||
if (instance != null) { | ||
constructed.put(type, instance); | ||
constructed.put(new QualifiedType(type, qualifiers), instance); | ||
} | ||
} | ||
|
||
return (R) instance; | ||
} | ||
|
||
private static class QualifiedType { | ||
private final Class<?> type; | ||
private final String[] qualifiers; | ||
|
||
public QualifiedType(Class<?> type, Collection<String> qualifiers) { | ||
this.type = type; | ||
this.qualifiers = qualifiers != null && qualifiers.size() > 0 ? | ||
qualifiers.toArray(new String[qualifiers.size()]) : null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return qualifiers != null ? | ||
Objects.hash(type, Arrays.hashCode(qualifiers)) : | ||
type.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof QualifiedType) { | ||
QualifiedType other = (QualifiedType) obj; | ||
return qualifiers != null ? | ||
Arrays.equals(qualifiers, other.qualifiers) && type == other.type : | ||
type == other.type; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.