Gridarta Editor
AttributeRangeChecker.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.validation.checks;
21 
22 import java.util.HashMap;
23 import java.util.IdentityHashMap;
24 import java.util.Map;
33 import org.jetbrains.annotations.NotNull;
34 
39 public class AttributeRangeChecker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractValidator<G, A, R> implements GameObjectValidator<G, A, R> {
40 
44  @NotNull
45  private final Map<Integer, Type<G, A, R>> types = new HashMap<>();
46 
50  @NotNull
51  private final Map<GameObjectMatcher, Type<G, A, R>> matchers = new IdentityHashMap<>();
52 
58  super(validatorPreferences);
59  }
60 
71  public void add(final int type, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue) throws InvalidCheckException {
72  if (minValue == Integer.MIN_VALUE && maxValue == Integer.MAX_VALUE) {
73  return;
74  }
75 
76  getType(type).add(name, displayName, new Range(minValue, maxValue));
77  }
78 
89  public void add(@NotNull final GameObjectMatcher matcher, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue) throws InvalidCheckException {
90  if (minValue == Integer.MIN_VALUE && maxValue == Integer.MAX_VALUE) {
91  return;
92  }
93 
94  getMatcher(matcher).add(name, displayName, new Range(minValue, maxValue));
95  }
96 
102  @NotNull
103  private Type<G, A, R> getType(final int type) {
104  final Type<G, A, R> oldType = types.get(type);
105  if (oldType != null) {
106  return oldType;
107  }
108 
109  final Type<G, A, R> newType = new Type<>();
110  types.put(type, newType);
111  return newType;
112  }
113 
119  @NotNull
120  private Type<G, A, R> getMatcher(@NotNull final GameObjectMatcher matcher) {
121  final Type<G, A, R> oldType = matchers.get(matcher);
122  if (oldType != null) {
123  return oldType;
124  }
125 
126  final Type<G, A, R> newType = new Type<>();
127  matchers.put(matcher, newType);
128  return newType;
129  }
130 
131  @Override
132  public void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector<G, A, R> errorCollector) {
133  if (!gameObject.isHead()) {
134  return;
135  }
136 
137  for (final Map.Entry<GameObjectMatcher, Type<G, A, R>> e : matchers.entrySet()) {
138  if (e.getKey().isMatching(gameObject)) {
139  e.getValue().validate(gameObject, errorCollector);
140  }
141  }
142 
143  final Type<G, A, R> type = types.get(gameObject.getTypeNo());
144  if (type == null) {
145  return;
146  }
147 
148  type.validate(gameObject, errorCollector);
149  }
150 
151 }
A validator that checks for suspicious attribute values.
This is the base class for validators.
void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validates a game object.
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
Type< G, A, R > getMatcher(@NotNull final GameObjectMatcher matcher)
Returns the Type instance for a given GameObjectMatcher.
This package contains the framework for validating maps.
final Map< GameObjectMatcher, Type< G, A, R > > matchers
Maps matcher to Type instance.
An Exception indicating that an attribute check is invalid.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Type< G, A, R > getType(final int type)
Returns the Type instance for a given object type.
final Map< Integer, Type< G, A, R > > types
Maps object type to Type instance.
GameObjects are the objects based on Archetypes found on maps.
void validate(@NotNull final G gameObject, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a game object.
Definition: Type.java:63
Represents a range of attribute values.
Definition: Range.java:26
Manages all checks for one game object type.
Definition: Type.java:34
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
An interface for classes that collect errors.
void add(final int type, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue)
Adds an attribute to check.
AttributeRangeChecker(@NotNull final ValidatorPreferences validatorPreferences)
Creates a new instance.
void add(@NotNull final GameObjectMatcher matcher, @NotNull final String name, @NotNull final String displayName, final int minValue, final int maxValue)
Adds an attribute to check.