com.sfs.beans.FunctionalityBean.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.beans.FunctionalityBean.java

Source

/*******************************************************************************
 * Copyright (c) 2010 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.beans;

import java.util.HashMap;
import java.util.Map;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

/**
 * The Class FunctionalityBean.
 *
 * @author David Harrison
 */
public class FunctionalityBean extends Object implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The actions. */
    private Map<String, Boolean> functionality = new HashMap<String, Boolean>();

    /**
     * Sets the functionality map.
     *
     * @param functionalityMap the functionality map
     */
    public final void setFunctionalityMap(final Map<String, Boolean> functionalityMap) {
        this.functionality = new HashMap<String, Boolean>();
        if (functionalityMap != null) {
            for (String function : functionalityMap.keySet()) {
                final boolean value = functionalityMap.get(function);
                this.functionality.put(function.toLowerCase(), value);
            }
        }
    }

    /**
     * Adds the functionality switch (on/off).
     *
     * @param name the name of the piece of functionality
     * @param enabled the flag for enabled or disabled
     */
    public final void addFunctionality(final String name, final boolean enabled) {
        if (this.functionality == null) {
            this.functionality = new HashMap<String, Boolean>();
        }
        if (StringUtils.isNotBlank(name)) {
            this.functionality.put(name.toLowerCase(), enabled);
        }
    }

    /**
     * Checks whether the functionality is enabled (default = true).
     *
     * @param function the piece of functionality
     * @return the enabled flag
     */
    public final boolean isEnabled(final String function) {
        return this.getFunctionality(function);
    }

    /**
     * Checks whether the functionality is disabled (default = false).
     *
     * @param function the piece of functionality
     * @return the enabled flag
     */
    public final boolean isDisabled(final String function) {
        boolean disabled = false;

        if (!this.getFunctionality(function)) {
            disabled = true;
        }
        return disabled;
    }

    /**
     * Gets the functionality flag.
     *
     * @param function the functionality
     * @return the functionality
     */
    public final boolean getFunctionality(final String function) {
        boolean enabled = true;
        if (this.functionality != null) {
            if (this.functionality.containsKey(function.toLowerCase())) {
                enabled = this.functionality.get(function.toLowerCase());
            }
        }
        return enabled;
    }
}