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 CFDictionaryGetCount to c.s.j.p.mac.CoreFoundation #1416

Merged
merged 3 commits into from Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,7 @@ Features
* [#1398](https://github.com/java-native-access/jna/pull/1398): Increase `c.s.j.p.win32.Sspi#MAX_TOKEN_SIZE` on Windows 8/Server 2012 and later - [@dbwiddis](https://github.com/dbwiddis).
* [#1403](https://github.com/java-native-access/jna/pull/1403): Rebuild AIX binaries with libffi 3.4.2 (other architectures were part of 5.10) - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1404](https://github.com/java-native-access/jna/issues/1404): Added Solaris Kstat2 library - [@dbwiddis](https://github.com/dbwiddis).
* [#1416](https://github.com/java-native-access/jna/pull/1416): Add `CFDictionaryGetCount` to `c.s.j.p.mac.CoreFoundation` - [@shalupov](https://github.com/shalupov)

Bug Fixes
---------
Expand Down
19 changes: 19 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java
Expand Up @@ -401,6 +401,16 @@ public Pointer getValue(PointerType key) {
return INSTANCE.CFDictionaryGetValue(this, key);
}

/**
* Convenience method for {@link CoreFoundation#CFDictionaryGetCount(CFDictionaryRef)}
* on this object.
*
* @return The number of key-value pairs in theDict.
*/
public long getCount() {
return INSTANCE.CFDictionaryGetCount(this).longValue();
}

/**
* Convenience method for
* {@link CoreFoundation#CFDictionaryGetValueIfPresent} on this object.
Expand Down Expand Up @@ -776,6 +786,15 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, CFIndex c
*/
CFIndex CFGetRetainCount(CFTypeRef cf);

/**
* Returns the number of key-value pairs in a dictionary.
*
* @param theDict
* The dictionary to examine.
* @return The number of key-value pairs in theDict.
*/
CFIndex CFDictionaryGetCount(CoreFoundation.CFDictionaryRef theDict);

/**
* Returns the value associated with a given key.
*
Expand Down
Expand Up @@ -209,6 +209,7 @@ public void testCFDictionary() {
CFStringRef oneStr = CFStringRef.createCFString("one");

// Key does not exist, returns null
assertEquals(0, dict.getCount());
shalupov marked this conversation as resolved.
Show resolved Hide resolved
assertFalse(dict.getValueIfPresent(oneStr, null));
Pointer cfNull = dict.getValue(oneStr);
assertNull(cfNull);
Expand All @@ -223,6 +224,7 @@ public void testCFDictionary() {
IntByReference one = new IntByReference(1);
CFNumberRef cfOne = CF.CFNumberCreate(null, CFNumberType.kCFNumberIntType.typeIndex(), one);
dict.setValue(oneStr, cfOne);
assertEquals(1, dict.getCount());

assertTrue(dict.getValueIfPresent(oneStr, null));
Pointer result = dict.getValue(oneStr);
Expand All @@ -240,6 +242,7 @@ public void testCFDictionary() {
result = dict.getValue(onePtr);
CFStringRef strRef = new CFStringRef(result);
assertEquals("one", strRef.stringValue());
assertEquals(2, dict.getCount());

oneStr.release();
cfOne.release();
Expand Down