Package magnet

Enum Scoping

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Scoping>

    public enum Scoping
    extends java.lang.Enum<Scoping>
    Declares the way Magnet binds instances of @Instance-annotated classes into scopes. See each separate setting for more detail.
    • 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.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • 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 and typeB are not bound into the scope. Each new scope.getSingle(TypeB.class) call will return new instances of typeA and typeB.

        
         @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 and typeB are bound into root.

        
         @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 to root;
        • TypeB depends on TypeA and because the instance of TypeA is available in root, and instance of TypeB can also be created in root.

        In the example below we bind instance of typeA into scope and typeB instance gets bound into the scope 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 in scope, thus the scope is the top most scope for dependent typeB 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 and typeB are bound into scope. Each new scope.getSingle(TypeB.class) call on the same instance of scope will return same instances of typeA and typeB.

        
         @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 name
        java.lang.NullPointerException - if the argument is null