Class ContextMultiBuilder


  • public final class ContextMultiBuilder
    extends java.lang.Object
    A mutable object that uses the builder pattern to specify properties for a multi-kind LDContext.

    Use this builder if you need to construct a context that has multiple ContextKind values, each with its own corresponding LDContext. To define a single-kind context, use LDContext.builder(String) or any of the single-kind factory methods in LDContext.

    Obtain an instance of ContextMultiBuilder by calling LDContext.multiBuilder(); then, call add(LDContext) to specify the individual context for each kind. The add(LDContext) method returns a reference to the same builder, so calls can be chained:

    
         LDContext context = LDContext.multiBuilder()
           .add(LDContext.create("context-key-123abc"))
           .add(LDContext.create(ContextKind.of("organization"), "my-org-key"))
           .build();
     

    A ContextMultiBuilder should not be accessed by multiple threads at once. Once you have called build(), the resulting LDContext is immutable and is safe to use from multiple threads. Instances created with build() are not affected by subsequent actions taken on the builder.

    See Also:
    LDContext.createMulti(LDContext...)
    • Method Detail

      • build

        public LDContext build()
        Creates an LDContext from the current builder properties.

        The LDContext is immutable and will not be affected by any subsequent actions on the builder.

        It is possible for a ContextMultiBuilder to represent an invalid state. Instead of throwing an exception, the ContextMultiBuilder always returns an LDContext, and you can check LDContext.isValid() or LDContext.getError() to see if it has an error. See LDContext.isValid() for more information about invalid context conditions. If you pass an invalid context to an SDK method, the SDK will detect this and will log a description of the error.

        If only one context kind was added to the builder, this method returns a single-kind LDContext rather than a multi-kind one.

        Returns:
        a new LDContext
      • add

        public ContextMultiBuilder add​(LDContext context)
        Adds an individual LDContext for a specific kind to the builer.

        It is invalid to add more than one LDContext for the same kind, or to add an LDContext that is itself invalid. This error is detected when you call build().

        If the nested context is multi-kind, this is exactly equivalent to adding each of the individual kinds from it separately. For instance, in the following example, "multi1" and "multi2" end up being exactly the same:

        
             LDContext c1 = LDContext.create(ContextKind.of("kind1"), "key1");
             LDContext c2 = LDContext.create(ContextKind.of("kind2"), "key2");
             LDContext c3 = LDContext.create(ContextKind.of("kind3"), "key3");
        
             LDContext multi1 = LDContext.multiBuilder().add(c1).add(c2).add(c3).build();
        
             LDContext c1plus2 = LDContext.multiBuilder().add(c1).add(c2).build();
             LDContext multi2 = LDContext.multiBuilder().add(c1plus2).add(c3).build();
         
        Parameters:
        context - the context to add
        Returns:
        the builder