Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G2-1632 Improvements to collection & graph ADTs #373

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,40 @@
import java.util.Iterator;
import java.util.stream.Stream;

import com.g2forge.alexandria.java.core.helpers.HCollection;

import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
public class CollectionCollection<T> implements ICollection<T> {
protected final Collection<T> collection;
protected final Collection<T> elements;

@SafeVarargs
public CollectionCollection(T... elements) {
this(HCollection.asList(elements));
}

@Override
public boolean isEmpty() {
return getElements().isEmpty();
}

@Override
public Iterator<T> iterator() {
return collection.iterator();
return getElements().iterator();
}

@Override
public Stream<T> stream() {
return collection.stream();
return getElements().stream();
}

@Override
public Collection<T> toCollection() {
return Collections.unmodifiableCollection(collection);
return Collections.unmodifiableCollection(getElements());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
*/
@FunctionalInterface
public interface DCollectionCollection<T> extends ICollection<T> {
@Override
public default boolean isEmpty() {
return toCollection().isEmpty();
}

@Override
public default Iterator<T> iterator() {
return toCollection().iterator();
}

@Override
public default Stream<T> stream() {
return toCollection().stream();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.g2forge.alexandria.adt.collection;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.stream.Stream;

import com.g2forge.alexandria.java.core.marker.ISingleton;

public class EmptyCollection<T> implements ICollection<T>, ISingleton {
protected static final EmptyCollection<Object> INSTANCE = new EmptyCollection<>();

@SuppressWarnings("unchecked")
public static <T> EmptyCollection<T> create() {
return (EmptyCollection<T>) INSTANCE;
}

protected EmptyCollection() {}

@Override
public boolean isEmpty() {
return true;
}

@Override
public Iterator<T> iterator() {
return Collections.emptyIterator();
}

@Override
public Stream<T> stream() {
return Stream.empty();
}

@Override
public Collection<T> toCollection() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public interface ICollection<T> extends Iterable<T> {
public Stream<T> stream();

public Collection<T> toCollection();

public default boolean isEmpty() {
return !iterator().hasNext();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.g2forge.alexandria.adt.collection;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.stream.Stream;

import com.g2forge.alexandria.java.core.helpers.HCollection;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class SingleCollection<T> implements ICollection<T> {
@Getter
protected final T value;

@Override
public Iterator<T> iterator() {
return stream().iterator();
}

@Override
public Stream<T> stream() {
return Stream.of(getValue());
}

@Override
public Collection<T> toCollection() {
return Collections.unmodifiableCollection(HCollection.asList(getValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import java.util.LinkedHashSet;
import java.util.Set;

import com.g2forge.alexandria.adt.collection.ICollection;
import com.g2forge.alexandria.adt.graph.v2.DiGraph.EdgeData;
import com.g2forge.alexandria.adt.graph.v2.DiGraph.ILockableGraphKey;
import com.g2forge.alexandria.adt.graph.v2.DiGraph.ImmutableGraphKey;
import com.g2forge.alexandria.adt.graph.v2.DiGraph.VertexData;
import com.g2forge.alexandria.adt.graph.v2.member.IMemberDataStrategy;
import com.g2forge.alexandria.annotations.note.Note;
import com.g2forge.alexandria.annotations.note.NoteType;
import com.g2forge.alexandria.java.core.helpers.HCollection;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -108,13 +108,13 @@ protected DiGraphBuilder<V, E> removeEdges(Collection<? extends E> edges) {
}

@Override
public DiGraphBuilder<V, E> removeEdges(@SuppressWarnings("unchecked") E... edges) {
public DiGraphBuilder<V, E> removeEdges(ICollection<E> edges) {
if (!getGraph().getKey().isWriteAllowed()) throw new IllegalStateException();
return removeEdges(HCollection.asList(edges));
return removeEdges(edges.toCollection());
}

@Override
public DiGraphBuilder<V, E> removeVertices(@SuppressWarnings("unchecked") V... vertices) {
public DiGraphBuilder<V, E> removeVertices(ICollection<V> vertices) {
final DiGraph<V, E> graph = getGraph();
if (!graph.getKey().isWriteAllowed()) throw new IllegalStateException();

Expand Down Expand Up @@ -143,7 +143,7 @@ protected DiGraphBuilder<V, E> self() {
}

@Override
public DiGraphBuilder<V, E> vertices(@SuppressWarnings("unchecked") V... vertices) {
public DiGraphBuilder<V, E> vertices(ICollection<V> vertices) {
final DiGraph<V, E> graph = getGraph();
if (!graph.getKey().isWriteAllowed()) throw new IllegalStateException();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.g2forge.alexandria.adt.graph.v2;

import com.g2forge.alexandria.adt.collection.CollectionCollection;
import com.g2forge.alexandria.adt.collection.ICollection;
import com.g2forge.alexandria.java.function.builder.IModifier;

public interface IGraphModifier<V, E, Result> extends IGraphGeneric<V, E> {
Expand Down Expand Up @@ -28,9 +30,21 @@ public interface IPathModifier<V, E, B> extends IModifier<B> {

public IPathModifier<V, E, Result> path(V source);

public Result removeEdges(@SuppressWarnings("unchecked") E... edges);
public default Result removeEdges(@SuppressWarnings("unchecked") E... edges) {
return removeEdges(new CollectionCollection<>(edges));
}

public Result removeEdges(ICollection<E> edges);

public Result removeVertices(ICollection<V> vertices);

public Result removeVertices(@SuppressWarnings("unchecked") V... vertices);
public default Result removeVertices(@SuppressWarnings("unchecked") V... vertices) {
return removeVertices(new CollectionCollection<>(vertices));
}

public Result vertices(ICollection<V> vertices);

public Result vertices(@SuppressWarnings("unchecked") V... vertices);
public default Result vertices(@SuppressWarnings("unchecked") V... vertices) {
return vertices(new CollectionCollection<>(vertices));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.LinkedHashSet;

import com.g2forge.alexandria.adt.collection.ICollection;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -52,19 +54,19 @@ public IPathModifier<V, E, Void> path(V source) {
}

@Override
public Void removeEdges(@SuppressWarnings("unchecked") E... edges) {
public Void removeEdges(ICollection<E> edges) {
getModifier().removeEdges(edges);
return null;
}

@Override
public Void removeVertices(@SuppressWarnings("unchecked") V... vertices) {
public Void removeVertices(ICollection<V> vertices) {
getModifier().removeVertices(vertices);
return null;
}

@Override
public Void vertices(@SuppressWarnings("unchecked") V... vertices) {
public Void vertices(ICollection<V> vertices) {
getModifier().vertices(vertices);
return null;
}
Expand Down
Loading