Gridarta Editor
ConnectionChecker.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.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
40 import org.jetbrains.annotations.NotNull;
41 
47 public class ConnectionChecker<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractValidator<G, A, R> implements MapValidator<G, A, R> {
48 
52  @NotNull
54 
58  @NotNull
60 
64  @NotNull
66 
75  public ConnectionChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final GameObjectMatcher sourceGameObjectMatcher, @NotNull final GameObjectMatcher sinkGameObjectMatcher, @NotNull final GameObjectMatcher sink2GameObjectMatcher) {
76  super(validatorPreferences);
77  this.sourceGameObjectMatcher = sourceGameObjectMatcher;
78  this.sinkGameObjectMatcher = sinkGameObjectMatcher;
79  this.sink2GameObjectMatcher = sink2GameObjectMatcher;
80  }
81 
82  @Override
83  public void validateMap(@NotNull final MapModel<G, A, R> mapModel, @NotNull final ErrorCollector<G, A, R> errorCollector) {
84  final Map<Integer, Rec> info = new HashMap<>();
85  for (final Iterable<G> mapSquare : mapModel) {
86  for (final G gameObject : mapSquare) {
87  check(gameObject, info);
88  for (final G invObject : gameObject.recursive()) {
89  check(invObject, info);
90  }
91  }
92  }
93 
94  for (final Map.Entry<Integer, Rec> e : info.entrySet()) {
95  e.getValue().addErrors(e.getKey(), mapModel, errorCollector);
96  }
97  }
98 
104  private void check(@NotNull final G gameObject, @NotNull final Map<Integer, Rec> info) {
105  final int[] values = Connections.parseConnections(gameObject);
106  if (values == null) {
107  return;
108  }
109 
110  for (final int value : values) {
111  getRec(value, info).add(gameObject);
112  }
113  }
114 
122  private Rec getRec(final int value, @NotNull final Map<Integer, Rec> info) {
123  final Rec existingRec = info.get(value);
124  if (existingRec != null) {
125  return existingRec;
126  }
127 
128  final Rec rec = new Rec();
129  info.put(value, rec);
130  return rec;
131  }
132 
138  private boolean isSource(@NotNull final GameObject<?, ?, ?> gameObject) {
139  return sourceGameObjectMatcher.isMatching(gameObject);
140  }
141 
147  private boolean isSink(@NotNull final GameObject<G, A, R> gameObject) {
148  return sinkGameObjectMatcher.isMatching(gameObject) || sink2GameObjectMatcher.isMatching(gameObject);
149  }
150 
154  private class Rec {
155 
159  @NotNull
160  private final Set<G> sourceGameObjects = new HashSet<>();
161 
165  @NotNull
166  private final Set<G> sinkGameObjects = new HashSet<>();
167 
172  @NotNull
173  private final Collection<G> unknowns = new HashSet<>();
174 
179  public void add(@NotNull final G gameObject) {
180  boolean isUnknown = true;
181  if (isSource(gameObject)) {
182  sourceGameObjects.add(gameObject);
183  isUnknown = false;
184  }
185  if (isSink(gameObject)) {
186  sinkGameObjects.add(gameObject);
187  isUnknown = false;
188  }
189  if (isUnknown) {
190  unknowns.add(gameObject);
191  }
192  }
193 
200  public void addErrors(final int connection, @NotNull final MapModel<G, A, R> mapModel, @NotNull final ErrorCollector<G, A, R> errorCollector) {
201  if (!unknowns.isEmpty()) {
202  for (final G gameObject : unknowns) {
203  errorCollector.collect(new ConnectionUnknownError<>(gameObject));
204  }
205  return;
206  }
207  if (sourceGameObjects.isEmpty()) {
208  errorCollector.collect(new ConnectionWithoutSourcesError<>(mapModel, connection, sinkGameObjects));
209  }
210  if (sinkGameObjects.isEmpty()) {
211  errorCollector.collect(new ConnectionWithoutSinksError<>(mapModel, connection, sourceGameObjects));
212  }
213  }
214 
215  }
216 
217 }
void check(@NotNull final G gameObject, @NotNull final Map< Integer, Rec > info)
Processes a game object.
static int [] parseConnections(@NotNull final BaseObject<?, ?, ?, ?> gameObject)
Extract the "connected" value(s) from a given game object.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
This is the base class for validators.
boolean isSource(@NotNull final GameObject<?, ?, ?> gameObject)
Returns whether a given game object is a source if connected.
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
This package contains the framework for validating maps.
Rec getRec(final int value, @NotNull final Map< Integer, Rec > info)
Returns the record that describes a connection value.
final Set< G > sourceGameObjects
The source game objects in this connection set.
Validation error for a set of connected objects for which no sinks exist.
void addErrors(final int connection, @NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
Creates validation errors for this connection set.
Manages information about one connection set.
ConnectionChecker(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final GameObjectMatcher sourceGameObjectMatcher, @NotNull final GameObjectMatcher sinkGameObjectMatcher, @NotNull final GameObjectMatcher sink2GameObjectMatcher)
Create a new instance.
Checks that for each connection value at least one source and at least one sink exists.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final GameObjectMatcher sinkGameObjectMatcher
The GameObjectMatcher for matching sinks.
final Collection< G > unknowns
The game objects in this connection set which are neither sources nor sinks.
final Set< G > sinkGameObjects
The sink game objects in this connection set.
GameObjects are the objects based on Archetypes found on maps.
final GameObjectMatcher sink2GameObjectMatcher
The GameObjectMatcher for matching sinks.
Validation error for a set of connected objects for which no sources exist.
Utility class to parse "connected" fields in game objects.
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
void validateMap(@NotNull final MapModel< G, A, R > mapModel, @NotNull final ErrorCollector< G, A, R > errorCollector)
Validate a map.
boolean isSink(@NotNull final GameObject< G, A, R > gameObject)
Returns whether a given game object is a sink if connected.
An interface for classes that collect errors.
void add(@NotNull final G gameObject)
Adds a game object to this connection set.
boolean isMatching(@NotNull GameObject<?, ?, ?> gameObject)
Matches an GameObject.
Validation error for a connected object that is not supposed to be connected.
final GameObjectMatcher sourceGameObjectMatcher
The GameObjectMatcher for matching sources.