Skip to content

Commit

Permalink
Merge pull request #376 from gdgib/G2-1646-StandardPathOperations
Browse files Browse the repository at this point in the history
G2-1646 Research & implement standard path operations
  • Loading branch information
gdgib authored Sep 25, 2024
2 parents 3b4023d + dc8558d commit c093170
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.stream.Stream;

import com.g2forge.alexandria.java.core.helpers.HCollection;
Expand Down Expand Up @@ -39,6 +42,10 @@ public Stream<T> stream() {

@Override
public Collection<T> toCollection() {
return Collections.unmodifiableCollection(getElements());
final Collection<T> elements = getElements();
if (elements instanceof List) return Collections.unmodifiableList((List<T>) elements);
if (elements instanceof SortedSet) return Collections.unmodifiableSet((SortedSet<T>) elements);
if (elements instanceof Set) return Collections.unmodifiableSet((Set<T>) elements);
return Collections.unmodifiableCollection(elements);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.g2forge.alexandria.java.core.error;

public class IllegalOperationException extends Error {
private static final long serialVersionUID = -49635096423947391L;

public IllegalOperationException() {}

public IllegalOperationException(String message) {
super(message);
}

public IllegalOperationException(String message, Throwable cause) {
super(message, cause);
}

public IllegalOperationException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,82 @@
package com.g2forge.alexandria.path.path;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;

import com.g2forge.alexandria.collection.ICollection;
import com.g2forge.alexandria.java.adt.compare.CollectionComparator;
import com.g2forge.alexandria.java.core.helpers.HCollection;

public interface IPath<T> {
public static class PathComparator<T, P extends IPath<? extends T>> implements Comparator<P> {
protected final CollectionComparator<T, Collection<? extends T>> collectionComparator;

public PathComparator(final Comparator<? super T> componentComparator) {
this.collectionComparator = new CollectionComparator<>(componentComparator);
}

@Override
public int compare(P c1, P c2) {
return collectionComparator.compare(c1.getComponents().toCollection(), c2.getComponents().toCollection());
}
}

public default boolean endsWith(IPath<T> other) {
final int thisSize = size();
final int thatSize = other.size();
if (thisSize < thatSize) return false;
final List<T> thisEnding = HCollection.asList(getComponents().toCollection()).subList(thisSize - thatSize, thisSize);
final List<T> thatList = HCollection.asList(other.getComponents().toCollection());
return thisEnding.equals(thatList);
}

public default boolean endsWith(T component) {
if (isEmpty()) return false;
return getLast().equals(component);
}

public default T getComponent(int index) {
return HCollection.get(getComponents().toCollection(), index);
}

public ICollection<T> getComponents();

public default T getFirst() {
return HCollection.getFirst(getComponents().toCollection());
}

public default T getLast() {
return HCollection.getLast(getComponents().toCollection());
}

public IPath<T> getParent();

public default boolean isEmpty() {
return getComponents().isEmpty();
}

public IPath<T> resolve(IPath<T> subpath);

public boolean isEmpty();
public default IPath<T> resolveSibling(IPath<T> subpath) {
return getParent().resolve(subpath);
}

public default int size() {
if (isEmpty()) return 0;
return getComponents().toCollection().size();
}

public default boolean startsWith(IPath<T> other) {
final int thisSize = size();
final int thatSize = other.size();
if (thisSize < thatSize) return false;
final List<T> beginning = HCollection.asList(getComponents().toCollection()).subList(0, thatSize);
return beginning.equals(other.getComponents().toCollection());
}

public default boolean startsWith(T component) {
if (isEmpty()) return false;
return getFirst().equals(component);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.g2forge.alexandria.path.path;

import java.util.Collection;
import java.util.List;

import com.g2forge.alexandria.collection.CollectionCollection;
import com.g2forge.alexandria.collection.EmptyCollection;
import com.g2forge.alexandria.collection.ICollection;
import com.g2forge.alexandria.java.core.error.IllegalOperationException;
import com.g2forge.alexandria.java.core.helpers.HCollection;

import lombok.Builder;
Expand Down Expand Up @@ -34,8 +36,10 @@ public Path(T... components) {
}

@Override
public boolean isEmpty() {
return getComponents().isEmpty();
public IPath<T> getParent() {
if (isEmpty()) throw new IllegalOperationException();
final List<T> list = HCollection.asList(getComponents().toCollection());
return new Path<>(list.subList(0, list.size() - 1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.g2forge.alexandria.path.path.format;

import com.g2forge.alexandria.collection.ICollection;
import com.g2forge.alexandria.path.path.IPath;

public interface IPathFormat<T> {
public interface IPathFormat<T, P extends IPath<T>> {
public T toComponent(String component);

public IPath<T> toPath(String path);
public P toPath(ICollection<T> components);

public P toPath(String path);

public String toString(IPath<T> path);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.g2forge.alexandria.path.path.format;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.g2forge.alexandria.collection.CollectionCollection;
import com.g2forge.alexandria.path.path.IPath;
import com.g2forge.alexandria.path.path.Path;

public interface IStandardPathFormat<T> extends IPathFormat<T> {
public interface IStandardPathFormat<T, P extends IPath<T>> extends IPathFormat<T, P> {
public String getSeparator();

@Override
public default IPath<T> toPath(String path) {
final String[] components = path.split(Pattern.quote(getSeparator()));
return new Path<>(Stream.of(components).map(this::toComponent).collect(Collectors.toList()));
public default P toPath(String path) {
final String[] componentsArray = path.split(Pattern.quote(getSeparator()));
final List<T> componentsList = Stream.of(componentsArray).map(this::toComponent).collect(Collectors.toList());
return toPath(new CollectionCollection<>(componentsList));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.g2forge.alexandria.path.path.format;

import com.g2forge.alexandria.path.path.IPath;

public interface IStringPathFormat<P extends IPath<String>> extends IStandardPathFormat<String, P> {
@Override
public default String toComponent(String component) {
return component;
}

@Override
public default String toString(String component) {
return component;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.g2forge.alexandria.path.path.format;

import com.g2forge.alexandria.collection.ICollection;
import com.g2forge.alexandria.path.path.IPath;
import com.g2forge.alexandria.path.path.Path;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum OSPathFormat implements IStandardPathFormat<String> {
public enum OSPathFormat implements IStringPathFormat<IPath<String>> {
Microsoft("\\"),
POSIX("/");

protected final String separator;

@Override
public String toComponent(String component) {
return component;
}

@Override
public String toString(String component) {
return component;
public IPath<String> toPath(ICollection<String> components) {
return new Path<>(components);
}
}
Loading

0 comments on commit c093170

Please sign in to comment.