Gridarta Editor
AbsoluteMapPath.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.mapmodel;
21 
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
24 
29 public class AbsoluteMapPath implements Comparable<AbsoluteMapPath>, MapPath {
30 
34  private static final long serialVersionUID = 1L;
35 
40  @NotNull
41  private final String path;
42 
46  public AbsoluteMapPath() {
47  path = "";
48  }
49 
55  public AbsoluteMapPath(@NotNull final AbsoluteMapPath parent, @NotNull final String name) {
56  if (name.isEmpty()) {
57  throw new IllegalArgumentException("map file is empty");
58  }
59  if (name.contains("/")) {
60  throw new IllegalArgumentException("map file contains '/': " + name);
61  }
62  if (name.equals(".")) {
63  path = parent.path;
64  } else if (name.equals("..")) {
65  if (parent.path.isEmpty()) {
66  path = "";
67  } else if (parent.path.endsWith("/..") || parent.path.equals("..")) {
68  path = parent.path + "/..";
69  } else {
70  final int index = parent.path.lastIndexOf('/');
71  path = index == -1 ? "" : parent.path.substring(0, index);
72  }
73  } else {
74  path = parent.path.isEmpty() ? name : parent.path + "/" + name;
75  }
76  }
77 
78  @NotNull
79  @Override
80  public String getPath() {
81  return path;
82  }
83 
84  @Nullable
85  @Override
86  public String getMapComponent() {
87  final int index = path.indexOf('/', 1);
88  return index == -1 ? null : path.substring(0, index);
89  }
90 
91  @NotNull
92  @Override
93  public RelativeMapPath getRelativeMapPathFrom(@NotNull final AbsoluteMapPath mapPath) {
94  return new RelativeMapPath(mapPath, this);
95  }
96 
97  @Override
98  public int compareTo(@NotNull final AbsoluteMapPath o) {
99  return path.compareTo(o.path);
100  }
101 
102  @Override
103  public boolean equals(@Nullable final Object obj) {
104  if (obj == null || getClass() != obj.getClass()) {
105  return false;
106  }
107  final AbsoluteMapPath mapPath = (AbsoluteMapPath) obj;
108  return path.equals(mapPath.path);
109  }
110 
111  @Override
112  public int hashCode() {
113  return path.hashCode();
114  }
115 
116  @Override
117  public String toString() {
118  return "/" + path;
119  }
120 
121 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.getPath
String getPath()
Returns the map path information.
Definition: AbsoluteMapPath.java:80
net.sf.gridarta.model.mapmodel.AbsoluteMapPath
A MapPath that is absolute, that is, it starts with a "/".
Definition: AbsoluteMapPath.java:29
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.getMapComponent
String getMapComponent()
Returns the initial path component of a map path.
Definition: AbsoluteMapPath.java:86
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.serialVersionUID
static final long serialVersionUID
The serial versionUID,.
Definition: AbsoluteMapPath.java:34
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.AbsoluteMapPath
AbsoluteMapPath()
Creates a new instance.
Definition: AbsoluteMapPath.java:46
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.hashCode
int hashCode()
Definition: AbsoluteMapPath.java:112
net.sf.gridarta.model.mapmodel.MapPath
Represents a maps directory local map path.
Definition: MapPath.java:31
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.toString
String toString()
Definition: AbsoluteMapPath.java:117
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.path
final String path
The map path information.
Definition: AbsoluteMapPath.java:41
net.sf.gridarta.model.mapmodel.RelativeMapPath
Represents a maps directory local map path.
Definition: RelativeMapPath.java:29
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.compareTo
int compareTo(@NotNull final AbsoluteMapPath o)
Definition: AbsoluteMapPath.java:98
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.getRelativeMapPathFrom
RelativeMapPath getRelativeMapPathFrom(@NotNull final AbsoluteMapPath mapPath)
Returns this map path relative to another map path.
Definition: AbsoluteMapPath.java:93
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.AbsoluteMapPath
AbsoluteMapPath(@NotNull final AbsoluteMapPath parent, @NotNull final String name)
Creates a new map path based on a parent map path.
Definition: AbsoluteMapPath.java:55
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.equals
boolean equals(@Nullable final Object obj)
Definition: AbsoluteMapPath.java:103