1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.persistence.utils;
21
22 import com.google.common.base.Joiner;
23 import com.google.common.base.Preconditions;
24 import com.google.common.base.Splitter;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.Lists;
27
28 import java.util.List;
29 import java.util.NoSuchElementException;
30
31
32
33
34
35
36
37
38
39
40
41 final class NestableDataPath {
42
43 private static final String PATH_SEP = "/";
44 private static final Splitter splitter = Splitter.on(PATH_SEP).omitEmptyStrings();
45 private static final Joiner joiner = Joiner.on(PATH_SEP).skipNulls();
46
47 private final List<PathSection> sections;
48 private final String terminus;
49
50 private NestableDataPath(List<PathSection> sections, String terminus) {
51 Preconditions.checkNotNull(terminus);
52 this.sections = ImmutableList.copyOf(sections);
53 this.terminus = terminus;
54 }
55
56 String getTerminus() {
57 return terminus;
58 }
59
60 public PathSection current() {
61 return sections.get(0);
62 }
63
64 public boolean isEmpty() {
65 return sections.isEmpty();
66 }
67
68 public NestableDataPath next() {
69 if (sections.isEmpty())
70 throw new NoSuchElementException();
71 List<PathSection> ns = Lists.newArrayList(sections);
72 ns.remove(0);
73 return new NestableDataPath(ns, terminus);
74 }
75
76 public static NestableDataPath fromString(String path) {
77 List<PathSection> sections = Lists.newArrayList();
78 List<String> ps = Lists.newArrayList(splitter.split(path));
79 String terminus = ps.remove(ps.size() - 1);
80 for (String s : ps)
81 sections.add(PathSection.fromString(s));
82
83
84 try {
85 sections.add(PathSection.fromString(terminus));
86 return new NestableDataPath(sections, null);
87 } catch (Exception e) {
88 return new NestableDataPath(sections, terminus);
89 }
90 }
91
92 public String toString() {
93 return joiner.join(joiner.join(sections), terminus);
94 }
95 }