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

Add convenience factory methods to create NSDictionary, NSArray, and NSSet #933

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 @@ -1058,6 +1058,50 @@ public static final <T> NSArray<T> emptyArray() {
return EmptyArray;
}

/**
* Returns an immutable empty {@code NSArray}.
*
* @param <E>
* the {@code NSArray}'s element type
* @return an empty {@code NSArray}
*/
public static <E> NSArray<E> of() {
return EmptyArray;
}

/**
* Returns an immutable {@code NSArray} containing one element.
*
* @param <E>
* the {@code NSArray}'s element type
* @param element
* the element to be contained in the array
* @return a {@code NSArray} containing the specified element
*/
public static <E> NSArray<E> of(E element) {
return new NSArray<>(element);
}

/**
* Returns an immutable {@code NSArray} containing an arbitrary number of elements.
*
* @param <E>
* the {@code NSArray}'s element type
* @param elements
* the elements to be contained in the array
* @return a {@code NSArray} containing the specified elements
*/
@SafeVarargs
public static <E> NSArray<E> of(E... elements) {
if (elements.length == 0) {
return EmptyArray;
} else if(elements.length == 1) {
return new NSArray(elements[0]);
}

return new NSArray<>(elements);
}

static {
try {
setOperatorForKey(CountOperatorName, new _CountOperator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,167 @@ public static final <K, V> NSDictionary<K, V> emptyDictionary() {
return NSDictionary.EmptyDictionary;
}

/**
* Returns an immutable {@code NSDictionary} containing zero entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return an empty {@code NSDictionary}
*/
public static <K, V> NSDictionary<K, V> of() {
return EmptyDictionary;
}

/**
* Returns an immutable {@code NSDictionary} containing a single entry.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1) {
return dictionaryOfImpl(k1, v1);
}

/**
* Returns an immutable {@code NSDictionary} containing two entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2) {
return dictionaryOfImpl(k1, v1, k2, v2);
}

/**
* Returns an immutable {@code NSDictionary} containing three entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3);
}

/**
* Returns an immutable {@code NSDictionary} containing four entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3, k4, v4);
}

/**
* Returns an immutable {@code NSDictionary} containing five entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}

/**
* Returns an immutable {@code NSDictionary} containing six entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6);
}

/**
* Returns an immutable {@code NSDictionary} containing seven entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7);
}

/**
* Returns an immutable {@code NSDictionary} containing eight entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8);
}

/**
* Returns an immutable {@code NSDictionary} containing nine entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9);
}

/**
* Returns an immutable {@code NSDictionary} containing ten entries.
*
* @param <K>the
* {@code NSDictionary}'s key type
* @param <V>the
* {@code NSDictionary}'s value type
* @return a {@code NSDictionary} containing the specified mapping
*/
public static <K, V> NSDictionary<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
return dictionaryOfImpl(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10);
}

@SuppressWarnings("unchecked")
private static <K, V> NSDictionary<K, V> dictionaryOfImpl(Object... input) {
if(input.length == 2) {
return new NSDictionary<>((V) input[1], (K) input[0]);
}

int capacity = input.length / 2;
Object[] keys = new Object[capacity];
Object[] values = new Object[capacity];

for (int i = 0, j = 0; i < input.length; i += 2, j++) {
keys[j] = input[i];
values[j] = input[i + 1];
}

return new NSDictionary<>((V[])values, (K[]) keys);
}

public static Object decodeObject(NSCoder coder) {
int count = coder.decodeInt();
Object[] keys = new Object[count];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,50 @@ public void encodeWithCoder(NSCoder coder) {
public static <T> NSSet<T> emptySet() {
return (NSSet<T>) EmptySet;
}

/**
* Returns an immutable empty {@code NSSet}.
*
* @param <E>
* the {@code NSSet}'s element type
* @return an empty {@code NSSet}
*/
public static <E> NSSet<E> of() {
return EmptySet;
}

/**
* Returns an immutable {@code NSSet} containing one element.
*
* @param <E>
* the {@code NSSet}'s element type
* @param element
* the element to be contained in the array
* @return a {@code NSSet} containing the specified element
*/
public static <E> NSSet<E> of(E element) {
return new NSSet<>(element);
}

/**
* Returns an immutable {@code NSSet} containing an arbitrary number of elements.
*
* @param <E>
* the {@code NSSet}'s element type
* @param elements
* the elements to be contained in the array
* @return a {@code NSSet} containing the specified elements
*/
@SafeVarargs
public static <E> NSSet<E> of(E... elements) {
if (elements.length == 0) {
return EmptySet;
} else if(elements.length == 1) {
return new NSSet(elements[0]);
}

return new NSSet<>(elements);
}

@Override
public boolean equals(Object object) {
Expand Down
37 changes: 36 additions & 1 deletion Tests/ERXTest/Sources/com/webobjects/foundation/NSArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1405,5 +1405,40 @@ public void testToArrayTArrayNull() {
} catch (NullPointerException e) {
}
}


public void testCreateNSArrayOfZeroElements() {
NSArray<?> array = NSArray.of();

assertTrue(array.isEmpty());
assertTrue(NSArray.EmptyArray == array);
}

public void testCreateNSArrayOfOneElement() {
NSArray<String> array = NSArray.of("e1");

assertEquals(1, array.size());
assertEquals("e1", array.get(0));
}

public void testCreateNSArrayOfTwoElements() {
NSArray<String> array = NSArray.of("e1", "e2");

assertEquals(2, array.size());
assertEquals("e1", array.get(0));
assertEquals("e2", array.get(1));
}

public void testCreateNSArrayFromEmptyArray() {
NSArray<String> array = NSArray.of(new String[] {});

assertTrue(array.isEmpty());
assertTrue(NSArray.EmptyArray == array);
}

public void testCreateNSArrayFromArrayWithOneElement() {
NSArray<String> array = NSArray.of(new String[] { "e1" });

assertEquals(1, array.size());
assertEquals("e1", array.get(0));
}
}
Loading