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

jaxx.runtime.DefaultJAXXContext Maven / Gradle / Ivy

The newest version!
package jaxx.runtime;

import static jaxx.runtime.JAXXContextEntryDef.newDef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.awt.Container;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * The default {@link JAXXContext} to be used in a {@link JAXXObject} by delegation.
 * 

* The values are store in a {@link Map} but we can not use directly the values as key. *

* Because, it does not work if we add for the same object multi entries (named and unamed)... *

* We prefer use as entry the {@link JAXXContextEntryDef} associated with the value. * * @author chemit */ public class DefaultJAXXContext implements JAXXContext { protected static final JAXXContextEntryDef PARENT_CONTEXT_ENTRY = newDef(JAXXContext.class); /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(DefaultJAXXContext.class); /** l'ui auquel est rattache le context */ protected JAXXObject ui; /** le context parent */ protected JAXXContext parentContext; /** les données contenues dans le context */ protected final Map data; public DefaultJAXXContext() { data = new HashMap(); } public DefaultJAXXContext(JAXXObject ui) { this(); this.ui = ui; } @Override public void setContextValue(T o) { setContextValue(o, null); } @Override public void setContextValue(T o, String name) { if (name == null && PARENT_CONTEXT_ENTRY.accept2(o.getClass(), null)) { setParentContext((JAXXContext) o); return; } JAXXContextEntryDef entry = getKey(name, o.getClass()); // first remove entry Object oldValue = remove0(o.getClass(), name); if (oldValue != null) { if (log.isDebugEnabled()) { log.debug("remove value " + oldValue.getClass() + " for " + entry); } } // then can put safely data.put(entry, o); } @Override public T getContextValue(Class clazz) { return getContextValue(clazz, null); } @SuppressWarnings({"unchecked"}) @Override public T getContextValue(Class clazz, String name) { if (parentContext != null && parentContext.getClass() == clazz || PARENT_CONTEXT_ENTRY.accept(clazz, name)) { return (T) getParentContext(); } for (Entry entry : data.entrySet()) { if (entry.getKey().accept(clazz, name)) { return (T) entry.getValue(); } } // no value found in this context, will try in the parent context if (JAXXContext.class == clazz) { // no seek in the parent context, since we are already looking for it return null; } JAXXContext parent = getParentContext(); if (parent == null) { // no parent context, so no value find return null; } // seek in parent context return parent.getContextValue(clazz, name); } @Override public void removeContextValue(Class klazz) { removeContextValue(klazz, null); } @Override public void removeContextValue(Class klazz, String name) { remove0(klazz, name); } @Override public O getParentContainer(Class clazz) { return this.getParentContainer(ui, clazz); } @SuppressWarnings({"unchecked"}) @Override public O getParentContainer(Object top, Class clazz) { if (ui == null) { throw new IllegalStateException("no ui attached to this context"); } if (top == null) { throw new IllegalArgumentException("top parameter can not be null"); } if (!Container.class.isAssignableFrom(top.getClass())) { throw new IllegalArgumentException("top parameter " + top + " is not a " + Container.class); } Container parent = ((Container) top).getParent(); if (parent != null && !clazz.isAssignableFrom(parent.getClass())) { parent = getParentContainer(parent, clazz); } return (O) parent; } /** * Obtain all the keys of data for a given type. * * @param klass the type of searched keys * @return the array of all names of keys for the given type of data * @since 1.3 */ public String[] getKeys(Class klass) { List keys = new java.util.ArrayList(); for (JAXXContextEntryDef key : data.keySet()) { if (key.getKlass() == klass) { keys.add(key.getName()); } } return keys.toArray(new String[keys.size()]); } public void clear() { data.clear(); } protected JAXXObject getUi() { return ui; } protected void setUi(JAXXObject ui) { this.ui = ui; } protected JAXXContextEntryDef getKey(String name, Class klass) { return JAXXContextEntryDef.newDef(name, klass); } @SuppressWarnings({"unchecked"}) protected T remove0(Class klazz, String name) { if (PARENT_CONTEXT_ENTRY.accept(klazz, name)) { JAXXContext old = getParentContext(); setParentContext(null); return (T) old; } JAXXContextEntryDef entry = null; for (JAXXContextEntryDef entryDef : data.keySet()) { if (entryDef.accept(klazz, name)) { entry = entryDef; break; } } if (entry != null) { return (T) data.remove(entry); } if (JAXXContext.class == klazz) { return null; } // try in parentContext JAXXContext parent = getParentContext(); if (parent == null) { return null; } if (parent instanceof DefaultJAXXContext) { return ((DefaultJAXXContext) parent).remove0(klazz, name); } return null; } protected JAXXContext getParentContext() { return parentContext; } protected void setParentContext(JAXXContext parentContext) { if (parentContext instanceof JAXXObject) { // keep the real context, not the ui parentContext = ((JAXXObject) parentContext).getDelegateContext(); } this.parentContext = parentContext; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy