Gridarta Editor
GameObjectText.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.baseobject;
21 
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
35 public class GameObjectText implements Serializable {
36 
40  private static final long serialVersionUID = 1L;
41 
48  @NotNull
49  private final StringBuilder objectText = new StringBuilder();
50 
56  private final Map<String, String> attributeCache = new HashMap<>();
57 
61  public GameObjectText() {
62  }
63 
68  public GameObjectText(@NotNull final GameObjectText gameObjectText) {
69  objectText.append(gameObjectText.objectText);
70  }
71 
75  private void clearAttributeCache() {
76  attributeCache.clear();
77  }
78 
84  @Nullable
85  public String getAttributeValue(@NotNull final String attributeName) {
86  final String result;
87  if (attributeCache.containsKey(attributeName)) {
88  result = attributeCache.get(attributeName);
89  } else {
90  result = getAttributeValueInt(attributeName);
91  attributeCache.put(attributeName, result);
92  }
93  return result;
94  }
95 
102  @Nullable
103  private String getAttributeValueInt(@NotNull final String attributeName) {
104  final String attr2 = attributeName.trim() + ' ';
105  String result = null;
106  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(objectText.toString(), 0)) {
107  if (line.startsWith(attr2)) {
108  result = line.substring(attr2.length());
109  break;
110  }
111  }
112  return result;
113  }
114 
119  @NotNull
120  public String getObjectText() {
121  return objectText.toString();
122  }
123 
128  public boolean hasObjectText() {
129  return objectText.length() > 0;
130  }
131 
139  public boolean addObjectText(@NotNull final String line, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject) {
140  if (line.isEmpty()) {
141  return false;
142  }
143  baseObject.beginGameObjectChange();
144  try {
145  objectText.append(line);
146  if (!line.endsWith("\n")) {
147  objectText.append('\n');
148  }
149  } finally {
150  baseObject.endGameObjectChange();
151  }
153  return true;
154  }
155 
162  public boolean setObjectText(@NotNull final String objectText, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject) {
163  final String oldObjectText = this.objectText.toString();
164  if (oldObjectText.length() == objectText.length()) {
165  final String oldSortedObjectText = StringUtils.sortLines(oldObjectText);
166  final String newSortedObjectText = StringUtils.sortLines(objectText);
167  if (oldSortedObjectText.equals(newSortedObjectText)) {
168  return false;
169  }
170  }
171 
172  baseObject.beginGameObjectChange();
173  try {
174  this.objectText.setLength(0);
175  this.objectText.append(objectText);
176  if (!objectText.isEmpty() && !objectText.endsWith("\n")) {
177  this.objectText.append('\n');
178  }
179  } finally {
180  baseObject.endGameObjectChange();
181  }
183  return true;
184  }
185 
186  @Override
187  public int hashCode() {
188  return objectText.toString().hashCode();
189  }
190 
191  @Override
192  public boolean equals(@Nullable final Object obj) {
193  if (obj == this) {
194  return true;
195  }
196  if (obj == null || obj.getClass() != getClass()) {
197  return false;
198  }
199  final GameObjectText gameObjectText = (GameObjectText) obj;
200  return gameObjectText.objectText.toString().equals(objectText.toString());
201  }
202 
209  public boolean removeAttribute(@NotNull final String attributeName, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject) {
210  final String attributeNameWithSpace = attributeName.trim() + " "; // attributeName must be followed by space
211 
212  final StringBuilder sb = new StringBuilder();
213  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(objectText.toString(), 0)) {
214  if (!line.isEmpty() && !line.startsWith(attributeNameWithSpace)) {
215  sb.append(line).append('\n');
216  }
217  }
218  return setObjectText(sb.toString(), baseObject);
219  }
220 
230  public boolean setAttributeValue(@NotNull final String attributeName, final boolean sameAsInArchetype, @NotNull final String value, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject) {
231  if (value.isEmpty()) {
232  return removeAttribute(attributeName, baseObject);
233  }
234 
235  final String attributeNameWithSpace = attributeName.trim() + " ";
236  boolean exists = false;
237  final StringBuilder result = new StringBuilder();
238  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(objectText.toString(), 0)) {
239  if (line.isEmpty()) {
240  // skip empty lines that occur due to split on empty object texts.
241  } else if (!line.startsWith(attributeNameWithSpace)) {
242  result.append(line).append('\n');
243  } else {
244  exists = true;
245  if (!sameAsInArchetype) {
246  result.append(attributeNameWithSpace).append(value).append('\n');
247  }
248  }
249  }
250  if (!exists && !sameAsInArchetype) {
251  result.append(attributeNameWithSpace).append(value).append('\n');
252  }
253  return setObjectText(result.toString(), baseObject);
254  }
255 
260  @NotNull
261  public Collection<String> getAttributeKeys() {
262  final Collection<String> keys = new HashSet<>();
263  for (final String line : StringUtils.PATTERN_END_OF_LINE.split(objectText.toString(), 0)) {
264  final int index = line.indexOf(' ');
265  if (index != -1) {
266  keys.add(line.substring(0, index));
267  }
268  }
269  return keys;
270  }
271 
272 }
net.sf.gridarta.model.baseobject.GameObjectText.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: GameObjectText.java:40
net.sf.gridarta.model.baseobject.GameObjectText.hashCode
int hashCode()
Definition: GameObjectText.java:187
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.baseobject.GameObjectText.clearAttributeCache
void clearAttributeCache()
Clears the attribute cache.
Definition: GameObjectText.java:75
net.sf.gridarta.model.baseobject.GameObjectText.GameObjectText
GameObjectText()
Creates a new instance.
Definition: GameObjectText.java:61
net.sf.gridarta.model.baseobject.GameObjectText.attributeCache
final Map< String, String > attributeCache
Map which caches attributes for faster access.
Definition: GameObjectText.java:56
net.sf.gridarta.model.baseobject.GameObjectText.getAttributeValueInt
String getAttributeValueInt(@NotNull final String attributeName)
Returns the requested attribute value from the objectText.
Definition: GameObjectText.java:103
net.sf.gridarta.model.baseobject.GameObjectText
A set of key/value attributes.
Definition: GameObjectText.java:35
net.sf.gridarta.model.baseobject.GameObjectText.getAttributeValue
String getAttributeValue(@NotNull final String attributeName)
Returns an attribute value by attribute name.
Definition: GameObjectText.java:85
net.sf
net.sf.gridarta.model.baseobject.AbstractBaseObject
Default implementation for GameObject implementing classes.
Definition: AbstractBaseObject.java:46
net.sf.gridarta.model.baseobject.GameObjectText.GameObjectText
GameObjectText(@NotNull final GameObjectText gameObjectText)
Creates a new instance as a copy of another instance.
Definition: GameObjectText.java:68
net.sf.gridarta.model.baseobject.GameObjectText.hasObjectText
boolean hasObjectText()
Returns whether an object text is set.
Definition: GameObjectText.java:128
net.sf.gridarta.model.baseobject.GameObjectText.objectText
final StringBuilder objectText
The objectText with the differences from the Archetype.
Definition: GameObjectText.java:49
net
net.sf.gridarta.model.baseobject.GameObjectText.setObjectText
boolean setObjectText(@NotNull final String objectText, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Sets the object text.
Definition: GameObjectText.java:162
net.sf.gridarta.utils.StringUtils.PATTERN_END_OF_LINE
static final Pattern PATTERN_END_OF_LINE
The pattern to match end of line characters separating lines.
Definition: StringUtils.java:61
net.sf.gridarta.model.baseobject.GameObjectText.removeAttribute
boolean removeAttribute(@NotNull final String attributeName, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Removes an attribute.
Definition: GameObjectText.java:209
net.sf.gridarta.model.baseobject.GameObjectText.equals
boolean equals(@Nullable final Object obj)
Definition: GameObjectText.java:192
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.utils.StringUtils.sortLines
static String sortLines(@NotNull final CharSequence string)
Sorts newline separated lines in a string.
Definition: StringUtils.java:197
net.sf.gridarta.model.baseobject.GameObjectText.getAttributeKeys
Collection< String > getAttributeKeys()
Returns all attribute keys of this object.
Definition: GameObjectText.java:261
net.sf.gridarta.model.baseobject.GameObjectText.setAttributeValue
boolean setAttributeValue(@NotNull final String attributeName, final boolean sameAsInArchetype, @NotNull final String value, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Updates an attribute's value.
Definition: GameObjectText.java:230
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.baseobject.GameObjectText.addObjectText
boolean addObjectText(@NotNull final String line, @NotNull final AbstractBaseObject<?, ?, ?, ?> baseObject)
Appends a line to the object text.
Definition: GameObjectText.java:139
net.sf.gridarta.model.baseobject.GameObjectText.getObjectText
String getObjectText()
Returns the object text.
Definition: GameObjectText.java:120