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 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public void testCFDictionary() {
CFStringRef oneStr = CFStringRef.createCFString("one");

// Key does not exist, returns null
assertEquals(0L, dict.getCount());
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(1L, 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(2L, dict.getCount());

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