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-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.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  if (index == -1) {
72  path = "";
73  } else {
74  path = parent.path.substring(0, index);
75  }
76  }
77  } else {
78  if (parent.path.isEmpty()) {
79  path = name;
80  } else {
81  path = parent.path + "/" + name;
82  }
83  }
84  }
85 
92  public RelativeMapPath(@NotNull final AbsoluteMapPath sourceMapPath, @NotNull final AbsoluteMapPath destinationMapPath) {
93  final String[] source = MapPath.SEPARATOR.split(sourceMapPath.getPath(), -1);
94  final String[] destination = MapPath.SEPARATOR.split(destinationMapPath.getPath(), -1);
95  int begin = 0;
96  while (begin < source.length && begin < destination.length && source[begin].equals(destination[begin])) {
97  begin++;
98  }
99  if (begin == destination.length && begin > 0) {
100  begin--;
101  }
102  final StringBuilder sb = new StringBuilder();
103  int i = source.length - 1;
104  while (i > begin) {
105  sb.append("/..");
106  i--;
107  }
108  while (i < destination.length) {
109  sb.append("/").append(destination[i]);
110  i++;
111  }
112  path = sb.length() > 0 ? sb.substring(1) : "";
113  }
114 
115  @NotNull
116  @Override
117 
118  public String getPath() {
119  return path;
120  }
121 
122  @Nullable
123  @Override
124  public String getMapComponent() {
125  return null;
126  }
127 
128  @Override
129  public int compareTo(@NotNull final RelativeMapPath o) {
130  return path.compareTo(o.path);
131  }
132 
133  @Override
134  public boolean equals(@Nullable final Object obj) {
135  if (obj == null || getClass() != obj.getClass()) {
136  return false;
137  }
138  final RelativeMapPath mapPath = (RelativeMapPath) obj;
139  return path.equals(mapPath.path);
140  }
141 
142  @Override
143  public int hashCode() {
144  return path.hashCode();
145  }
146 
147  @Override
148  public String toString() {
149  return path;
150  }
151 
152 }
RelativeMapPath(@NotNull final AbsoluteMapPath sourceMapPath, @NotNull final AbsoluteMapPath destinationMapPath)
Creates a new instance as the difference between two absolute map paths.
A MapPath that is absolute, that is, it starts with a "/".
Pattern SEPARATOR
A Pattern to split a map path&#39;s string representation into components.
Definition: MapPath.java:38
RelativeMapPath(@NotNull final RelativeMapPath parent, @NotNull final String name)
Creates a new map path based on a parent map path.
Represents a maps directory local map path.
Definition: MapPath.java:31
boolean equals(@Nullable final Object obj)
String getPath()
Returns the map path information.
int compareTo(@NotNull final RelativeMapPath o)
static final long serialVersionUID
The serial versionUID,.
Represents a maps directory local map path.
String getMapComponent()
Returns the initial path component of a map path.
final String path
The map path information.