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  return index2 == null ? o1.compareTo(o2) : +1;
95  }
96  return index2 == null ? -1 : index1.compareTo(index2);
97  }
98 
99  };
100 
105  public void addArchetypeType(@NotNull final ArchetypeType archetypeType) {
106  archetypeTypeList.add(archetypeType);
107  final String typeName = archetypeType.getTypeName();
108  archetypeTypeNames.put(typeName, archetypeType);
109  if (typeName.equals("Misc")) {
110  fallbackArchetypeType = archetypeType;
111  }
112  }
113 
120  @Nullable
121  public ArchetypeType getArchetypeType(@NotNull final String typeName) {
122  return archetypeTypeNames.get(typeName);
123  }
124 
134  @NotNull
135  public ArchetypeType getArchetypeTypeByName(@NotNull final String typeName) {
136  final ArchetypeType type = archetypeTypeNames.get(typeName.trim());
137  return type == null ? fallbackArchetypeType : type;
138  }
139 
146  @NotNull
148  for (final ArchetypeType archetypeType : archetypeTypeList) {
149  if (archetypeType.matches(baseObject)) {
150  return archetypeType;
151  }
152  }
153 
154  return fallbackArchetypeType;
155  }
156 
157  @NotNull
158  @Override
159  public Iterator<ArchetypeType> iterator() {
160  return Collections.unmodifiableList(archetypeTypeList).iterator();
161  }
162 
168  public int getArchetypeTypeIndex(@NotNull final ArchetypeType archetypeType) {
169  return archetypeTypeList.indexOf(archetypeType);
170  }
171 
177  public int getArchetypeTypeCount() {
178  return archetypeTypeList.size();
179  }
180 
187  public boolean isFallbackArchetypeType(@NotNull final ArchetypeType archetypeType) {
188  return archetypeType == fallbackArchetypeType;
189  }
190 
196  public void addList(@NotNull final String attribute, @NotNull final ArchetypeTypeList list) {
197  listTable.put(attribute, list);
198  }
199 
206  @Nullable
207  public ArchetypeTypeList getList(@NotNull final String listName) {
208  return listTable.get(listName);
209  }
210 
216  public void addBitmask(@NotNull final String attribute, @NotNull final AttributeBitmask attributeBitmask) {
217  bitmaskTable.put(attribute, attributeBitmask);
218  }
219 
225  @Nullable
226  public AttributeBitmask getBitmask(@NotNull final String bitmaskName) {
227  return bitmaskTable.get(bitmaskName);
228  }
229 
238  public boolean addAttributeOrder(@NotNull final String attributeName) {
239  if (attributeOrder.containsKey(attributeName)) {
240  return true;
241  }
242 
243  attributeOrder.put(attributeName, attributeOrder.size());
244  return false;
245  }
246 
252  @NotNull
253  public Comparator<String> getAttributeOrderComparator() {
255  }
256 
262  @NotNull
263  public String getDisplayName(@NotNull final BaseObject<?, ?, ?, ?> baseObject) {
264  return getArchetypeTypeByBaseObject(baseObject).getDisplayName(baseObject);
265  }
266 
267  @NotNull
268  @Override
269  public String toString() {
270  final StringBuilder sb = new StringBuilder();
271  final Map<String, ArchetypeType> sortedArchetypeTypes = new TreeMap<>(archetypeTypeNames);
272  for (final ArchetypeType archetypeType : sortedArchetypeTypes.values()) {
273  archetypeType.toString(sb);
274  }
275  return sb.toString();
276  }
277 
278 }
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getList
ArchetypeTypeList getList(@NotNull final String listName)
Definition: ArchetypeTypeSet.java:207
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.addBitmask
void addBitmask(@NotNull final String attribute, @NotNull final AttributeBitmask attributeBitmask)
Definition: ArchetypeTypeSet.java:216
net.sf.gridarta
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.archetypeTypeNames
final Map< String, ArchetypeType > archetypeTypeNames
Definition: ArchetypeTypeSet.java:61
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getDisplayName
String getDisplayName(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Definition: ArchetypeTypeSet.java:263
net.sf
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getArchetypeTypeIndex
int getArchetypeTypeIndex(@NotNull final ArchetypeType archetypeType)
Definition: ArchetypeTypeSet.java:168
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getArchetypeType
ArchetypeType getArchetypeType(@NotNull final String typeName)
Definition: ArchetypeTypeSet.java:121
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.addList
void addList(@NotNull final String attribute, @NotNull final ArchetypeTypeList list)
Definition: ArchetypeTypeSet.java:196
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet
Definition: ArchetypeTypeSet.java:40
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getAttributeOrderComparator
Comparator< String > getAttributeOrderComparator()
Definition: ArchetypeTypeSet.java:253
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.toString
String toString()
Definition: ArchetypeTypeSet.java:269
net.sf.gridarta.model.archetypetype.ArchetypeTypeList
Definition: ArchetypeTypeList.java:33
net
net.sf.gridarta.model.archetypetype.AttributeBitmask
Definition: AttributeBitmask.java:42
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getArchetypeTypeByBaseObject
ArchetypeType getArchetypeTypeByBaseObject(@NotNull final BaseObject<?, ?, ?, ?> baseObject)
Definition: ArchetypeTypeSet.java:147
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.iterator
Iterator< ArchetypeType > iterator()
Definition: ArchetypeTypeSet.java:159
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.bitmaskTable
final Map< String, AttributeBitmask > bitmaskTable
Definition: ArchetypeTypeSet.java:75
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getArchetypeTypeByName
ArchetypeType getArchetypeTypeByName(@NotNull final String typeName)
Definition: ArchetypeTypeSet.java:135
net.sf.gridarta.model.archetypetype.ArchetypeAttributesDefinition
Definition: ArchetypeAttributesDefinition.java:34
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.fallbackArchetypeType
ArchetypeType fallbackArchetypeType
Definition: ArchetypeTypeSet.java:47
net.sf.gridarta.model.archetypetype.ArchetypeType.getDisplayName
String getDisplayName(@NotNull final Attributes baseObject)
Definition: ArchetypeType.java:279
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.attributeOrderComparator
final Comparator< String > attributeOrderComparator
Definition: ArchetypeTypeSet.java:87
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.addAttributeOrder
boolean addAttributeOrder(@NotNull final String attributeName)
Definition: ArchetypeTypeSet.java:238
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.archetypeTypeList
final List< ArchetypeType > archetypeTypeList
Definition: ArchetypeTypeSet.java:54
net.sf.gridarta.model
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.addArchetypeType
void addArchetypeType(@NotNull final ArchetypeType archetypeType)
Definition: ArchetypeTypeSet.java:105
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.listTable
final Map< String, ArchetypeTypeList > listTable
Definition: ArchetypeTypeSet.java:68
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.attributeOrder
final Map< String, Integer > attributeOrder
Definition: ArchetypeTypeSet.java:81
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.isFallbackArchetypeType
boolean isFallbackArchetypeType(@NotNull final ArchetypeType archetypeType)
Definition: ArchetypeTypeSet.java:187
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getBitmask
AttributeBitmask getBitmask(@NotNull final String bitmaskName)
Definition: ArchetypeTypeSet.java:226
net.sf.gridarta.model.archetypetype.ArchetypeType
Definition: ArchetypeType.java:35
net.sf.gridarta.model.archetypetype.ArchetypeTypeSet.getArchetypeTypeCount
int getArchetypeTypeCount()
Definition: ArchetypeTypeSet.java:177