Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

位掩碼(BitMask)與 EnumSet 的取捨實踐

編輯:關於 Android 程式設計

位運算在實際開發中其實並不常見,原因很簡單:它對不熟悉的人來說既難讀、又難懂、更難手算,若沒有經常動手實踐,很快就會生疏。但它的優點也很鮮明——計算快、代碼精簡。在某些特定場景下,這種優勢會被放大得非常明顯,例如下面這段權限管理的代碼:

 public class NewPermission { // 是否允許查詢,二進制第1位,0表示否,1表示是 public static final int ALLOW_SELECT = 1 << 0; // 0001 // 是否允許新增,二進制第2位,0表示否,1表示是 public static final int ALLOW_INSERT = 1 << 1; // 0010 // 是否允許修改,二進制第3位,0表示否,1表示是 public static final int ALLOW_UPDATE = 1 << 2; // 0100 // 是否允許刪除,二進制第4位,0表示否,1表示是 public static final int ALLOW_DELETE = 1 << 3; // 1000 // 存儲目前的權限狀態 private int flag; /** * 重新設置權限 */ public void setPermission(int permission) { flag = permission; } /** * 添加一項或多項權限 */ public void enable(int permission) { flag |= permission; } /** * 刪除一項或多項權限 */ public void disable(int permission) { flag &= ~permission; } /** * 是否擁某些權限 */ public boolean isAllow(int permission) { return (flag & permission) == permission; } /** * 是否禁用了某些權限 */ public boolean isNotAllow(int permission) { return (flag & permission) == 0; } /** * 是否僅僅擁有某些權限 */ public boolean isOnlyAllow(int permission) { return flag == permission; } }

從業務角度看,權限本來就可以有很多種,用傳統方式來實現上面這些功能其實也不會太麻煩。但這種「取巧」的位運算寫法,在 Android 源碼裡可謂隨處可見,最典型的就是 EditText 的 InputType:

 /* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.text; import android.text.TextUtils; /** * Bit definitions for an integer defining the basic content type of text * held in an {@link Editable} object. Supported classes may be combined * with variations and flags to indicate desired behaviors. */ public interface InputType { public static final int TYPE_MASK_CLASS = 0x0000000f; public static final int TYPE_MASK_VARIATION = 0x00000ff0; public static final int TYPE_MASK_FLAGS = 0x00fff000; public static final int TYPE_NULL = 0x00000000; public static final int TYPE_CLASS_TEXT = 0x00000001; public static final int TYPE_TEXT_FLAG_CAP_CHARACTERS = 0x00001000; public static final int TYPE_TEXT_FLAG_CAP_WORDS = 0x00002000; public static final int TYPE_TEXT_FLAG_CAP_SENTENCES = 0x00004000; public static final int TYPE_TEXT_FLAG_AUTO_CORRECT = 0x00008000; public static final int TYPE_TEXT_FLAG_AUTO_COMPLETE = 0x00010000; public static final int TYPE_TEXT_FLAG_MULTI_LINE = 0x00020000; public static final int TYPE_TEXT_FLAG_IME_MULTI_LINE = 0x00040000; public static final int TYPE_TEXT_FLAG_NO_SUGGESTIONS = 0x00080000; public static final int TYPE_TEXT_VARIATION_NORMAL = 0x00000000; public static final int TYPE_TEXT_VARIATION_URI = 0x00000010; public static final int TYPE_TEXT_VARIATION_EMAIL_ADDRESS = 0x00000020; public static final int TYPE_TEXT_VARIATION_PASSWORD = 0x00000080; public static final int TYPE_CLASS_NUMBER = 0x00000002; public static final int TYPE_NUMBER_FLAG_SIGNED = 0x00001000; public static final int TYPE_NUMBER_FLAG_DECIMAL = 0x00002000; public static final int TYPE_CLASS_PHONE = 0x00000003; public static final int TYPE_CLASS_DATETIME = 0x00000004; public static final int TYPE_DATETIME_VARIATION_NORMAL = 0x00000000; public static final int TYPE_DATETIME_VARIATION_DATE = 0x00000010; public static final int TYPE_DATETIME_VARIATION_TIME = 0x00000020; }

不過,位掩碼有一個相當棘手的問題——類型安全。一旦對類型安全把控不到位,後果可能不堪設想。正因為如此,人們引入了枚舉來約束參數類型。Android 源碼裡用的是 @IntDef 註解;而作為另一種更現代的實現,EnumSet 在可讀性、性能以及類型安全等各個維度上,幾乎都不遜於甚至優於位掩碼。下面用 EnumSet 重寫一遍前面的權限邏輯:

 public class NewPermission2 { private EnumSet permissions = EnumSet.noneOf(Permission.class); private enum Permission { SELECT, INSERT, UPDATE, DELETE; public static final EnumSet ALL_PERMISSIONS = EnumSet.allOf(Permission.class); public static final EnumSet VIP_PERMISSIONS = EnumSet.range(Permission.SELECT, Permission.UPDATE); } /** * 重新設置權限 */ public void setPermission(EnumSet s) { this.permissions.clear(); this.permissions = s; } /** * 刪除一項或多項權限 */ public void disable(Permission permission) { permissions.remove(permission); } /** * 是否擁某些權限 */ public boolean isAllow(Permission s) { return permissions.contains(s); } /** * 是否是游客 */ public boolean isVip(Permission s) { return Permission.VIP_PERMISSIONS.contains(s); } }

EnumSet 的具體實現可以翻閱源碼。個人認為,這種方式最大的優點在於「可控」:無論是對業務邏輯的理解,還是代碼實現的便捷程度,各個方面都要比位掩碼這種取巧做法更值得推薦,也更接近官方的正規寫法。當然,如果從 Android 的性能角度權衡,仍有兩點值得留意:

1、內部存在個別反射的身影;

2、EnumSet 本質上是一個實現 Set 接口的集合類。

基於這兩點,在設計用到 EnumSet 的類時,還是需要審慎一些——儘管官方已經給出了相當可靠的性能保證。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved