| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2007, 2009 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.pde.api.tools.internal.provisional; |
| 12 | |
| 13 | import org.eclipse.pde.api.tools.internal.util.Util; |
| 14 | |
| 15 | /** |
| 16 | * Class containing constants and utility methods for restriction modifiers |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | */ |
| 20 | public final class RestrictionModifiers { |
| 21 | |
| 22 | /** |
| 23 | * Restriction kind constant indicating there are no restrictions on a type |
| 24 | */ |
| 25 | public static final int NO_RESTRICTIONS = 0x0000; |
| 26 | /** |
| 27 | * Restriction kind constant indicating an interface cannot be implemented |
| 28 | */ |
| 29 | public static final int NO_IMPLEMENT = 0x0001; |
| 30 | /** |
| 31 | * Restriction kind constant indicating a type or member cannot be extended. |
| 32 | */ |
| 33 | public static final int NO_EXTEND = 0x0002; |
| 34 | /** |
| 35 | * Restriction kind constant indicating a class cannot be instantiated. |
| 36 | */ |
| 37 | public static final int NO_INSTANTIATE = 0x0004; |
| 38 | /** |
| 39 | * Restriction kind constant indicating a class cannot have a member referenced |
| 40 | */ |
| 41 | public static final int NO_REFERENCE = 0x0008; |
| 42 | /** |
| 43 | * Restriction kind constant indicating a method cannot be overridden |
| 44 | */ |
| 45 | public static final int NO_OVERRIDE = 0x0010; |
| 46 | |
| 47 | /** |
| 48 | * Bit mask of all restrictions. |
| 49 | */ |
| 50 | public static final int ALL_RESTRICTIONS = 0xFF; |
| 51 | |
| 52 | /** |
| 53 | * Constructor |
| 54 | * no instantiating |
| 55 | */ |
| 56 | private RestrictionModifiers() {} |
| 57 | |
| 58 | /** |
| 59 | * Returns if the no_implement modifier has been set in the specified modifiers |
| 60 | * @param modifiers the modifiers to resolve |
| 61 | * @return if the no_implement modifier has been set in the specified modifiers |
| 62 | */ |
| 63 | public static final boolean isImplementRestriction(int modifiers) { |
| 64 | return (modifiers & NO_IMPLEMENT) > 0; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns if the no_extend modifier has been set in the specified modifiers |
| 69 | * @param modifiers the modifiers to resolve |
| 70 | * @return if the no_extend modifier has been set in the specified modifiers |
| 71 | */ |
| 72 | public static final boolean isExtendRestriction(int modifiers) { |
| 73 | return (modifiers & NO_EXTEND) > 0; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns if the no_override modifier has been set in the specified modifiers |
| 78 | * @param modifiers the modifiers to resolve |
| 79 | * @return if the no_override modifier has been set in the specified modifiers |
| 80 | */ |
| 81 | public static final boolean isOverrideRestriction(int modifiers) { |
| 82 | return (modifiers & NO_OVERRIDE) > 0; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns if the no_instantiate modifier has been set in the specified modifiers |
| 87 | * @param modifiers the modifiers to resolve |
| 88 | * @return if the no_instantiate modifier has been set in the specified modifiers |
| 89 | */ |
| 90 | public static final boolean isInstantiateRestriction(int modifiers) { |
| 91 | return (modifiers & NO_INSTANTIATE) > 0; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns if the no_reference modifier has been set in the specified modifiers |
| 96 | * @param modifiers the modifiers to resolve |
| 97 | * @return if the no_reference modifier has been set in the specified modifiers |
| 98 | */ |
| 99 | public static final boolean isReferenceRestriction(int modifiers) { |
| 100 | return (modifiers & NO_REFERENCE) > 0; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns if the modifiers indicate no restrictions. |
| 105 | * @param modifiers the modifiers to test |
| 106 | * @return if the modifiers indicate no restrictions |
| 107 | */ |
| 108 | public static final boolean isUnrestricted(int modifiers) { |
| 109 | return modifiers == NO_RESTRICTIONS; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the string representation of the specified restriction(s) or <code>UNKNOWN_KIND</code> |
| 114 | * if the kind is unknown. |
| 115 | * |
| 116 | * @param restrictions the restrictions to get the display string for |
| 117 | * @return the string representation for the given restrictions or <code>UNKNOWN_KIND</code> |
| 118 | * @since 1.0.1 |
| 119 | */ |
| 120 | public static String getRestrictionText(int restrictions) { |
| 121 | StringBuffer buffer = new StringBuffer(); |
| 122 | if(restrictions == NO_RESTRICTIONS) { |
| 123 | return "NO_RESTRICTIONS"; //$NON-NLS-1$ |
| 124 | } |
| 125 | if(restrictions == ALL_RESTRICTIONS) { |
| 126 | buffer.append("ALL_RESTRICTIONS"); //$NON-NLS-1$ |
| 127 | } |
| 128 | else { |
| 129 | if((restrictions & NO_EXTEND) > 0) { |
| 130 | buffer.append("NO_EXTEND"); //$NON-NLS-1$ |
| 131 | } |
| 132 | if((restrictions & NO_IMPLEMENT) > 0) { |
| 133 | if(buffer.length() > 0) { |
| 134 | buffer.append(" | "); //$NON-NLS-1$ |
| 135 | } |
| 136 | buffer.append("NO_IMPLEMENT"); //$NON-NLS-1$ |
| 137 | } |
| 138 | if((restrictions & NO_INSTANTIATE) > 0) { |
| 139 | if(buffer.length() > 0) { |
| 140 | buffer.append(" | "); //$NON-NLS-1$ |
| 141 | } |
| 142 | buffer.append("NO_INSTANTIATE"); //$NON-NLS-1$ |
| 143 | } |
| 144 | if((restrictions & NO_REFERENCE) > 0) { |
| 145 | if(buffer.length() > 0) { |
| 146 | buffer.append(" | "); //$NON-NLS-1$ |
| 147 | } |
| 148 | buffer.append("NO_REFERENCE"); //$NON-NLS-1$ |
| 149 | } |
| 150 | if((restrictions & NO_RESTRICTIONS) > 0) { |
| 151 | if(buffer.length() > 0) { |
| 152 | buffer.append(" | "); //$NON-NLS-1$ |
| 153 | } |
| 154 | buffer.append("NO_RESTRICTIONS"); //$NON-NLS-1$ |
| 155 | } |
| 156 | if((restrictions & NO_OVERRIDE) > 0) { |
| 157 | if(buffer.length() > 0) { |
| 158 | buffer.append(" | "); //$NON-NLS-1$ |
| 159 | } |
| 160 | buffer.append("NO_OVERRIDE"); //$NON-NLS-1$ |
| 161 | } |
| 162 | } |
| 163 | if(buffer.length() == 0) { |
| 164 | return Util.UNKNOWN_KIND; |
| 165 | } |
| 166 | return buffer.toString(); |
| 167 | } |
| 168 | } |