Class B2JsonInitializedTypeHandler<T>

  • All Implemented Interfaces:
    B2JsonTypeHandler<T>
    Direct Known Subclasses:
    B2JsonNonUrlTypeHandler, B2JsonTypeHandlerWithDefaults

    public abstract class B2JsonInitializedTypeHandler<T>
    extends java.lang.Object
    implements B2JsonTypeHandler<T>
    Base class for all implementations of B2JsonTypeHandler that have an initialize() method.

    (De)serialization is expected to be fast, so each implementation class gathers information it needs when it's set up, so when it's time to run it has all of the necessary information at hand. This gets tricky because dependencies between handlers may have loops.

    The plan is to initialize in three phases:

    First, the constructor, which must not depend on any other handlers, and which must gather all of the information that other handlers will need.

    Second, the initialize() method does any work that needs information from other type handlers.

    Third, check the validity of default values, now that all type handlers have gone through at least the second phase.

    All phases are protected by the lock on B2JsonHandlerMap, so they don't need to lock, and the data they store in the object is guaranteed to be visible without further locking.

    Methods that return data set during initialize() should include this check:

    Preconditions.checkState(isInitialized());

    NOTE: adding the initialize() method to BzJsonTypeHandler would change the interface and break any clients who have written their own handlers.

    • Constructor Detail

      • B2JsonInitializedTypeHandler

        public B2JsonInitializedTypeHandler()
    • Method Detail

      • initializeImplementation

        protected void initializeImplementation​(B2JsonHandlerMap b2JsonHandlerMap)
                                         throws B2JsonException
        Does any initialization specific to the concrete class. Override in classes that need to gather information from other handlers.
        Throws:
        B2JsonException
      • isInitialized

        protected boolean isInitialized()
        Has the initialize method run?