com.github.rutledgepaulv.qbuilders.structures.FieldPath Maven / Gradle / Ivy
package com.github.rutledgepaulv.qbuilders.structures;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.*;
public class FieldPath implements Iterable {
private List chain = new LinkedList<>();
public FieldPath(String raw) {
chain.add(new FieldNamespace(raw));
}
public FieldPath(List namespaces) {
chain.addAll(namespaces);
}
@Override
public Iterator iterator() {
return chain.iterator();
}
public Stream stream() {
return streamIter(this);
}
public Optional getParentPath() {
if (chain.size() > 1) {
return Optional.of(new FieldPath(chain.subList(0, chain.size() - 1)));
} else {
return Optional.empty();
}
}
public FieldPath append(String... path) {
List chain = new LinkedList<>();
chain.addAll(this.chain);
chain.addAll(Arrays.stream(path).map(FieldNamespace::new).collect(Collectors.toList()));
return new FieldPath(chain);
}
public FieldPath append(FieldPath... path) {
List newChain = new LinkedList<>();
newChain.addAll(this.chain);
newChain.addAll(Arrays.stream(path).flatMap($ ->
$.chain.stream()).collect(Collectors.toList()));
return new FieldPath(newChain);
}
public FieldPath prepend(String path) {
List chain = new LinkedList<>();
chain.add(new FieldNamespace(path));
chain.addAll(this.chain);
return new FieldPath(chain);
}
public FieldPath prepend(FieldPath path) {
List chain = new LinkedList<>();
chain.addAll(path.chain);
chain.addAll(this.chain);
return new FieldPath(chain);
}
public String asFullyQualifiedKey() {
return stream().map(Objects::toString).collect(joining("."));
}
public String asFullyQualifiedPrefix() {
return stream().map(Objects::toString).collect(joining(".", "", "."));
}
public String asKey() {
return chain.get(chain.size() - 1).toString();
}
public String asPrefix() {
return chain.get(chain.size() - 1).toString() + ".";
}
@Override
public String toString() {
return asFullyQualifiedKey();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FieldPath)) {
return false;
}
FieldPath that = (FieldPath) o;
return Objects.equals(chain, that.chain);
}
@Override
public int hashCode() {
return Objects.hash(chain);
}
public static class FieldNamespace implements Iterable {
private String raw;
private FieldNamespace(String raw) {
this.raw = strip(raw);
}
@Override
public Iterator iterator() {
return Arrays.stream(StringUtils.split(raw, ".")).iterator();
}
public Stream stream() {
return StreamSupport.stream(spliterator(), false);
}
@Override
public String toString() {
return raw;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FieldNamespace)) {
return false;
}
FieldNamespace strings = (FieldNamespace) o;
return Objects.equals(raw, strings.raw);
}
@Override
public int hashCode() {
return Objects.hash(raw);
}
}
private static String strip(String value) {
return StringUtils.strip(value, ".");
}
private static Stream streamIter(Iterable iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}
}