java中位掩码的作用

java中有个类,叫DefaultButtonModel,其中用到了位掩码,至少我理解是位掩码


/**
 * The default implementation of a <code>Button</code> component's data model.
 * <p>
 * <strong>Warning:</strong>
 * Serialized objects of this class will not be compatible with
 * future Swing releases. The current serialization support is
 * appropriate for short term storage or RMI between applications running
 * the same version of Swing. As of 1.4, support for long term storage
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
 * has been added to the <code>java.beans</code> package.
 * Please see {@link java.beans.XMLEncoder}.
 *
 * @version 1.49 05/25/06
 * @author Jeff Dinkins
 */
public class DefaultButtonModel implements ButtonModel, Serializable {

    /** The bitmask used to store the state of the button. */
    protected int stateMask = 0;
    
    /**
     * Constructs a <code>DefaultButtonModel</code>.
     *
     */
    public DefaultButtonModel() {
        stateMask = 0;
        setEnabled(true);
    }
        
    /**
     * Identifies the "armed" bit in the bitmask, which
     * indicates partial commitment towards choosing/triggering
     * the button.
     */
    public final static int ARMED = 1 << 0;
        
    /**
     * Identifies the "selected" bit in the bitmask, which
     * indicates that the button has been selected. Only needed for
     * certain types of buttons - such as radio button or check box.
     */
    public final static int SELECTED = 1 << 1;
        
    /**
     * Identifies the "pressed" bit in the bitmask, which
     * indicates that the button is pressed.
     */
    public final static int PRESSED = 1 << 2;
        
    /**
     * Identifies the "enabled" bit in the bitmask, which
     * indicates that the button can be selected by
     * an input device (such as a mouse pointer).
     */
    public final static int ENABLED = 1 << 3;

    /**
     * Identifies the "rollover" bit in the bitmask, which
     * indicates that the mouse is over the button.
     */
    public final static int ROLLOVER = 1 << 4;
        

    /**
     * {@inheritDoc}
     */
    public boolean isArmed() {
        return (stateMask & ARMED) != 0;
    }
        
    /**
     * {@inheritDoc}
     */
    public boolean isSelected() {
        return (stateMask & SELECTED) != 0;
    }
        
    /**
     * {@inheritDoc}
     */
    public boolean isEnabled() {
        return (stateMask & ENABLED) != 0;
    }
        
    /**
     * {@inheritDoc}
     */
    public boolean isPressed() {
        return (stateMask & PRESSED) != 0;
    }
        
    /**
     * {@inheritDoc}
     */
    public boolean isRollover() {
        return (stateMask & ROLLOVER) != 0;
    }
        
    /**
     * {@inheritDoc}
     */
    public void setArmed(boolean b) {
        if(isMenuItem() && 
                UIManager.getBoolean("MenuItem.disabledAreNavigable")) {
            if ((isArmed() == b)) {
                return;
            }
        } else {
            if ((isArmed() == b) || !isEnabled()) {
                return;
            }
        }
            
        if (b) {
            stateMask |= ARMED;
        } else {
            stateMask &= ~ARMED;
        }
            
        fireStateChanged();
    }

    /**
     * {@inheritDoc}
     */
    public void setEnabled(boolean b) {
        if(isEnabled() == b) {
            return;
        }
            
        if (b) {
            stateMask |= ENABLED;
        } else {
            stateMask &= ~ENABLED;
	    // unarm and unpress, just in case
            stateMask &= ~ARMED;
            stateMask &= ~PRESSED;
        }

            
        fireStateChanged();
    }
        
    /**
     * {@inheritDoc}
     */
    public void setSelected(boolean b) {
        if (this.isSelected() == b) {
            return;
        }

        if (b) {
            stateMask |= SELECTED;
        } else {
            stateMask &= ~SELECTED;
        }

        fireItemStateChanged(
                new ItemEvent(this,
                              ItemEvent.ITEM_STATE_CHANGED,
                              this,
                              b ?  ItemEvent.SELECTED : ItemEvent.DESELECTED));
        
        fireStateChanged();
        
    }
        
        
    /**
     * {@inheritDoc}
     */
    public void setPressed(boolean b) {
        if((isPressed() == b) || !isEnabled()) {
            return;
        }
        
        if (b) {
            stateMask |= PRESSED;
        } else {
            stateMask &= ~PRESSED;
        }

        if(!isPressed() && isArmed()) {
            int modifiers = 0;
            AWTEvent currentEvent = EventQueue.getCurrentEvent();
            if (currentEvent instanceof InputEvent) {
                modifiers = ((InputEvent)currentEvent).getModifiers();
            } else if (currentEvent instanceof ActionEvent) {
                modifiers = ((ActionEvent)currentEvent).getModifiers();
            }

            fireActionPerformed(
                new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
                                getActionCommand(),
                                EventQueue.getMostRecentEventTime(),
                                modifiers));
        }
            
        fireStateChanged();
    }   

    /**
     * {@inheritDoc}
     */
    public void setRollover(boolean b) {
        if((isRollover() == b) || !isEnabled()) {
            return;
        }
        
        if (b) {
            stateMask |= ROLLOVER;
        } else {
            stateMask &= ~ROLLOVER;
        }

        fireStateChanged();
    }
}
其中 armed,selected,enabled,pressed,rollover都是按钮对应的状态,如果用boolean表示的话,则就要使用5个boolean值.而且是每一个按钮就要这么6个值排列组合出来会有2^5种情况,很痛苦jdk源代码里面提供了这么一种方式:用stateMask这个int来存储按钮的状态,就一个int就ok了,就可以代替本来的6个boolean其中 if (b) { stateMask |= ROLLOVER; } else { stateMask &= ~ROLLOVER; } }的意思是:如果b为true,则在状态属性stateMask中加入ROLLOVER这一个状态如果b为false,则在状态属性stateMask中减去ROLLOVER这一个状态所以以后当有对象存在多种状态时,而这多种状态又可用boolean值组合而成时,可以参考jdk的做法
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,自定义权限码(Custom Permission Masks)通常是在使用Java安全模型(Java Security Model)时,为特定操作或权限分配的一种抽象表示。这个模型用于控制代码运行时对系统资源(如文件、网络连接等)的访问权限。 在Java,权限是由一组位(bits)组成的码,每个位代表一种特定的权限级别。例如,`java.lang.RuntimePermission` 类提供了一组预定义的权限,比如 `accessClassInPackage` 和 `setContextClassLoader`,它们都有自己的二进制码。 如果你需要自定义权限,你可以创建一个新的权限类,继承自 `java.security.Permission` 或其子类,比如 `BasicPermission` 或 `RuntimePermission`。然后,你需要为这个新权限定义一个名称和一个码字符串,该字符串通常是一个以`"permissionName=mask"`格式表示的字符串,其`permissionName`是权限的类别名,`mask`是你自定义的二进制码。 例如: ```java public class MyCustomPermission extends BasicPermission { public MyCustomPermission(String name) { super(name); } private static final String MASK = "00000001"; // 假设这是一个自定义的四位码,表示四种不同的权限级别 } // 使用自定义权限 MyCustomPermission permission = new MyCustomPermission("myCustomAction"); System.out.println(permission.getName()); // 输出 "myCustomAction" System.out.println(permission.getActions()); // 输出 "myCustomAction" ``` 在实际应用,可能需要在`Policy`或`PolicyProvider`注册这些自定义权限,并在`SecurityManager`的检查方法处理这些权限的验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值