-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #376 from gdgib/G2-1646-StandardPathOperations
G2-1646 Research & implement standard path operations
- Loading branch information
Showing
9 changed files
with
288 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
ax-java/src/main/java/com/g2forge/alexandria/java/core/error/IllegalOperationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
73 changes: 72 additions & 1 deletion
73
ax-path/src/main/java/com/g2forge/alexandria/path/path/IPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
ax-path/src/main/java/com/g2forge/alexandria/path/path/format/IPathFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
ax-path/src/main/java/com/g2forge/alexandria/path/path/format/IStandardPathFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
ax-path/src/main/java/com/g2forge/alexandria/path/path/format/IStringPathFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 7 additions & 8 deletions
15
ax-path/src/main/java/com/g2forge/alexandria/path/path/format/OSPathFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.