Merge pull request #60 from lacerta-doc/model/public_path

PublicPathの追加
This commit is contained in:
ろむねこ 2024-01-13 15:20:45 +09:00 committed by GitHub
commit 385f2f327d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,68 @@
package one.nem.lacerta.model;
import java.util.ArrayList;
import java.util.List;
public class PublicPath {
/*
* ユーザーが扱うパス(内部パスの代替)
* (時間がないのでInjectされることは考慮しない)
*
* TODO-rca:
* - こわれたパスを検知する
* - バリデーション
*/
List<String> path = new ArrayList<String>();
public PublicPath() {
}
public PublicPath(List<String> path) {
this.path = path;
}
private void add(String path) {
this.path.add(path);
}
private void resolveInternal(String path) {
if (path.equals("..")) {
this.path.remove(this.path.size() - 1);
} else {
add(path);
}
}
public PublicPath resolve(String path) {
resolveInternal(path);
return this;
}
public PublicPath resolve(List<String> path) {
for (String p : path) {
resolveInternal(p);
}
return this;
}
public PublicPath resolve(PublicPath path) {
for (String p : path.getPath()) {
resolveInternal(p);
}
return this;
}
public PublicPath parent() {
this.path.remove(this.path.size() - 1);
return this;
}
public List<String> getPath() {
return path;
}
public String getStringPath() {
return String.join("/", path);
}
}