20 package net.sf.gridarta.model.baseobject;
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
49 private final StringBuilder
objectText =
new StringBuilder();
69 objectText.append(gameObjectText.objectText);
76 attributeCache.clear();
87 if (attributeCache.containsKey(attributeName)) {
88 result = attributeCache.get(attributeName);
91 attributeCache.put(attributeName, result);
104 final String attr2 = attributeName.trim() +
' ';
105 String result = null;
107 if (line.startsWith(attr2)) {
108 result = line.substring(attr2.length());
121 return objectText.toString();
129 return objectText.length() > 0;
140 if (line.isEmpty()) {
143 baseObject.beginGameObjectChange();
145 objectText.append(line);
146 if (!line.endsWith(
"\n")) {
147 objectText.append(
'\n');
150 baseObject.endGameObjectChange();
163 final String oldObjectText = this.objectText.toString();
164 if (oldObjectText.length() == objectText.length()) {
167 if (oldSortedObjectText.equals(newSortedObjectText)) {
172 baseObject.beginGameObjectChange();
174 this.objectText.setLength(0);
175 this.objectText.append(objectText);
176 if (!objectText.isEmpty() && !objectText.endsWith(
"\n")) {
177 this.objectText.append(
'\n');
180 baseObject.endGameObjectChange();
188 return objectText.toString().hashCode();
192 public boolean equals(@Nullable
final Object obj) {
196 if (obj == null || obj.getClass() != getClass()) {
200 return gameObjectText.
objectText.toString().equals(objectText.toString());
210 final String attributeNameWithSpace = attributeName.trim() +
" ";
212 final StringBuilder sb =
new StringBuilder();
214 if (!line.isEmpty() && !line.startsWith(attributeNameWithSpace)) {
215 sb.append(line).append(
'\n');
231 if (value.isEmpty()) {
235 final String attributeNameWithSpace = attributeName.trim() +
" ";
236 boolean exists =
false;
237 final StringBuilder result =
new StringBuilder();
239 if (line.isEmpty()) {
241 }
else if (!line.startsWith(attributeNameWithSpace)) {
242 result.append(line).append(
'\n');
245 if (!sameAsInArchetype) {
246 result.append(attributeNameWithSpace).append(value).append(
'\n');
250 if (!exists && !sameAsInArchetype) {
251 result.append(attributeNameWithSpace).append(value).append(
'\n');
262 final Collection<String> keys =
new HashSet<>();
264 final int index = line.indexOf(
' ');
266 keys.add(line.substring(0, index));
String getObjectText()
Returns the object text.
String getAttributeValue(@NotNull final String attributeName)
Returns an attribute value by attribute name.
Utility class for string manipulation.
Collection< String > getAttributeKeys()
Returns all attribute keys of this object.
boolean removeAttribute(@NotNull final String attributeName, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Removes an attribute.
final Map< String, String > attributeCache
Map which caches attributes for faster access.
boolean addObjectText(@NotNull final String line, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Appends a line to the object text.
final StringBuilder objectText
The objectText with the differences from the Archetype.
boolean setObjectText(@NotNull final String objectText, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Sets the object text.
boolean setAttributeValue(@NotNull final String attributeName, final boolean sameAsInArchetype, @NotNull final String value, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Updates an attribute's value.
Base package of all Gridarta classes.
String getAttributeValueInt(@NotNull final String attributeName)
Returns the requested attribute value from the objectText.
boolean equals(@Nullable final Object obj)
boolean hasObjectText()
Returns whether an object text is set.
static final Pattern PATTERN_END_OF_LINE
The pattern to match end of line characters separating lines.
Default implementation for GameObject implementing classes.
GameObjectText(@NotNull final GameObjectText gameObjectText)
Creates a new instance as a copy of another instance.
GameObjectText()
Creates a new instance.
A set of key/value attributes.
void clearAttributeCache()
Clears the attribute cache.
static String sortLines(@NotNull final CharSequence string)
Sorts newline separated lines in a string.
static final long serialVersionUID
The serial version UID.