Skip to content

Commit

Permalink
Add CFDictionaryGetCount to c.s.j.p.mac.CoreFoundation (#1416)
Browse files Browse the repository at this point in the history
* Add CFDictionaryGetCount to c.s.j.p.mac.CoreFoundation

* Update CHANGES.md

* Add CFDictionaryGetCount: review fixes
  • Loading branch information
shalupov authored Mar 11, 2022
1 parent ec33108 commit 7ed3fa4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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

0 comments on commit 7ed3fa4

Please sign in to comment.