Skip to content

Commit

Permalink
Merge pull request eclipse#436 from mlsharrock/master
Browse files Browse the repository at this point in the history
Add multiReader factories to Sets eclipse#338
  • Loading branch information
nikhilnanivadekar authored Jan 25, 2018
2 parents 077107a + 31f7432 commit 45910d0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Goldman Sachs and others.
* Copyright (c) 2018 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.set.fixed.FixedSizeSetFactoryImpl;
import org.eclipse.collections.impl.set.immutable.ImmutableSetFactoryImpl;
import org.eclipse.collections.impl.set.mutable.MultiReaderMutableSetFactory;
import org.eclipse.collections.impl.set.mutable.MutableSetFactoryImpl;
import org.eclipse.collections.impl.set.mutable.SetAdapter;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
Expand Down Expand Up @@ -81,6 +82,7 @@ public final class Sets
public static final ImmutableSetFactory immutable = ImmutableSetFactoryImpl.INSTANCE;
public static final FixedSizeSetFactory fixedSize = FixedSizeSetFactoryImpl.INSTANCE;
public static final MutableSetFactory mutable = MutableSetFactoryImpl.INSTANCE;
public static final MutableSetFactory multiReader = MultiReaderMutableSetFactory.INSTANCE;

private static final Predicate<Set<?>> INSTANCE_OF_SORTED_SET_PREDICATE = set -> set instanceof SortedSet;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2018 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

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

import org.eclipse.collections.api.factory.set.MutableSetFactory;
import org.eclipse.collections.api.set.MutableSet;

public enum MultiReaderMutableSetFactory implements MutableSetFactory
{
INSTANCE;

@Override
public <T> MutableSet<T> empty()
{
return MultiReaderUnifiedSet.newSet();
}

@Override
public <T> MutableSet<T> of()
{
return this.empty();
}

@Override
public <T> MutableSet<T> with()
{
return this.empty();
}

@Override
public <T> MutableSet<T> of(T... items)
{
return this.with(items);
}

@Override
public <T> MutableSet<T> with(T... items)
{
return MultiReaderUnifiedSet.newSetWith(items);
}

@Override
public <T> MutableSet<T> ofInitialCapacity(int capacity)
{
return this.withInitialCapacity(capacity);
}

@Override
public <T> MutableSet<T> withInitialCapacity(int capacity)
{
if (capacity < 0)
{
throw new IllegalArgumentException("initial capacity cannot be less than 0");
}

return MultiReaderUnifiedSet.newSet(capacity);
}

@Override
public <T> MutableSet<T> ofAll(Iterable<? extends T> iterable)
{
return this.withAll(iterable);
}

@Override
public <T> MutableSet<T> withAll(Iterable<? extends T> iterable)
{
return MultiReaderUnifiedSet.newSet((Iterable<T>) iterable);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Goldman Sachs and others.
* Copyright (c) 2018 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -31,6 +31,7 @@
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.set.mutable.MultiReaderUnifiedSet;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.test.domain.Key;
Expand Down Expand Up @@ -728,6 +729,23 @@ public void fixedSize()
Verify.assertInstanceOf(FixedSizeSet.class, setFactory.of(1, 2, 3, 4));
}

@Test
public void multiReader()
{
MutableSetFactory setFactory = Sets.multiReader;
Assert.assertEquals(MultiReaderUnifiedSet.newSet(), setFactory.of());
Verify.assertInstanceOf(MultiReaderUnifiedSet.class, setFactory.of());
Assert.assertEquals(MultiReaderUnifiedSet.newSet(), setFactory.with());
Verify.assertInstanceOf(MultiReaderUnifiedSet.class, setFactory.with());
Assert.assertEquals(MultiReaderUnifiedSet.newSet(), setFactory.ofInitialCapacity(1));
Verify.assertInstanceOf(MultiReaderUnifiedSet.class, setFactory.ofInitialCapacity(1));
Verify.assertThrows(IllegalArgumentException.class, () -> setFactory.ofInitialCapacity(-1));
Assert.assertEquals(MultiReaderUnifiedSet.newSetWith(1), setFactory.of(1));
Verify.assertInstanceOf(MultiReaderUnifiedSet.class, setFactory.of(1));
Assert.assertEquals(MultiReaderUnifiedSet.newSetWith(1, 2, 3), setFactory.ofAll(UnifiedSet.newSetWith(1, 2, 3)));
Verify.assertInstanceOf(MultiReaderUnifiedSet.class, setFactory.ofAll(UnifiedSet.newSetWith(1, 2, 3)));
}

@Test
public void powerSet()
{
Expand Down

0 comments on commit 45910d0

Please sign in to comment.