Skip to content

Loosen type parameter restrictions in removal operations #4

@roothog

Description

@roothog

The type parameters on arguments to removal operations is overly strict, causing difficulty integrating with Java collections having looser restrictions. For example:
FSet.less(E obj);
FSet.difference(Collection<? extends E> without);

These could instead be declared as:
FSet.less(Object obj);
FSet.difference(Collection<?> without);
without loss of functionality or performance.

The stricter type parameters make it difficult to efficiently use an FSet object to back a custom collections object that implements a Java Collections API. For example, implementing
Set.removeAll(Collection<?> without);
in a custom collection backed by an FSet field named "set" is cumbersome solely due to type parameters:

@Override
public boolean removeAll(@NotNull Collection<?> toRemove) {
	FSet<E> orig = set;

	// We iterate the set rather than calling difference due to type parameter disagreement.
	for (Object o : toRemove) {
		try {
			set = set.less((E) o);
		} catch (ClassCastException ignored) {}
	}

	return orig != set;
}

If instead the FSet API looked like:
FSet.difference(Collection<?> without);
then the custom method could be written as:

@Override
public boolean removeAll(@NotNull Collection<?> toRemove) {
	int origsize = set.size();
	set = set.difference(toRemove);
	return origsize != set.size();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions