Gridarta Editor
ArchetypeTypeSet.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 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.archetypetype;
21 
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.TreeMap;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
40 public class ArchetypeTypeSet implements Iterable<ArchetypeType> {
41 
46  @NotNull
47  private ArchetypeType fallbackArchetypeType = new ArchetypeType("", 0, "", true, null, false, null, null, new ArchetypeAttributesDefinition());
48 
53  @NotNull
54  private final List<ArchetypeType> archetypeTypeList = new ArrayList<>();
55 
60  @NotNull
61  private final Map<String, ArchetypeType> archetypeTypeNames = new HashMap<>();
62 
67  @NotNull
68  private final Map<String, ArchetypeTypeList> listTable = new HashMap<>();
69 
74  @NotNull
75  private final Map<String, AttributeBitmask> bitmaskTable = new HashMap<>();
76 
80  @NotNull
81  private final Map<String, Integer> attributeOrder = new LinkedHashMap<>();
82 
86  @NotNull
87  private final Comparator<String> attributeOrderComparator = new Comparator<String>() {
88 
89  @Override
90  public int compare(@NotNull final String o1, @NotNull final String o2) {
91  final Integer index1 = attributeOrder.get(o1);
92  final Integer index2 = attributeOrder.get(o2);
93  if (index1 == null) {
94  if (index2 == null) {
95  return o1.compareTo(o2);
96  }
97  return +1;
98  }
99  if (index2 == null) {
100  return -1;
101  }
102  if (index1 < index2) {
103  return -1;
104  }
105  if (index1 > index2) {
106  return +1;
107  }
108  return 0;
109  }
110 
111  };
112 
117  public void addArchetypeType(@NotNull final ArchetypeType archetypeType) {
118  archetypeTypeList.add(archetypeType);
119  final String typeName = archetypeType.getTypeName();
120  archetypeTypeNames.put(typeName, archetypeType);
121  if (typeName.equals("Misc")) {
122  fallbackArchetypeType = archetypeType;
123  }
124  }
125 
132  @Nullable
133  public ArchetypeType getArchetypeType(@NotNull final String typeName) {
134  return archetypeTypeNames.get(typeName);
135  }
136 
146  @NotNull
147  public ArchetypeType getArchetypeTypeByName(@NotNull final String typeName) {
148  final ArchetypeType type = archetypeTypeNames.get(typeName.trim());
149  if (type != null) {
150  return type;
151  }
152 
153  return fallbackArchetypeType;
154  }
155 
162  @NotNull
164  for (final ArchetypeType archetypeType : archetypeTypeList) {
165  if (archetypeType.matches(baseObject)) {
166  return archetypeType;
167  }
168  }
169 
170  return fallbackArchetypeType;
171  }
172 
173  @NotNull
174  @Override
175  public Iterator<ArchetypeType> iterator() {
176  return Collections.unmodifiableList(archetypeTypeList).iterator();
177  }
178 
184  public int getArchetypeTypeIndex(@NotNull final ArchetypeType archetypeType) {
185  return archetypeTypeList.indexOf(archetypeType);
186  }
187 
193  public int getArchetypeTypeCount() {
194  return archetypeTypeList.size();
195  }
196 
203  public boolean isFallbackArchetypeType(@NotNull final ArchetypeType archetypeType) {
204  return archetypeType == fallbackArchetypeType;
205  }
206 
212  public void addList(@NotNull final String attribute, @NotNull final ArchetypeTypeList list) {
213  listTable.put(attribute, list);
214  }
215 
222  @Nullable
223  public ArchetypeTypeList getList(@NotNull final String listName) {
224  return listTable.get(listName);
225  }
226 
232  public void addBitmask(@NotNull final String attribute, @NotNull final AttributeBitmask attributeBitmask) {
233  bitmaskTable.put(attribute, attributeBitmask);
234  }
235 
241  @Nullable
242  public AttributeBitmask getBitmask(@NotNull final String bitmaskName) {
243  return bitmaskTable.get(bitmaskName);
244  }
245 
254  public boolean addAttributeOrder(@NotNull final String attributeName) {
255  if (attributeOrder.containsKey(attributeName)) {
256  return true;
257  }
258 
259  attributeOrder.put(attributeName, attributeOrder.size());
260  return false;
261  }
262 
268  @NotNull
269  public Comparator<String> getAttributeOrderComparator() {
271  }
272 
278  @NotNull
279  public String getDisplayName(@NotNull final BaseObject<?, ?, ?, ?> baseObject) {
280  return getArchetypeTypeByBaseObject(baseObject).getDisplayName(baseObject);
281  }
282 
283  @NotNull
284  @Override
285  public String toString() {
286  final StringBuilder sb = new StringBuilder();
287  final Map<String, ArchetypeType> sortedArchetypeTypes = new TreeMap<>(archetypeTypeNames);
288  for (final ArchetypeType archetypeType : sortedArchetypeTypes.values()) {
289  archetypeType.toString(sb);
290  }
291  return sb.toString();
292  }
293 
294 }
Comparator< String > getAttributeOrderComparator()
Returns a Comparator that sorts attributes keys by the order defined by addAttributeOrder(String).
Manages ArchetypeType instances, list, and bitmask definitions.
final Map< String, ArchetypeTypeList > listTable
Table with List objects for lists (value) accessible by name (key).
String getDisplayName(@NotNull final Attributes baseObject)
Returns a description of this type.
ArchetypeType getArchetypeTypeByBaseObject(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Returns the ArchetypeType for the given BaseObject.
Contains the data of one Gridarta Object-Type.
final Map< String, ArchetypeType > archetypeTypeNames
Table with type archetype type name as keys (String), and ArchetypeType object as values (...
boolean isFallbackArchetypeType(@NotNull final ArchetypeType archetypeType)
Returns whether a given ArchetypeType is the fallback archetype type used for game objects not matchi...
int getArchetypeTypeIndex(@NotNull final ArchetypeType archetypeType)
Returns the index of an ArchetypeType instance.
AttributeBitmask getBitmask(@NotNull final String bitmaskName)
Returns a bitmask type by name.
void addArchetypeType(@NotNull final ArchetypeType archetypeType)
Adds an ArchetypeType instance.
void addList(@NotNull final String attribute, @NotNull final ArchetypeTypeList list)
Adds a list definition.
boolean addAttributeOrder(@NotNull final String attributeName)
Adds an attribute name for attribute ordering when saving.
Base package of all Gridarta classes.
This class manages bitmask values which appear in Gridarta archetype attributes.
final Map< String, AttributeBitmask > bitmaskTable
Table with AttributeBitmask objects (value) accessible by name (key).
void addBitmask(@NotNull final String attribute, @NotNull final AttributeBitmask attributeBitmask)
Adds a bitmask definition.
int getArchetypeTypeCount()
Returns the number of ArchetypeTypes in the list.
ArchetypeType fallbackArchetypeType
The default (fallback) ArchetypeType used for game objects not matching any defined type...
Pair< Integer, String > get(final int index)
Returns an entry by index.
final List< ArchetypeType > archetypeTypeList
Lists with all defined ArchetypeTypes.
ArchetypeType getArchetypeType(@NotNull final String typeName)
Returns an ArchetypeType by type name.
String getDisplayName(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Returns a description of this type.
ArchetypeTypeList getList(@NotNull final String listName)
Returns a list type definition.
final Map< String, Integer > attributeOrder
Attribute names in save order.
final Comparator< String > attributeOrderComparator
A Comparator that compares strings by attributeOrder.
ArchetypeType getArchetypeTypeByName(@NotNull final String typeName)
Finds and returns the type-structure (.