Gridarta Editor
DelegatingMapValidator.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;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
30 import org.jetbrains.annotations.NotNull;
31 
36 public class DelegatingMapValidator<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>, SquareValidator<G, A, R>, MapValidator<G, A, R>, Iterable<Validator<G, A, R>> {
37 
41  @NotNull
42  private final Collection<Validator<G, A, R>> validators = new ArrayList<>();
43 
47  @NotNull
48  public static final String DEFAULT_KEY = "Validator.All";
49 
56  }
57 
63  private DelegatingMapValidator(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final String key) {
64  super(validatorPreferences, key);
65  }
66 
71  public void validateAll(@NotNull final MapModel<G, A, R> mapModel) {
72  final ErrorCollector<G, A, R> errorCollector = new DefaultErrorCollector<>();
73  validateMap(mapModel, errorCollector);
74  for (final MapSquare<G, A, R> mapSquare : mapModel) {
75  validateSquare(mapSquare, errorCollector);
76  for (final G archObject : mapSquare) {
77  validateGameObject(archObject, errorCollector);
78  for (final G invObject : archObject.recursive()) {
79  validateGameObject(invObject, errorCollector);
80  }
81  }
82  }
83  mapModel.setErrors(errorCollector);
84  }
85 
86  @Override
87  public void validateMap(@NotNull final MapModel<G, A, R> mapModel, @NotNull final ErrorCollector<G, A, R> errorCollector) {
88  for (final Validator<G, A, R> validator : validators) {
89  if (validator.isEnabled()) {
90  if (validator instanceof MapValidator) {
91  ((MapValidator<G, A, R>) validator).validateMap(mapModel, errorCollector);
92  }
93  }
94  }
95  }
96 
97  @Override
98  public void validateSquare(@NotNull final MapSquare<G, A, R> mapSquare, @NotNull final ErrorCollector<G, A, R> errorCollector) {
99  for (final Validator<G, A, R> validator : validators) {
100  if (validator.isEnabled() && validator instanceof SquareValidator) {
101  ((SquareValidator<G, A, R>) validator).validateSquare(mapSquare, errorCollector);
102  }
103  }
104  }
105 
106  @Override
107  public void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector<G, A, R> errorCollector) {
108  for (final Validator<G, A, R> validator : validators) {
109  if (validator.isEnabled() && validator instanceof GameObjectValidator) {
110  ((GameObjectValidator<G, A, R>) validator).validateGameObject(gameObject, errorCollector);
111  }
112  }
113  }
114 
120  public void validate(@NotNull final G[] gameObjects, @NotNull final ErrorCollector<G, A, R> errorCollector) {
121  for (final G gameObject : gameObjects) {
122  validateGameObject(gameObject, errorCollector);
123  }
124  }
125 
132  public void addValidator(@NotNull final Validator<G, A, R> validator) {
133  validators.add(validator);
134  }
135 
136  @NotNull
137  @Override
138  public Iterator<Validator<G, A, R>> iterator() {
139  return validators.iterator();
140  }
141 
142 }
A MapModel reflects the data of a map.
Definition: MapModel.java:75
This is the base class for validators.
DelegatingMapValidator(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final String key)
Create a DelegatingMapValidator for a specific key.
void validateAll(@NotNull final MapModel< G, A, R > mapModel)
Perform all validations on a map.
Base package of all Gridarta classes.
void validateGameObject(@NotNull final G gameObject, @NotNull final ErrorCollector< G, A, R > errorCollector)
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
final Collection< Validator< G, A, R > > validators
Map Validators to validate against.
DelegatingMapValidator(@NotNull final ValidatorPreferences validatorPreferences)
Create a DelegatingMapValidator for the default key.
void validateSquare(@NotNull final MapSquare< G, A, R > mapSquare, @NotNull final ErrorCollector< G, A, R > errorCollector)
A Map Validator that delegates to other MapValidators.
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
void validateMap(@NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
void validate(@NotNull final G[] gameObjects, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a set of game objects.
Simple error collector that just collects the error in a collection for later retrieval through itera...
void addValidator(@NotNull final Validator< G, A, R > validator)
Add a Validator that might to be queried.