Skip to content

Commit

Permalink
Implemented boxing wrappers for primitive sets. #1408.
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Goldberg <alexander.goldberg@bnymellon.com>
  • Loading branch information
goldbal330 committed Mar 28, 2023
1 parent 0380dcf commit d687f11
Show file tree
Hide file tree
Showing 13 changed files with 546 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ targetPath() ::= "org/eclipse/collections/api/set/primitive"
fileName(primitive) ::= "Mutable<primitive.name>Set"

class(primitive) ::= <<
<body(primitive.type, primitive.name)>
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>

body(type, name) ::= <<
body(type, name, wrapperName) ::= <<
<copyrightAndOthers()>

package org.eclipse.collections.api.set.primitive;
Expand Down Expand Up @@ -51,6 +51,8 @@ public interface Mutable<name>Set extends Mutable<name>Collection, <name>Set
@Override
Mutable<name>Set asSynchronized();

MutableSet\<<wrapperName>\> boxed();

/**
* Returns a frozen copy of this set. A frozen copy is the same thing as an immutable copy without safe-publish guarantees.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ skipBoolean() ::= "true"
fileName(primitive) ::= "AbstractMutable<primitive.name>KeySet"

class(primitive) ::= <<
<body(primitive.type, primitive.name)>
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>

body(type, name) ::= <<
body(type, name, wrapperName) ::= <<
<copyrightAndOthers()>

package org.eclipse.collections.impl.map.mutable.primitive;
Expand Down Expand Up @@ -47,6 +47,7 @@ import org.eclipse.collections.api.set.primitive.<name>Set;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.primitive.Mutable<name>Set;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.set.mutable.primitive.BoxedMutable<name>Set;
import org.eclipse.collections.impl.set.mutable.primitive.<name>HashSet;
import org.eclipse.collections.impl.set.mutable.primitive.Synchronized<name>Set;
import org.eclipse.collections.impl.set.mutable.primitive.Unmodifiable<name>Set;
Expand Down Expand Up @@ -79,6 +80,12 @@ public abstract class AbstractMutable<name>KeySet implements Mutable<name>Set, E
return !isEmptyKey(key) && !isRemovedKey(key);
}

@Override
public MutableSet\<<wrapperName>\> boxed()
{
return new BoxedMutable<name>Set(this);
}

protected abstract <type> getKeyAtIndex(int index);

protected abstract int getTableSize();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import "copyrightOf.stg"

targetPath() ::= "org/eclipse/collections/impl/set/mutable/primitive"

fileName(primitive) ::= "BoxedMutable<primitive.name>Set"

class(primitive) ::= <<
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>

body(type, name, wrapperName) ::= <<
<copyrightOf("The Bank of New York Mellon")>

package org.eclipse.collections.impl.set.mutable.primitive;

import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;

import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.iterator.Mutable<name>Iterator;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.ParallelUnsortedSetIterable;
import org.eclipse.collections.api.set.primitive.Mutable<name>Set;
import org.eclipse.collections.impl.set.mutable.AbstractMutableSet;

/**
* A boxed view of a {@link Mutable<name>Set}.
*
* This is for scenarios where optimization is not a consideration and therefore no further methods will be optimized.
* \<p>
* This file was automatically generated from template file boxedPrimitiveSet.stg.
*
* @since 12.0
*/
public class BoxedMutable<name>Set extends AbstractMutableSet\<<wrapperName>\>
implements MutableSet\<<wrapperName>\>
{
private final Mutable<name>Set delegate;

public BoxedMutable<name>Set(Mutable<name>Set delegate)
{
this.delegate = Objects.requireNonNull(delegate);
}

@Override
public int size()
{
return this.delegate.size();
}

@Override
public <wrapperName> getFirst()
{
throw new UnsupportedOperationException();
}

@Override
public <wrapperName> getLast()
{
throw new UnsupportedOperationException();
}

@Override
public void each(Procedure\<? super <wrapperName>\> procedure)
{
this.delegate.each(procedure::value);
}

@Override
public boolean add(<wrapperName> item)
{
return this.delegate.add(item.<type>Value());
}

@Override
public boolean contains(Object object)
{
return object instanceof <wrapperName> && this.delegate.contains(((<wrapperName>) object).<type>Value());
}

@Override
public boolean remove(Object object)
{
return object instanceof <wrapperName> && this.delegate.remove(((<wrapperName>) object).<type>Value());
}

@Override
public void clear()
{
this.delegate.clear();
}

@Override
public ParallelUnsortedSetIterable\<<wrapperName>\> asParallel(ExecutorService executorService, int batchSize)
{
throw new UnsupportedOperationException();
}

@Override
public int hashCode()
{
return this.delegate.hashCode();
}

@Override
public boolean equals(Object object)
{
if (this == object)
{
return true;
}

if (!(object instanceof Set))
{
return false;
}

Set\<?> other = (Set\<?>) object;
return this.size() == other.size() && this.containsAll(other);
}

@Override
public Iterator\<<wrapperName>\> iterator()
{
return new BoxedPrimitive<name>SetIterator(this.delegate.<type>Iterator());
}

private static final class BoxedPrimitive<name>SetIterator implements Iterator\<<wrapperName>\>
{
private final Mutable<name>Iterator delegate;

private BoxedPrimitive<name>SetIterator(Mutable<name>Iterator iterator)
{
this.delegate = iterator;
}

@Override
public boolean hasNext()
{
return this.delegate.hasNext();
}

@Override
public <wrapperName> next()
{
return this.delegate.next();
}
}
}

>>
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public class <name>HashSet extends Abstract<name>Set implements Mutable<name>Set
System.arraycopy(set.table, 0, this.table, 0, set.table.length);
}

@Override
public MutableSet\<<wrapperName>\> boxed()
{
return new BoxedMutable<name>Set(this);
}

private int smallestPowerOfTwoGreaterThan(int n)
{
return n > 1 ? Integer.highestOneBit(n - 1) \<\< 1 : 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ targetPath() ::= "org/eclipse/collections/impl/set/mutable/primitive"
fileName(primitive) ::= "Synchronized<primitive.name>Set"

class(primitive) ::= <<
<body(primitive.type, primitive.name)>
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>

body(type, name) ::= <<
body(type, name, wrapperName) ::= <<
<copyrightAndOthers()>

package org.eclipse.collections.impl.set.mutable.primitive;
Expand All @@ -33,6 +33,7 @@ import org.eclipse.collections.api.tuple.primitive.<name><name>Pair;
import org.eclipse.collections.impl.collection.mutable.primitive.AbstractSynchronized<name>Collection;
import org.eclipse.collections.impl.lazy.primitive.Lazy<name>IterableAdapter;
import org.eclipse.collections.impl.factory.primitive.<name>Sets;
import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet;

/**
* A synchronized view of a {@link Mutable<name>Set}. It is imperative that the user manually synchronize on the collection when iterating over it using the
Expand Down Expand Up @@ -92,6 +93,15 @@ public class Synchronized<name>Set
return this;
}

@Override
public MutableSet\<<wrapperName>\> boxed()
{
synchronized (this.getLock())
{
return SynchronizedMutableSet.of(new BoxedMutable<name>Set(this), this.getLock());
}
}

@Override
public Synchronized<name>Set with(<type> element)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ targetPath() ::= "org/eclipse/collections/impl/set/mutable/primitive"
fileName(primitive) ::= "Unmodifiable<primitive.name>Set"

class(primitive) ::= <<
<body(primitive.type, primitive.name)>
<body(primitive.type, primitive.name, primitive.wrapperName)>
>>

body(type, name) ::= <<
body(type, name, wrapperName) ::= <<
<copyrightAndOthers()>

package org.eclipse.collections.impl.set.mutable.primitive;
Expand Down Expand Up @@ -67,6 +67,12 @@ public class Unmodifiable<name>Set
throw new UnsupportedOperationException("Cannot call with() on " + this.getClass().getSimpleName());
}

@Override
public MutableSet\<<wrapperName>\> boxed()
{
return new BoxedMutable<name>Set(this);
}

@Override
public Unmodifiable<name>Set without(<type> element)
{
Expand Down
Loading

0 comments on commit d687f11

Please sign in to comment.