Enum Scoping
- java.lang.Object
-
- java.lang.Enum<Scoping>
-
- magnet.Scoping
-
-
Enum Constant Summary
Enum Constants Enum Constant Description DIRECT
Magnet will bind created instance into the same scope, in which this instance has been created.TOPMOST
Magnet will bind created instance into the most top scope in the chain of scopes, where all dependencies for the created instance are still fulfilled.UNSCOPED
Magnet will not bind created instance into any scope.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Scoping
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.static Scoping[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
UNSCOPED
public static final Scoping UNSCOPED
Magnet will not bind created instance into any scope. This is equivalent to having a factory creating new instances each time when instance is requested.In the example below, both instances
typeA
andtypeB
are not bound into thescope
. Each newscope.getSingle(TypeB.class)
call will return new instances oftypeA
andtypeB
.@Instance(type = TypeA.class, scoping = Scoping.UNSCOPED) class TypeA { TypeA() {} } @Instance(type = TypeB.class, scoping = Scoping.UNSCOPED) class TypeB { final TypeA typeA; TypeB(@NonNull TypeA dependency) { typeA = dependency; } } ... Scope scope = Magnet.createScope(); TypeB typeB = scope.getSingle(TypeB.class); TypeA typeA = typeB.typeA; TypeA typeA2 = scope.getSingle(TypeA.class); typeA !== typeA2 // different instances
-
TOPMOST
public static final Scoping TOPMOST
Magnet will bind created instance into the most top scope in the chain of scopes, where all dependencies for the created instance are still fulfilled.Scopes in Magnet can build a chain of parent-child relations (see
Scope
for more detail). This option allows binding instances of annotated class into a one of parent scopes. Magnet goes up the scope chain and checks, whether all dependencies for the instance can still be satisfied in that scope. The most top reached scope with satisfied dependencies is the scope, into which the instance gets bound.In the example below both instances
typeA
andtypeB
are bound intoroot
.@Instance(type = TypeA.class, scoping = Scoping.TOPMOST) class TypeA { TypeA() {} } @Instance(type = TypeB.class) class TypeB { final TypeA typeA; TypeB(@NonNull TypeA dependency) { typeA = dependency; } } ... Scope root = Magnet.createScope(); Scope scope = root.createSubscope(); TypeB typeB = scope.getSingle(TypeB.class); TypeA typeA = typeB.typeA; TypeA typeA2 = scope.getSingle(TypeA.class); typeA === typeA2 // same instance
Explanation:TypeA
has no dependencies and thus it gets bound toroot
;TypeB
depends onTypeA
and because the instance ofTypeA
is available inroot
, and instance ofTypeB
can also be created inroot
.
In the example below we bind instance of
typeA
intoscope
andtypeB
instance gets bound into thescope
too.@Instance(type = TypeA.class, scoping = Scoping.TOPMOST) class TypeA { TypeA() {} } @Instance(type = TypeB.class, scoping = Scoping.TOPMOST) class TypeB { final TypeA typeA; TypeB(@NonNull TypeA dependency) { typeA = dependency; } } ... Scope root = Magnet.createScope(); Scope scope = root.createSubscope().bind(TypeA.class, new TypeA()); TypeB typeB = scope.getSingle(TypeB.class); TypeA typeA = typeB.typeA; TypeA typeA2 = scope.getSingle(TypeA.class); typeA === typeA2 // same instance
Explanation:typeB
cannot be bound in a scope above its dependencies;typeA
is available inscope
, thus thescope
is the top most scope for dependenttypeB
too.
-
DIRECT
public static final Scoping DIRECT
Magnet will bind created instance into the same scope, in which this instance has been created.In the example below, both instances
typeA
andtypeB
are bound intoscope
. Each newscope.getSingle(TypeB.class)
call on the same instance ofscope
will return same instances oftypeA
andtypeB
.@Instance(type = TypeA.class, scoping = Scoping.DIRECT) class TypeA { TypeA() {} } @Instance(type = TypeB.class, scoping = Scoping.DIRECT) class TypeB { final TypeA typeA; TypeB(@NonNull TypeA dependency) { typeA = dependency; } } ... Scope root = Magnet.createScope(); Scope scope = root.createSubscope(); TypeB typeB = scope.getSingle(TypeB.class); TypeA typeA = typeB.typeA; TypeA typeA2 = scope.getSingle(TypeA.class); typeA === typeA2 // same instance
-
-
Method Detail
-
values
public static Scoping[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (Scoping c : Scoping.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static Scoping valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is null
-
-