20 package net.sf.gridarta.model.archetypetype;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
30 import org.apache.log4j.Category;
31 import org.apache.log4j.Logger;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
60 private final List<String>
bitName =
new ArrayList<>();
72 private final Map<String, Integer>
namedValues =
new HashMap<>();
78 private final Map<Integer, String>
encodings =
new HashMap<>();
84 private final Map<Integer, String>
names =
new HashMap<>();
122 return Integer.toString(value);
125 final StringBuilder sb =
new StringBuilder();
127 if (sb.length() > 0) {
132 return sb.toString();
146 private Iterable<String>
encodeValueAsList(
final int value, @NotNull
final Map<Integer, String> strings) {
147 final Collection<String> result =
new ArrayList<>();
151 final Collection<String> negResult =
new ArrayList<>();
152 negResult.add(
"all");
154 if (value == moveAll) {
158 final String str = strings.get(0);
159 result.add(str == null ?
"0" : str);
169 for (
int i = moveAll; i != 0; i >>= 1) {
170 final String strNull = strings.get(1 << count);
171 final String str = strNull == null ? Integer.toString(1 << count) : strNull;
172 if ((value & (1 << count)) == 0) {
173 negResult.add(
"-" + str);
180 if ((value & ~moveAll) != 0) {
181 result.add(Integer.toString(value & ~moveAll));
189 return count >= 4 && allCount <= 1 ? negResult : result;
202 if (encodedValue.isEmpty()) {
208 final boolean negated;
210 if (word.startsWith(
"-")) {
212 name = word.substring(1);
217 final Integer integerValue = namedValues.get(name);
219 if (integerValue != null) {
220 value = integerValue;
223 value = Integer.parseInt(name);
224 }
catch (
final NumberFormatException ignored) {
225 LOG.warn(
"Ignoring unknown bitmask value: " + name);
243 return (1 << bitName.size()) - 1;
251 return bitName.size();
261 return bitName.get(index);
269 public void addName(@NotNull
final String name,
final int value) {
270 names.put(value, name);
279 namedValues.put(name, value);
280 encodings.put(value, name);
289 return encodings.containsKey(value);
298 public boolean addBitName(
final int bitValue, @NotNull
final String name) {
299 if (bitValue >= bitName.size()) {
302 }
while (bitValue >= bitName.size());
303 }
else if (bitName.get(bitValue) != null) {
307 bitName.set(bitValue, name);
Utility class for string manipulation.
static final int MAX_CHARS_PER_LINE
Maximum number of characters in a line before line break (see getText(int)).
boolean addBitName(final int bitValue, @NotNull final String name)
Defines a bit name for a bit value.
String getBitName(final int index)
Returns the name of a bitmask value.
Utility class for parsing strings into numbers.
static final Pattern PATTERN_SPACES
The pattern that matches a non-empty sequence of spaces.
final List< String > bitName
The names of the bitmask entries.
int getMaxValue()
Returns the maximum allowed bitmask value.
Base package of all Gridarta classes.
void addName(@NotNull final String name, final int value)
Adds a readable name for a bit value.
String encodeValue(final int value)
Convert a value to external representation.
void append(final String str)
Append a word.
int getNumber()
Returns the number of bitmask entries (not counting zero).
AttributeBitmask(final boolean isNamed)
Constructor of a bitmask from XML element.
static int parseInt(@NotNull final String s)
Parses an integer string.
Iterable< String > encodeValueAsList(final int value, @NotNull final Map< Integer, String > strings)
Convert a value to string representation.
This class manages bitmask values which appear in Gridarta archetype attributes.
final Map< String, Integer > namedValues
Maps names for value encoding in external representation.
int decodeValue(@NotNull final String encodedValue)
Convert a value from external representation.
final boolean isNamed
Set if the bitmask value may be encoded as strings in external representation; unset if the value is ...
void addNamedValue(@NotNull final String name, final int value)
Adds a name for external encoding of a value.
String getText(final int value)
Generate the text to be displayed for a given bitmask value.
static final Category LOG
The Logger for printing log messages.
String toString()
Return the concatenated words as a string.
final Map< Integer, String > encodings
Maps bit value to external representation.
final Map< Integer, String > names
Maps bit value to readable name.
boolean containsEncoding(final int value)
Returns whether an external encoding for value exists.
Implements a string buffer that separates words by "," and wraps lines at a given margin...