Gridarta Editor
Connections.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.connectionview;
21 
22 import java.util.regex.Pattern;
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26 
31 public class Connections {
32 
36  private static final Pattern PATTERN_VALUES = Pattern.compile("\\d+(, *\\d+)*");
37 
41  private static final Pattern PATTERN_VALUE_SEPARATOR = Pattern.compile(",\\s*");
42 
46  private Connections() {
47  }
48 
55  public static int @Nullable [] parseConnections(@NotNull final BaseObject<?, ?, ?, ?> gameObject) {
56  final String connectionSpec = gameObject.getAttributeString("connected", false);
57  if (!PATTERN_VALUES.matcher(connectionSpec).matches()) {
58  return null;
59  }
60 
61  final String[] values = PATTERN_VALUE_SEPARATOR.split(connectionSpec.trim(), 0);
62  final int[] result = new int[values.length];
63  for (int i = 0; i < values.length; i++) {
64  try {
65  result[i] = Integer.valueOf(values[i]);
66  } catch (final NumberFormatException ignore) {
67  // just ignore invalid connection values; will be detected by a map validator
68  }
69  }
70  return result;
71  }
72 
73 }
net.sf.gridarta.model.connectionview.Connections.parseConnections
static int[] parseConnections(@NotNull final BaseObject<?, ?, ?, ?> gameObject)
Extract the "connected" value(s) from a given game object.
Definition: Connections.java:55
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.connectionview.Connections.PATTERN_VALUES
static final Pattern PATTERN_VALUES
Pattern to match the arguments of "connected" fields.
Definition: Connections.java:36
net
net.sf.gridarta.model.connectionview.Connections.PATTERN_VALUE_SEPARATOR
static final Pattern PATTERN_VALUE_SEPARATOR
Pattern to split different values in "connected" field arguments.
Definition: Connections.java:41
net.sf.gridarta.model.connectionview.Connections.Connections
Connections()
Private constructor to prevent instantiation.
Definition: Connections.java:46
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.model
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.connectionview.Connections
Utility class to parse "connected" fields in game objects.
Definition: Connections.java:31