001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.vmd.midp.components.general;
043:
044: import java.util.List;
045:
046: /**
047: *
048: * @author Martin Brehovsky, Karol Harezlak
049: */
050: public abstract class Bitmask {
051:
052: private int bitmask;
053:
054: public Bitmask(int bitmask) {
055: this .bitmask = bitmask;
056: }
057:
058: /**
059: * Gets enum all available values
060: * @return
061: */
062: public abstract List<BitmaskItem> getBitmaskItems();
063:
064: public int getBitmask() {
065: return bitmask;
066: }
067:
068: public boolean isSet(BitmaskItem item) {
069: int affectedBits = item.getAffectedBits();
070: return (affectedBits & bitmask) == affectedBits;
071: }
072:
073: public int addToBitmask(BitmaskItem item, boolean value) {
074: if (value) {
075: bitmask |= item.getAffectedBits();
076: } else {
077: bitmask &= ~item.getAffectedBits();
078: }
079: return bitmask;
080: }
081:
082: public int setBitmask(int bitmask) {
083: this .bitmask = bitmask;
084: return this .bitmask;
085: }
086:
087: public boolean equals(Bitmask bitmask) {
088: if (this == bitmask) {
089: return true;
090: } else {
091: // need to check the bitmask value
092: return this .bitmask == bitmask.bitmask;
093: }
094: }
095:
096: @Override
097: public boolean equals(Object obj) {
098: if (obj instanceof Bitmask) {
099: return equals((Bitmask) obj);
100: } else {
101: return false;
102: }
103: }
104:
105: public BitmaskItem getBitmaskItem(int affectedBits) {
106: for (BitmaskItem bitmaskItem : getBitmaskItems()) {
107: if (bitmaskItem.getAffectedBits() == affectedBits)
108: return bitmaskItem;
109: }
110:
111: return null;
112: }
113:
114: public BitmaskItem getBitmaskItem(String displayName) {
115: for (BitmaskItem bitmaskItem : getBitmaskItems()) {
116: if (bitmaskItem.getDisplayName().equals(displayName))
117: return bitmaskItem;
118: }
119:
120: return null;
121: }
122:
123: /**
124: * Individual bit mask item
125: * @author breh
126: * @version
127: */
128: public final static class BitmaskItem {
129:
130: private String name;
131: private String displayName;
132: private int affectedBits;
133:
134: public BitmaskItem(String name, int affectedBits) {
135: this (affectedBits, null, name);
136: }
137:
138: public BitmaskItem(int affectedBits, String displayName,
139: String name) {
140: if (name == null)
141: throw new NullPointerException(
142: "Name parameter cannot be null"); // NOI18N
143: this .name = name;
144: if (displayName != null) {
145: this .displayName = displayName;
146: } else {
147: this .displayName = this .name;
148: }
149: this .affectedBits = affectedBits;
150: }
151:
152: /**
153: * Gets display name of this element (usefull for property editors)
154: * @return
155: */
156: public String getDisplayName() {
157: return displayName;
158: }
159:
160: /**
161: * Gets name of this element (useful for code genrators)
162: * @return
163: */
164: public String getName() {
165: return name;
166: }
167:
168: /**
169: * Gets affectedBits of this individual bitmask item (i.e. which bits are
170: * set when this items set is true)
171: * @return
172: */
173: public int getAffectedBits() {
174: return affectedBits;
175: }
176:
177: }
178:
179: }
|