package com.onaro.sanscreen.client.view.init; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.apache.commons.lang3.StringUtils; import com.onaro.util.xml.XmlIterator; public class ViewDirectorInitInfo implements Cloneable { private String type; private String name; private Set refresherTypes; /** * Map of additional optional key/value parameters for the view initialization. * This variable is lazily initialized in the {@link #getParametersImpl()} method (which * should be used for all accesses) */ private Map parameters; /** * The unique id of this view. */ private String id; public ViewDirectorInitInfo(String type, String name) { this(type,name, null); } public ViewDirectorInitInfo(String type, String name, String refresherTypesStr) { if (StringUtils.isBlank(type)) throw new IllegalArgumentException("type"); //$NON-NLS-1$ if (StringUtils.isBlank(name)) throw new IllegalArgumentException("name"); //$NON-NLS-1$ this.type = type; this.name = name; HashSet refresherTypes = new HashSet(); if (StringUtils.isNotBlank(refresherTypesStr)) { String[] types = refresherTypesStr.split(","); //$NON-NLS-1$ Collections.addAll(refresherTypes, types); } setRefresherTypes(refresherTypes); } public ViewDirectorInitInfo(XmlIterator confItr) { this(confItr.getName(), confItr.getAttribute("name"), confItr.getAttribute("refreshTypes")); //$NON-NLS-1$ //$NON-NLS-2$ id = confItr.getAttribute("id"); //$NON-NLS-1$ if (StringUtils.isBlank(id)) id = null; } /** * Returns the type of view */ public String getType() { return type; } public final void setType(String type) { this.type = type; } /** * Returns the name of the view */ public String getName() { if (name == null) throw new IllegalStateException(); return name; } /** * Get the refresh type(s) (comma deliminated) used for the view */ public Set getRefresherTypes() { return refresherTypes; } public void setRefresherTypes(Set refresherTypes) { this.refresherTypes = refresherTypes; } public void setRefresherTypes(String...refresherTypes) { Set types = new HashSet(Arrays.asList(refresherTypes)); setRefresherTypes(types); } /** * Set a parameter on the init info * @param key the parameter key (should be a constant) * @param value parameter value * @return the previous value set for the given key, null if no previous value */ public Object setParameter(String key, Object value) { return getParametersImpl().put(key, value); } /** * Get a parameter from the init info * @param key the parameter key (should be a constant) * @return the value set for the given key, null if no value has been set for the key */ public Object getParameter(String key) { return getParametersImpl().get(key); } /** * Get the complete map of parameters set on the init info * @return an unmodifiable version of the parameters map */ public Map getParameters() { return Collections.unmodifiableMap(getParametersImpl()); } /** * Get the parameters map. This method will lazily initialize the parameters map on first use. * @return the parameters map. */ private Map getParametersImpl() { if (parameters == null) { parameters = new HashMap(); } return parameters; } @Override public ViewDirectorInitInfo clone() throws CloneNotSupportedException { ViewDirectorInitInfo clone = (ViewDirectorInitInfo)super.clone(); // deep clone stuff: clone.setRefresherTypes(new HashSet(refresherTypes)); if (parameters != null) { clone.parameters = new HashMap(parameters); } return clone; } /** * The unique id of this view, if the id is not defined, the name. */ public final String getId() { String id = this.id; if (id == null) { id = getName(); } if (id == null) throw new IllegalStateException(); return id; } public final void setId(String id) { this.id = id; } public static String getId(XmlIterator confItr) { String id = confItr.getAttribute("id"); //$NON-NLS-1$ if (StringUtils.isBlank(id)) { id = confItr.getAttribute("name"); //$NON-NLS-1$ } return id; } }