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

org.nuiton.jaxx.runtime.context.JAXXInitialContext Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-6
Show newest version
/*
 * #%L
 * JAXX :: Runtime
 * %%
 * Copyright (C) 2008 - 2017 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package org.nuiton.jaxx.runtime.context;

import org.nuiton.jaxx.runtime.JAXXContext;
import org.nuiton.jaxx.runtime.JAXXObject;

import java.util.Map.Entry;

/**
 * An initial context to be inject in a {@link JAXXObject}.
 *
 * The method {@link #add(Object)}  register a simple value.
 *
 * The method {@link #add(String, Object)} register a named value.
 *
 * The method {@link #to(JAXXContext)} inject in the {@link JAXXObject} the values registred in the initial context.
 *
 * The  initial context is also a "limited" {@link JAXXContext}, since we can only use the two methods
 *
 * {@link #getContextValue(Class)} or {@link #getContextValue(Class, String)}.
 *
 * @see JAXXContext
 */
public class JAXXInitialContext extends DefaultJAXXContext {

    public JAXXInitialContext() {
        super();
    }

    /**
     * Register a simple (none named) value in the context.
     *
     * @param value the value to be registred in the context
     * @return the instance of the context
     */
    public JAXXInitialContext add(Object value) {
        return add((String) null, value);
    }

    /**
     * Register a named value in the context.
     *
     * @param name  the name of the value
     * @param value the value to registred
     * @return the instance of the context
     */
    public JAXXInitialContext add(String name, Object value) {
        super.setContextValue(value, name);
        return this;
    }

    /**
     * Register a named (or not) value in the context.
     *
     * @param    type of data to add
     * @param def   definition of entry
     * @param value the value to registred
     * @return the instance of the context
     */
    public  JAXXInitialContext add(JAXXContextEntryDef def, O value) {
        super.setContextValue(value, def.getName());
        return this;
    }

    /**
     * Inject all the registed values into the {@link JAXXObject}
     *
     * @param dst the object to fill.
     */
    public void to(JAXXContext dst) {
        if (parentContext != null) {
            dst.setContextValue(parentContext);
        }
        for (Entry, Object> entry : data.entrySet()) {
            dst.setContextValue(entry.getValue(), entry.getKey().getName());
        }
    }

    @Override
    public void setContextValue(Object o) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setContextValue(Object o, String name) {
        throw new UnsupportedOperationException();
    }

    @Override
    public  void removeContextValue(Class klazz) {
        throw new UnsupportedOperationException();
    }

    @Override
    public  void removeContextValue(Class klazz, String name) {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy