All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.varmateo.yawg.spi.PageVarsBuilder Maven / Gradle / Ivy

/**************************************************************************
 *
 * Copyright (c) 2017-2020 Yawg project contributors.
 *
 **************************************************************************/

package com.varmateo.yawg.spi;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;


/**
 * A builder of PageVars instances.
 */
public final class PageVarsBuilder {


    private final Map _map;


    /**
     *
     */
    private PageVarsBuilder(final Map map) {

        _map = new HashMap<>(map);
    }


    /**
     * Creates a new empty builder.
     *
     * @return A newly created PageVarsBuilder instance.
     */
    public static PageVarsBuilder create() {

        return new PageVarsBuilder(Collections.emptyMap());
    }


    /**
     * Creates a new builder initialized with the data from the given
     * map.
     *
     * @param data Used for initializing the builder state.
     *
     * @return A newly created Builder instance.
     */
    public static PageVarsBuilder create(final Map data) {

        return new PageVarsBuilder(data);
    }


    /**
     * Creates a new builder initialized with the data from the given
     * PageVars.
     *
     * @param pageVars Used for initializing the builder state.
     *
     * @return A newly created Builder instance.
     */
    public static PageVarsBuilder create(final PageVars pageVars) {

        return new PageVarsBuilder(pageVars.asMap());
    }


    /**
     * Adds or updates a page variable variable.
     */
    public PageVarsBuilder addVar(
            final String varName,
            final Object varValue) {

        _map.put(varName, varValue);
        return this;
    }


    /**
     * Adds to this builder all the variables contained in the
     * given PageVars.
     */
    public PageVarsBuilder addPageVars(final PageVars pageVars) {

        _map.putAll(pageVars.asMap());
        return this;
    }


    /**
     * Creates a new PageVars instance that containing
     * the data currently in this builder.
     *
     * @return A new PageVars instance.
     */
    public PageVars build() {

        return new PageVarsImpl(_map);
    }


    /**
     *
     */
    private static final class PageVarsImpl
            implements PageVars {


        private final Map _map;


        /**
         *
         */
        /* default */ PageVarsImpl() {

            _map = Collections.emptyMap();
        }


        /**
         *
         */
        /* default */ PageVarsImpl(final Map data) {

            _map = Collections.unmodifiableMap(data);
        }


        /**
         * {@inheritDoc}
         */
        @Override
        public Optional get(final String key) {

            final Object value = _map.get(key);
            return Optional.ofNullable(value);
        }


        /**
         * {@inheritDoc}
         */
        @Override
        public Map asMap() {

            return _map;
        }

    }


}