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

simple.common.FeatureList Maven / Gradle / Ivy

The newest version!

package simple.common;

import java.util.HashMap;

/**
 * A list of [enabled] features.
 */
public class FeatureList {

    private HashMap list;

    /**
     * Create a list of [enabled] features.
     */
    public FeatureList() {
        list = new HashMap();
    }

    //
    // FeatureList
    //
    /**
     * Clear the list of features.
     */
    public void clear() {
        getList().clear();
    }

    /**
     * Read an encoded features list.
     *
     * Encoded features are in the form of:
* name[=value][:name[=value]...] */ public void decode(String encoded) { int len; int pos; int epos; int cpos; String name; String value; getList().clear(); len = encoded.length(); pos = 0; while (pos < len) { if ((cpos = encoded.indexOf(':', pos)) == -1) { cpos = len; } if (((epos = encoded.indexOf('=', pos)) == -1) || (epos > cpos)) { epos = cpos; } name = encoded.substring(pos, epos); if (epos < cpos) { value = encoded.substring(epos + 1, cpos); } else { value = ""; } getList().put(name, value); pos = cpos + 1; } } /** * Build an encoded features list. * * Encoded features are in the form of:
* name[=value][:name[=value]...] */ public String encode() { StringBuffer sbuf; sbuf = new StringBuffer(); for (String name : getList().keySet()) { String value = getList().get(name); if (sbuf.length() != 0) { sbuf.append(':'); } sbuf.append(name); if (value.length() != 0) { sbuf.append('='); sbuf.append(value); } } return sbuf.toString(); } /** * Get a feature value. * * @return A feature value, or null if not-enabled. */ public String get(String name) { return getList().get(name); } /** * Determine if a feature is enabled. * * @return true is a feature is enabled. */ public boolean has(String name) { return getList().containsKey(name); } /** * Enable/disable a feature. * * @param name * The feature mnemonic. * @param enabled * Flag indicating if enabled. * * @return true if the list changed, * false otherwise. */ public boolean set(String name, boolean enabled) { return set(name, enabled ? "" : null); } /** * Set/remove a feature. NOTE: The names and values MUST NOT contain * = (equals), or : (colon). * * @param name * The feature mnemonic. * @param value * The feature value, or null to disable. * * @return true if the list changed, * false otherwise. */ public boolean set(String name, String value) { if (value != null) { getList().put(name, value); } else { if (getList().remove(name) == null) { return false; } } return true; } public HashMap getList() { return list; } }