Gridarta Editor
RelativeMapPath.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 RelativeMapPath implements Comparable<RelativeMapPath>, MapPath {
30 
34  private static final long serialVersionUID = 1L;
35 
40  @NotNull
41  private final String path;
42 
46  public RelativeMapPath() {
47  path = "";
48  }
49 
55  public RelativeMapPath(@NotNull final RelativeMapPath 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 
83  @SuppressWarnings("TypeMayBeWeakened")
84  public RelativeMapPath(@NotNull final AbsoluteMapPath sourceMapPath, @NotNull final AbsoluteMapPath destinationMapPath) {
85  final String[] source = MapPath.SEPARATOR.split(sourceMapPath.getPath(), -1);
86  final String[] destination = MapPath.SEPARATOR.split(destinationMapPath.getPath(), -1);
87  int begin = 0;
88  while (begin < source.length && begin < destination.length && source[begin].equals(destination[begin])) {
89  begin++;
90  }
91  if (begin == destination.length && begin > 0) {
92  begin--;
93  }
94  final StringBuilder sb = new StringBuilder();
95  int i = source.length - 1;
96  while (i > begin) {
97  sb.append("/..");
98  i--;
99  }
100  while (i < destination.length) {
101  sb.append("/").append(destination[i]);
102  i++;
103  }
104  path = sb.length() > 0 ? sb.substring(1) : "";
105  }
106 
107  @NotNull
108  @Override
109 
110  public String getPath() {
111  return path;
112  }
113 
114  @Nullable
115  @Override
116  public String getMapComponent() {
117  return null;
118  }
119 
120  @NotNull
121  @Override
122  public RelativeMapPath getRelativeMapPathFrom(@NotNull final AbsoluteMapPath mapPath) {
123  //noinspection ReturnOfThis
124  return this;
125  }
126 
127  @Override
128  public int compareTo(@NotNull final RelativeMapPath o) {
129  return path.compareTo(o.path);
130  }
131 
132  @Override
133  public boolean equals(@Nullable final Object obj) {
134  if (obj == null || getClass() != obj.getClass()) {
135  return false;
136  }
137  final RelativeMapPath mapPath = (RelativeMapPath) obj;
138  return path.equals(mapPath.path);
139  }
140 
141  @Override
142  public int hashCode() {
143  return path.hashCode();
144  }
145 
146  @Override
147  public String toString() {
148  return path;
149  }
150 
151 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
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.RelativeMapPath.RelativeMapPath
RelativeMapPath()
Creates a new instance.
Definition: RelativeMapPath.java:46
net.sf.gridarta.model.mapmodel.RelativeMapPath.toString
String toString()
Definition: RelativeMapPath.java:147
net.sf.gridarta.model.mapmodel.RelativeMapPath.getRelativeMapPathFrom
RelativeMapPath getRelativeMapPathFrom(@NotNull final AbsoluteMapPath mapPath)
Returns this map path relative to another map path.
Definition: RelativeMapPath.java:122
net.sf.gridarta.model.mapmodel.RelativeMapPath.compareTo
int compareTo(@NotNull final RelativeMapPath o)
Definition: RelativeMapPath.java:128
net.sf.gridarta.model.mapmodel.MapPath
Represents a maps directory local map path.
Definition: MapPath.java:31
net.sf.gridarta.model.mapmodel.RelativeMapPath.serialVersionUID
static final long serialVersionUID
The serial versionUID,.
Definition: RelativeMapPath.java:34
net.sf.gridarta.model.mapmodel.RelativeMapPath.equals
boolean equals(@Nullable final Object obj)
Definition: RelativeMapPath.java:133
net.sf.gridarta.model.mapmodel.RelativeMapPath.path
final String path
The map path information.
Definition: RelativeMapPath.java:41
net.sf.gridarta.model.mapmodel.RelativeMapPath
Represents a maps directory local map path.
Definition: RelativeMapPath.java:29
net.sf.gridarta.model.mapmodel.MapPath.SEPARATOR
Pattern SEPARATOR
A Pattern to split a map path's string representation into components.
Definition: MapPath.java:38
net.sf.gridarta.model.mapmodel.RelativeMapPath.RelativeMapPath
RelativeMapPath(@NotNull final RelativeMapPath parent, @NotNull final String name)
Creates a new map path based on a parent map path.
Definition: RelativeMapPath.java:55
net.sf.gridarta.model.mapmodel.RelativeMapPath.hashCode
int hashCode()
Definition: RelativeMapPath.java:142
net.sf.gridarta.model.mapmodel.RelativeMapPath.getPath
String getPath()
Returns the map path information.
Definition: RelativeMapPath.java:110
net.sf.gridarta.model.mapmodel.RelativeMapPath.getMapComponent
String getMapComponent()
Returns the initial path component of a map path.
Definition: RelativeMapPath.java:116