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-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 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  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 
86  @NotNull
87  @Override
88  public String getPath() {
89  return path;
90  }
91 
97  @NotNull
98  public RelativeMapPath getRelativeMapPathTo(@NotNull final MapPath mapPath) {
99  if (mapPath instanceof RelativeMapPath) {
100  return (RelativeMapPath) mapPath;
101  }
102  if (mapPath instanceof AbsoluteMapPath) {
103  return new RelativeMapPath(this, (AbsoluteMapPath) mapPath);
104  }
105  throw new AssertionError();
106  }
107 
108  @Nullable
109  @Override
110  public String getMapComponent() {
111  final int index = path.indexOf('/', 1);
112  if (index == -1) {
113  return null;
114  }
115 
116  return path.substring(0, index);
117  }
118 
119  @Override
120  public int compareTo(@NotNull final AbsoluteMapPath o) {
121  return path.compareTo(o.path);
122  }
123 
124  @Override
125  public boolean equals(@Nullable final Object obj) {
126  if (obj == null || getClass() != obj.getClass()) {
127  return false;
128  }
129  final AbsoluteMapPath mapPath = (AbsoluteMapPath) obj;
130  return path.equals(mapPath.path);
131  }
132 
133  @Override
134  public int hashCode() {
135  return path.hashCode();
136  }
137 
138  @Override
139  public String toString() {
140  return "/" + path;
141  }
142 
143 }
String getPath()
Returns the map path information.
int compareTo(@NotNull final AbsoluteMapPath o)
A MapPath that is absolute, that is, it starts with a "/".
Represents a maps directory local map path.
Definition: MapPath.java:31
String getMapComponent()
Returns the initial path component of a map path.
RelativeMapPath getRelativeMapPathTo(@NotNull final MapPath mapPath)
Returns a map path of an another map path relative to this map path.
static final long serialVersionUID
The serial versionUID,.
final String path
The map path information.
boolean equals(@Nullable final Object obj)
Represents a maps directory local map path.
AbsoluteMapPath(@NotNull final AbsoluteMapPath parent, @NotNull final String name)
Creates a new map path based on a parent map path.