1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

Clean up and javadoc

This commit is contained in:
Alena Laskavaia 2010-05-25 01:33:23 +00:00
parent 63954a8d48
commit 6a49ab36dd
12 changed files with 211 additions and 130 deletions

View file

@ -25,6 +25,7 @@ public class Messages extends NLS {
public static String CodanApplication_Usage; public static String CodanApplication_Usage;
public static String CodanApplication_verbose_option; public static String CodanApplication_verbose_option;
public static String CodanBuilder_Code_Analysis_On; public static String CodanBuilder_Code_Analysis_On;
public static String FileScopeProblemPreference_Label;
static { static {
// initialize resource bundle // initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class); NLS.initializeMessages(BUNDLE_NAME, Messages.class);

View file

@ -6,3 +6,4 @@ CodanApplication_Options=Options:
CodanApplication_all_option= -all - run on all projects in workspace CodanApplication_all_option= -all - run on all projects in workspace
CodanApplication_verbose_option= -verbose - print verbose build information CodanApplication_verbose_option= -verbose - print verbose build information
CodanBuilder_Code_Analysis_On=Code analysis on CodanBuilder_Code_Analysis_On=Code analysis on
FileScopeProblemPreference_Label=Exclusion and Inclusion

View file

@ -17,22 +17,17 @@ import java.io.StreamTokenizer;
/** /**
* Default implementation of problem preference. It keeps preference metadata * Default implementation of problem preference. It keeps preference metadata
* together with preference itself. * together with preference value. Some implementations may separate them.
* *
*/ */
public abstract class AbstractProblemPreference implements IProblemPreference { public abstract class AbstractProblemPreference implements IProblemPreference {
public static final String PARAM = "param"; //$NON-NLS-1$ public static final String PARAM = "param"; //$NON-NLS-1$
protected String key; private String key = PARAM;
protected String label; private String label = ""; //$NON-NLS-1$
protected String toolTip = null; private String toolTip = null;
protected PreferenceType type; private String uiInfo;
protected String uiInfo;
private IProblemPreference parent; private IProblemPreference parent;
public PreferenceType getType() {
return type;
}
public String getLabel() { public String getLabel() {
return label; return label;
} }
@ -50,11 +45,13 @@ public abstract class AbstractProblemPreference implements IProblemPreference {
} }
public void setKey(String key) { public void setKey(String key) {
if (key == null)
throw new NullPointerException("key"); //$NON-NLS-1$
if (isValidIdentifier(key)) if (isValidIdentifier(key))
this.key = key; this.key = key;
else else
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Key must have java identifier syntax or number, i.e no dots and other funky stuff"); //$NON-NLS-1$ "Key must have java identifier syntax or number, i.e no dots and other funky stuff: " + key); //$NON-NLS-1$
} }
protected boolean isValidIdentifier(String id) { protected boolean isValidIdentifier(String id) {
@ -81,12 +78,6 @@ public abstract class AbstractProblemPreference implements IProblemPreference {
this.toolTip = tooltip; this.toolTip = tooltip;
} }
public void setType(PreferenceType type) {
if (type == null)
throw new NullPointerException("Type cannot be null"); //$NON-NLS-1$
this.type = type;
}
public void setUiInfo(String uiinfo) { public void setUiInfo(String uiinfo) {
this.uiInfo = uiinfo; this.uiInfo = uiinfo;
} }
@ -136,7 +127,7 @@ public abstract class AbstractProblemPreference implements IProblemPreference {
/** /**
* @param parent * @param parent
* the parent to set * the parent to set
*/ */
public void setParent(IProblemPreference parent) { public void setParent(IProblemPreference parent) {
this.parent = parent; this.parent = parent;

View file

@ -22,30 +22,32 @@ import java.util.regex.Pattern;
* *
*/ */
public class BasicProblemPreference extends AbstractProblemPreference { public class BasicProblemPreference extends AbstractProblemPreference {
Object value; protected Object value;
{ private PreferenceType type = PreferenceType.TYPE_STRING;
key = PARAM;
type = PreferenceType.TYPE_STRING; public PreferenceType getType() {
return type;
}
public void setType(PreferenceType type) {
if (type == null)
throw new NullPointerException("Type cannot be null"); //$NON-NLS-1$
this.type = type;
} }
/** /**
* Generate an info with given key and label * Generate an info with given key and label
* *
* @param key * @param key
* - property id (use in actual property hash of a checker) * - property id (use in actual property hash of a checker)
* @param label * @param label
* - label to be shown to user * - label to be shown to user
* @param type * @param type
* - parameter type * - parameter type
* @return * @return
*/ */
public BasicProblemPreference(String key, String label, PreferenceType type) { public BasicProblemPreference(String key, String label, PreferenceType type) {
if (key == null) this(key, label);
throw new NullPointerException("key"); //$NON-NLS-1$
if (type == null)
throw new NullPointerException("type"); //$NON-NLS-1$
setKey(key);
setLabel(label);
setType(type); setType(type);
} }
@ -53,9 +55,9 @@ public class BasicProblemPreference extends AbstractProblemPreference {
* Generate an info with given key and label * Generate an info with given key and label
* *
* @param key * @param key
* - property id (use in actual property hash of a checker) * - property id (use in actual property hash of a checker)
* @param label * @param label
* - label to be shown to user * - label to be shown to user
* @return * @return
*/ */
public BasicProblemPreference(String key, String label) { public BasicProblemPreference(String key, String label) {
@ -113,7 +115,7 @@ public class BasicProblemPreference extends AbstractProblemPreference {
@Override @Override
public String toString() { public String toString() {
return "(" + type + ")" + key + ((value == null) ? "" : "=" + value); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ return "(" + type + ")" + getKey() + ((value == null) ? "" : "=" + value); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
} }
@Override @Override

View file

@ -16,6 +16,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.eclipse.cdt.codan.core.Messages;
import org.eclipse.cdt.codan.internal.core.CharOperation; import org.eclipse.cdt.codan.internal.core.CharOperation;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
@ -26,7 +27,9 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
/** /**
* TODO: add description * Custom preference for resource scope
*
* @noextend This class is not intended to be extended by clients.
*/ */
public class FileScopeProblemPreference extends AbstractProblemPreference { public class FileScopeProblemPreference extends AbstractProblemPreference {
public static final String KEY = "fileScope"; //$NON-NLS-1$ public static final String KEY = "fileScope"; //$NON-NLS-1$
@ -38,13 +41,18 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
public FileScopeProblemPreference() { public FileScopeProblemPreference() {
setKey(KEY); setKey(KEY);
setLabel("File Exclusion and Inclusion"); setLabel(Messages.FileScopeProblemPreference_Label);
setType(PreferenceType.TYPE_CUSTOM); }
public PreferenceType getType() {
return PreferenceType.TYPE_CUSTOM;
} }
/** /**
* Get attribute. Possible keys are EXCUSION and INCLUSION
*
* @param key * @param key
* @return * @return class attribute for given key
*/ */
public IPath[] getAttribute(String key) { public IPath[] getAttribute(String key) {
if (key == EXCLUSION) if (key == EXCLUSION)
@ -54,6 +62,11 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
return null; return null;
} }
/**
* Set attribute to a value. Possible keys are EXCUSION and INCLUSION
*
* @param key
*/
public void setAttribute(String key, IPath[] value) { public void setAttribute(String key, IPath[] value) {
if (key == EXCLUSION) if (key == EXCLUSION)
exclusion = value.clone(); exclusion = value.clone();
@ -62,7 +75,8 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
} }
/** /**
* @return * @return null for workspace, or project of the resource it is applicable
* for
*/ */
public IProject getProject() { public IProject getProject() {
if (resource != null) if (resource != null)
@ -71,7 +85,7 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
} }
/** /**
* @return * @return path of the resource it is applicable to
*/ */
public IPath getPath() { public IPath getPath() {
if (resource != null) if (resource != null)
@ -83,14 +97,14 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
/** /**
* @param resource * @param resource
* the resource to set * the resource to set
*/ */
public void setResource(IResource resource) { public void setResource(IResource resource) {
this.resource = resource; this.resource = resource;
} }
/** /**
* @return the resource * @return the resource for which scope is define. Null if workspace.
*/ */
public IResource getResource() { public IResource getResource() {
return resource; return resource;
@ -101,12 +115,7 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
+ exportPathList(EXCLUSION, exclusion); + exportPathList(EXCLUSION, exclusion);
} }
/** protected String exportPathList(String key, IPath[] arr) {
* @param inclusion2
* @param inclusion3
* @return
*/
private String exportPathList(String key, IPath[] arr) {
String res = key + "=>("; //$NON-NLS-1$ String res = key + "=>("; //$NON-NLS-1$
for (int i = 0; i < arr.length; i++) { for (int i = 0; i < arr.length; i++) {
if (i != 0) if (i != 0)
@ -116,13 +125,6 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
return res + ")"; //$NON-NLS-1$ return res + ")"; //$NON-NLS-1$
} }
/*
* (non-Javadoc)
*
* @see
* org.eclipse.cdt.codan.core.param.AbstractProblemPreference#importValue
* (java.io.StreamTokenizer)
*/
@Override @Override
public void importValue(StreamTokenizer tokenizer) throws IOException { public void importValue(StreamTokenizer tokenizer) throws IOException {
List<IPath> inc = importPathList(tokenizer, INCLUSION); List<IPath> inc = importPathList(tokenizer, INCLUSION);
@ -132,11 +134,6 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
exclusion = exc.toArray(new IPath[exc.size()]); exclusion = exc.toArray(new IPath[exc.size()]);
} }
/**
* @param tokenizer
* @param c
* @throws IOException
*/
private void checkChar(StreamTokenizer tokenizer, char c) private void checkChar(StreamTokenizer tokenizer, char c)
throws IOException { throws IOException {
tokenizer.nextToken(); tokenizer.nextToken();
@ -144,12 +141,6 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
throw new IllegalArgumentException("Expected " + c); //$NON-NLS-1$ throw new IllegalArgumentException("Expected " + c); //$NON-NLS-1$
} }
/**
* @param tokenizer
* @param inclusion2
* @throws IOException
* @throws IllegalAccessException
*/
private void checkKeyword(StreamTokenizer tokenizer, String keyword) private void checkKeyword(StreamTokenizer tokenizer, String keyword)
throws IOException { throws IOException {
tokenizer.nextToken(); tokenizer.nextToken();
@ -219,28 +210,29 @@ public class FileScopeProblemPreference extends AbstractProblemPreference {
} }
/** /**
* @param file * Checks that resource denotated by the given path is in scope (defined by
* @return * exclusion/inclusion settings of this class). In inclusion list is defined
* check first if it belongs to it, returns false if not.
* Then checks if it belongs to exclusion list and return false if it is.
*
* @param path
* - resource path
* @return true is given path is in scope
*/ */
public boolean isInScope(IPath file) { public boolean isInScope(IPath path) {
//System.err.println("test " + file + " " + exportValue()); //System.err.println("test " + file + " " + exportValue());
if (inclusion.length > 0) { if (inclusion.length > 0) {
if (!matchesFilter(file, inclusion)) if (!matchesFilter(path, inclusion))
return false; return false;
} }
if (exclusion.length > 0) { if (exclusion.length > 0) {
if (matchesFilter(file, exclusion)) if (matchesFilter(path, exclusion))
return false; return false;
} }
return true; return true;
} }
/** public boolean matchesFilter(IPath resourcePath, IPath[] paths) {
* @param resourcePath
* @param inclusion2
* @return
*/
private boolean matchesFilter(IPath resourcePath, IPath[] paths) {
char[] path = resourcePath.toString().toCharArray(); char[] path = resourcePath.toString().toCharArray();
for (int i = 0, length = paths.length; i < length; i++) { for (int i = 0, length = paths.length; i < length; i++) {
char[] pattern = paths[i].toString().toCharArray(); char[] pattern = paths[i].toString().toCharArray();

View file

@ -11,10 +11,14 @@
package org.eclipse.cdt.codan.core.param; package org.eclipse.cdt.codan.core.param;
/** /**
* Value of the problem preference. If more than one it can be composite, i.e. * Problem preference. If problem has more than one it can be composite, i.e.
* map. Instead of implementing this interface use * map. Instead of implementing this interface clients must extend
* {@link AbstractProblemPreference} class. * {@link AbstractProblemPreference} class.
* *
* Problem Preference constist of preference metadata
* (IProblemPreferenceDescriptor)
* and value of preference (IProblemPreferenceValue).
*
* @noextend This interface is not intended to be extended by clients. * @noextend This interface is not intended to be extended by clients.
* @noimplement This interface is not intended to be implemented by clients. * @noimplement This interface is not intended to be implemented by clients.
*/ */

View file

@ -28,7 +28,7 @@ public interface IProblemPreferenceCompositeDescriptor {
IProblemPreference getChildDescriptor(String key); IProblemPreference getChildDescriptor(String key);
/** /**
* Available if type is list or map. Returns array of children. * Available for composite types. Returns array of children.
* *
* @return * @return
*/ */

View file

@ -17,9 +17,26 @@ package org.eclipse.cdt.codan.core.param;
* @noimplement This interface is not intended to be implemented by clients. * @noimplement This interface is not intended to be implemented by clients.
*/ */
public interface IProblemPreferenceCompositeValue { public interface IProblemPreferenceCompositeValue {
/**
* Returns value of the child element of a given key
*
* @param key
* @return
*/
Object getChildValue(String key); Object getChildValue(String key);
/**
* Sets the value of the child element of a given key
*
* @param key
* @param value
*/
void setChildValue(String key, Object value); void setChildValue(String key, Object value);
/**
* Removes child element matching the given key
*
* @param key
*/
void removeChildValue(String key); void removeChildValue(String key);
} }

View file

@ -15,7 +15,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* Problem parameter usually key=value settings that allow to alter checker * Problem parameter usually key=value settings that allows to alter checker
* behaviour for given problem. For example if checker finds violation of naming * behaviour for given problem. For example if checker finds violation of naming
* conventions for function, parameter would be the pattern of allowed names. * conventions for function, parameter would be the pattern of allowed names.
* *
@ -84,17 +84,18 @@ public interface IProblemPreferenceDescriptor extends Cloneable {
/** /**
* type of the parameter, supports boolean, integer, string, file, list and * type of the parameter, supports boolean, integer, string, file, list and
* hash. If list is the value - it is an array - subparameter can be * map. For list type child preference can be
* accessed by number, if hash is the value - it is a hash - subparameter * accessed by number (index), if map is the type child preference can be
* can be accesses by name * accessed by a key (string)
* *
* @return string value of the type * @return type of the preference
*/ */
PreferenceType getType(); PreferenceType getType();
/** /**
* Additional info on how it is represented in the ui, for example boolean * Additional info on how it is represented in the ui, for example boolean
* can be represented as checkbox, drop-down and so on, Values TBD * can be represented as checkbox, drop-down and so on, Values TBD.
* Not supported at the moment.
* *
* @return ui info or null if not set * @return ui info or null if not set
*/ */
@ -108,7 +109,7 @@ public interface IProblemPreferenceDescriptor extends Cloneable {
String getLabel(); String getLabel();
/** /**
* Detailed explanation of parameter * Detailed explanation of parameter. Not supported at the moment.
* *
* @return the toolTip text * @return the toolTip text
*/ */
@ -118,7 +119,10 @@ public interface IProblemPreferenceDescriptor extends Cloneable {
IProblemPreference getParent(); IProblemPreference getParent();
public void setParent(IProblemPreference parent); /**
* Combined key of values from parents plus itself separated by dot
*
* @return
*/
String getQualifiedKey(); String getQualifiedKey();
} }

View file

@ -12,7 +12,8 @@ package org.eclipse.cdt.codan.core.param;
/** /**
* Value of the problem preference. If more than one it can be composite, i.e. * Value of the problem preference. If more than one it can be composite, i.e.
* map * map.Extend {@link AbstractProblemPreference} class
* to implement this interface.
* *
* @noextend This interface is not intended to be extended by clients. * @noextend This interface is not intended to be extended by clients.
* @noimplement This interface is not intended to be implemented by clients. * @noimplement This interface is not intended to be implemented by clients.

View file

@ -29,36 +29,34 @@ public class ListProblemPreference extends AbstractProblemPreference implements
/** /**
* @param key * @param key
* - key to access this preference
* @param label * @param label
* - label to be shown in UI
*/ */
public ListProblemPreference(String key, String label) { public ListProblemPreference(String key, String label) {
setKey(key); setKey(key);
setLabel(label); setLabel(label);
} }
@Override
public PreferenceType getType() { public PreferenceType getType() {
return PreferenceType.TYPE_LIST; return PreferenceType.TYPE_LIST;
} }
@Override
public void setType(PreferenceType type) {
throw new UnsupportedOperationException();
}
/** /**
* Set child descriptor (all elements have the same) * Set child descriptor (all elements have the same). Value and key
* of the it would be ignored and reset.
* *
* @param i * @param desc
* @param info
* @return * @return
*/ */
public IProblemPreference setChildDescriptor(IProblemPreference info) { public IProblemPreference setChildDescriptor(IProblemPreference desc) {
childDescriptor = info; childDescriptor = desc;
childDescriptor.setValue(null); if (desc != null) {
((AbstractProblemPreference) childDescriptor) childDescriptor.setValue(null);
.setKey(COMMON_DESCRIPTOR_KEY); ((AbstractProblemPreference) childDescriptor)
return info; .setKey(COMMON_DESCRIPTOR_KEY);
}
return desc;
} }
/** /**
@ -76,10 +74,22 @@ public class ListProblemPreference extends AbstractProblemPreference implements
return getChildDescriptor(key); return getChildDescriptor(key);
} }
/**
* Returns descriptor of the child elements
*
* @return
*/
public IProblemPreference getChildDescriptor() { public IProblemPreference getChildDescriptor() {
return childDescriptor; return childDescriptor;
} }
/**
* Returns cloned descriptor of the i'th child. Modifying return value would
* not affect internal state of the list element.
*
* @param i
* @return
*/
public IProblemPreference getChildDescriptor(int i) { public IProblemPreference getChildDescriptor(int i) {
Object value = list.get(i); Object value = list.get(i);
AbstractProblemPreference desc = (AbstractProblemPreference) childDescriptor AbstractProblemPreference desc = (AbstractProblemPreference) childDescriptor
@ -94,7 +104,7 @@ public class ListProblemPreference extends AbstractProblemPreference implements
* If key is null or # return generic descriptor with null value. * If key is null or # return generic descriptor with null value.
* *
* @throws NumberFormatException * @throws NumberFormatException
* if key is not number * if key is not number
*/ */
public IProblemPreference getChildDescriptor(String key) public IProblemPreference getChildDescriptor(String key)
throws NumberFormatException { throws NumberFormatException {
@ -113,6 +123,9 @@ public class ListProblemPreference extends AbstractProblemPreference implements
return getChildDescriptor(iv.intValue()); return getChildDescriptor(iv.intValue());
} }
/**
* Return array of clones values of child preferences.
*/
public IProblemPreference[] getChildDescriptors() { public IProblemPreference[] getChildDescriptors() {
IProblemPreference[] res = new IProblemPreference[list.size()]; IProblemPreference[] res = new IProblemPreference[list.size()];
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
@ -122,8 +135,12 @@ public class ListProblemPreference extends AbstractProblemPreference implements
} }
public Object getChildValue(String key) { public Object getChildValue(String key) {
IProblemPreference childInfo = getChildDescriptor(key); int index = Integer.parseInt(key);
return childInfo.getValue(); return getChildValue(index);
}
public Object getChildValue(int index) {
return list.get(index);
} }
public void setChildValue(String key, Object value) { public void setChildValue(String key, Object value) {
@ -131,11 +148,7 @@ public class ListProblemPreference extends AbstractProblemPreference implements
setChildValue(i, value); setChildValue(i, value);
} }
/** public void setChildValue(int i, Object value) {
* @param i
* @param value
*/
protected void setChildValue(int i, Object value) {
if (value != null) { if (value != null) {
while (i >= list.size()) { while (i >= list.size()) {
list.add(null); list.add(null);
@ -229,23 +242,44 @@ public class ListProblemPreference extends AbstractProblemPreference implements
} }
} }
/**
* If info key is '#' resets common descritors to null, otherwise removes
* value
*/
public void removeChildDescriptor(IProblemPreference info) { public void removeChildDescriptor(IProblemPreference info) {
throw new UnsupportedOperationException(); if (info.getKey().equals(COMMON_DESCRIPTOR_KEY))
setChildDescriptor(null);
else
removeChildValue(info.getKey());
} }
/**
* @return children size
*/
public int size() { public int size() {
return list.size(); return list.size();
} }
/**
* Removes all values from the list
*/
public void clear() { public void clear() {
list.clear(); list.clear();
} }
/**
* Return array of values of children elements.
*/
@Override @Override
public Object getValue() { public Object getValue() {
return getValues(); return getValues();
} }
/**
* Sets list value to values of array given as argument.
*
* @param value - must be Object[]
*/
@Override @Override
public void setValue(Object value) { public void setValue(Object value) {
Object[] values = (Object[]) value; Object[] values = (Object[]) value;
@ -264,6 +298,9 @@ public class ListProblemPreference extends AbstractProblemPreference implements
return childDescriptor + ":" + list.toString(); //$NON-NLS-1$ return childDescriptor + ":" + list.toString(); //$NON-NLS-1$
} }
/**
* Return array of values of children elements.
*/
public Object[] getValues() { public Object[] getValues() {
return list.toArray(new Object[list.size()]); return list.toArray(new Object[list.size()]);
} }

View file

@ -40,27 +40,21 @@ public class MapProblemPreference extends AbstractProblemPreference implements
/** /**
* @param key * @param key
* - key for itself * - key for itself
* @param label * @param label
* - label for this group of parameters * - label for this group of parameters
*/ */
public MapProblemPreference(String key, String label) { public MapProblemPreference(String key, String label) {
setKey(key); setKey(key);
setLabel(label); setLabel(label);
} }
@Override
public PreferenceType getType() { public PreferenceType getType() {
return PreferenceType.TYPE_MAP; return PreferenceType.TYPE_MAP;
} }
@Override
public void setType(PreferenceType type) {
throw new UnsupportedOperationException();
}
/** /**
* Get parameter into for element by key * Get parameter preference for element by key
* *
*/ */
public IProblemPreference getChildDescriptor(String key) { public IProblemPreference getChildDescriptor(String key) {
@ -68,32 +62,52 @@ public class MapProblemPreference extends AbstractProblemPreference implements
} }
/** /**
* Put parameter info into the map for element with the key equals to * Adds or replaces child descriptor and value for the element with the key
* info.getKey() * equals to desc.getKey(). The desc object would be put in the map, some of
* its field may be modified.
* *
* @param i * @param desc
* @param info
*/ */
public IProblemPreference addChildDescriptor(IProblemPreference desc) { public IProblemPreference addChildDescriptor(IProblemPreference desc) {
desc.setParent(this); ((AbstractProblemPreference) desc).setParent(this);
hash.put(desc.getKey(), desc); hash.put(desc.getKey(), desc);
return desc; return desc;
} }
/**
* Return list of child descriptors. Client should threat returned value as
* read only,
* and not assume that modifying its elements would modify actual child
* values.
*/
public IProblemPreference[] getChildDescriptors() { public IProblemPreference[] getChildDescriptors() {
return hash.values().toArray( return hash.values().toArray(
new IProblemPreference[hash.values().size()]); new IProblemPreference[hash.values().size()]);
} }
/**
* Returns value of the child element by its key
*/
public Object getChildValue(String key) { public Object getChildValue(String key) {
IProblemPreference childInfo = getChildDescriptor(key); IProblemPreference childInfo = getChildDescriptor(key);
return childInfo.getValue(); return childInfo.getValue();
} }
/**
* Set child value by its key
*/
public void setChildValue(String key, Object value) { public void setChildValue(String key, Object value) {
getChildDescriptor(key).setValue(value); IProblemPreference pref = getChildDescriptor(key);
if (pref == null)
throw new IllegalArgumentException("Preference for " + key //$NON-NLS-1$
+ " must exists before setting its value"); //$NON-NLS-1$
pref.setValue(value);
hash.put(key, pref); // cannot assume getChildDescriptor returns shared value
} }
/**
* Removes child value and descriptor by key
*/
public void removeChildValue(String key) { public void removeChildValue(String key) {
hash.remove(key); hash.remove(key);
} }
@ -170,8 +184,11 @@ public class MapProblemPreference extends AbstractProblemPreference implements
} }
} }
/**
* Removes child descriptor by its key
*/
public void removeChildDescriptor(IProblemPreference info) { public void removeChildDescriptor(IProblemPreference info) {
hash.remove(info); hash.remove(info.getKey());
} }
public int size() { public int size() {
@ -187,6 +204,11 @@ public class MapProblemPreference extends AbstractProblemPreference implements
return hash.values().toString(); return hash.values().toString();
} }
/**
* Value of this preference is a map key=>value of child preferences.
* Modifying this returned map would not change internal state of this
* object.
*/
@Override @Override
public Object getValue() { public Object getValue() {
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(); LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
@ -198,6 +220,14 @@ public class MapProblemPreference extends AbstractProblemPreference implements
return map; return map;
} }
/**
* Set values for this object child elements. Elements are not present in
* this map would be removed.
* Preference descriptors for the keys must be set before calling this
* method, unless value if instanceof IProblemPreference.
*
* @param value - must be Map<String,Object>
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void setValue(Object value) { public void setValue(Object value) {
@ -212,6 +242,7 @@ public class MapProblemPreference extends AbstractProblemPreference implements
if (value2 instanceof IProblemPreference) { if (value2 instanceof IProblemPreference) {
hash.put(key, (IProblemPreference) value2); hash.put(key, (IProblemPreference) value2);
} else { } else {
setChildValue(key, value2);
IProblemPreference pref = hash2.get(key); IProblemPreference pref = hash2.get(key);
pref.setValue(value2); pref.setValue(value2);
hash.put(key, pref); hash.put(key, pref);