All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bugvm.apple.foundation.NSDictionary Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2013-2015 RoboVM AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bugvm.apple.foundation;

/**/
import java.io.*;
import java.nio.*;
import java.util.*;
import com.bugvm.objc.*;
import com.bugvm.objc.annotation.*;
import com.bugvm.objc.block.*;
import com.bugvm.rt.*;
import com.bugvm.rt.annotation.*;
import com.bugvm.rt.bro.*;
import com.bugvm.rt.bro.annotation.*;
import com.bugvm.rt.bro.ptr.*;
import com.bugvm.apple.corefoundation.*;
import com.bugvm.apple.uikit.*;
import com.bugvm.apple.coretext.*;
import com.bugvm.apple.coreanimation.*;
import com.bugvm.apple.coredata.*;
import com.bugvm.apple.coregraphics.*;
import com.bugvm.apple.coremedia.*;
import com.bugvm.apple.security.*;
import com.bugvm.apple.dispatch.*;
/**/

/**/

/**/
/**/@Library("Foundation") @NativeClass/**/
/**/public/**/ class /**/NSDictionary/**/ 
    extends /**/NSObject/**/ 
    /**/implements NSPropertyList, NSFastEnumeration, Map/**/ {

    public static class NSDictionaryPtr extends Ptr, NSDictionaryPtr> {}
    
    public static class AsStringMapMarshaler {
        @MarshalsPointer
        public static Map toObject(Class cls, long handle, long flags) {
            NSDictionary o = (NSDictionary) NSObject.Marshaler.toObject(cls, handle, flags);
            if (o == null) {
                return null;
            }
            return o.asStringMap();
        }
        @MarshalsPointer
        public static long toNative(Map l, long flags) {
            if (l == null) {
                return 0L;
            }
            return NSObject.Marshaler.toNative(NSDictionary.fromStringMap(l), flags);
        }
    }
    
    public static class AsStringStringMapMarshaler {
        @MarshalsPointer
        public static Map toObject(Class cls, long handle, long flags) {
            NSDictionary o = (NSDictionary) NSObject.Marshaler.toObject(cls, handle, flags);
            if (o == null) {
                return null;
            }
            Map map = new HashMap<>();
            for (Map.Entry e : o.entrySet()) {
                map.put(e.getKey().toString(), e.getValue().toString());
            }
            
            return map;
        }
        @MarshalsPointer
        public static long toNative(Map l, long flags) {
            if (l == null) {
                return 0L;
            }
            NSDictionary o = new NSMutableDictionary<>();
            for (Map.Entry e : l.entrySet()) {
                o.put(new NSString(e.getKey()), new NSString(e.getValue()));
            }
            
            return NSObject.Marshaler.toNative(o, flags);
        }
    }
    
    public static class AsStringNumberMapMarshaler {
        @SuppressWarnings("unchecked")
        @MarshalsPointer
        public static Map toObject(Class cls, long handle, long flags) {
            NSDictionary o = (NSDictionary) NSObject.Marshaler.toObject(cls, handle, flags);
            if (o == null) {
                return null;
            }
            Map map = new HashMap<>();
            for (Map.Entry e : o.entrySet()) {
                map.put(e.getKey().toString(), e.getValue().doubleValue());
            }
            
            return map;
        }
        @MarshalsPointer
        public static long toNative(Map l, long flags) {
            if (l == null) {
                return 0L;
            }
            NSDictionary o = new NSMutableDictionary<>();
            for (Map.Entry e : l.entrySet()) {
                Number value = e.getValue();
                NSNumber number = null;
                
                if (value instanceof Byte) {
                   number = NSNumber.valueOf((byte) value);
                } else if (value instanceof Short) {
                    number = NSNumber.valueOf((short) value);
                } else if (value instanceof Integer) {
                    number = NSNumber.valueOf((int) value);
                } else if (value instanceof Long) {
                    number = NSNumber.valueOf((long) value);
                } else if (value instanceof Float) {
                    number = NSNumber.valueOf((float) value);
                } else if (value instanceof Double) {
                    number = NSNumber.valueOf((double) value);
                } else {
                    throw new UnsupportedOperationException("Only values of type Number (byte, short, int, long, float, double) are allowed!");
                }
                
                o.put(new NSString(e.getKey()), number);
            }
            
            return NSObject.Marshaler.toNative(o, flags);
        }
    }
    
    static class KeySet extends AbstractSet {
        private final NSDictionary map;
        
        KeySet(NSDictionary map) {
            this.map = map;
        }

        @Override
        public Iterator iterator() {
            final Iterator it = map.getAllKeys().iterator();
            return new Iterator() {
                private K last = null;
                @Override
                public boolean hasNext() {
                    return it.hasNext();
                }
                @Override
                public K next() {
                    last = it.next();
                    return last;
                }
                @Override
                public void remove() {
                    if (last == null) {
                        throw new IllegalStateException();
                    }
                    if (map.get(last) == null) {
                        throw new ConcurrentModificationException();
                    }
                    map.remove(last);
                    last = null;
                }
            };
        }

        @Override
        public int size() {
            return (int) map.getCount();
        }
    }
    
    static class Entry implements Map.Entry {
        private final NSDictionary map;
        private final K key;
        private final V value;
        
        Entry(NSDictionary map, K key, V value) {
            this.map = map;
            this.key = key;
            this.value = value;
        }

        public V setValue(V v) {
            if (value != map.get(key)) {
                throw new ConcurrentModificationException();
            }
            return map.put(key, v);
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }
        
        public boolean equals(Object object) {
            if (this == object) {
                return true;
            }
            if (object instanceof Map.Entry) {
                Map.Entry entry = (Map.Entry) object;
                return (key == null ? entry.getKey() == null : key.equals(entry.getKey()))
                        && (value == null ? entry.getValue() == null : value.equals(entry.getValue()));
            }
            return false;
        }
        
        public int hashCode() {
            return key.hashCode() ^ value.hashCode();
        }

        public String toString() {
            return key + "=" + value;
        }
    }
    
    static class EntrySet extends AbstractSet> {
        private final NSDictionary map;

        EntrySet(NSDictionary map) {
            this.map = map;
        }

        @Override
        public Iterator> iterator() {
            final Iterator keysIt = map.keySet().iterator();
            return new Iterator>() {
                private Map.Entry entry = null;

                @Override
                public boolean hasNext() {
                    return keysIt.hasNext();
                }

                @Override
                public Map.Entry next() {
                    final K key = keysIt.next();
                    final V value = map.get(key);
                    if (value == null) {
                        throw new ConcurrentModificationException();
                    }
                    entry = new Entry(map, key, value);
                    return entry;
                }

                @Override
                public void remove() {
                    if (entry == null) {
                        throw new IllegalStateException();
                    }
                    Object value = map.get(entry.getKey());
                    if (entry.getValue() != value) {
                        throw new ConcurrentModificationException();
                    }
                    keysIt.remove();
                    entry = null;
                }
                
            };
        }

        @Override
        public int size() {
            return (int) map.getCount();
        }
        
    }
    
    /**/static { ObjCRuntime.bind(NSDictionary.class); }/**/
    /**//**/
    /**/
    public NSDictionary() {}
    protected NSDictionary(SkipInit skipInit) { super(skipInit); }
    /**/

    @SuppressWarnings("unchecked")
    public NSDictionary(K k, V v) {
        super((SkipInit) null);
        initObject(init(new NSArray(v), new NSArray(k)));
    }

    @SuppressWarnings("unchecked")
    public NSDictionary(K k1, V v1, K k2, V v2) {
        super((SkipInit) null);
        initObject(init(new NSArray(v1, v2), new NSArray(k1, k2)));
    }

    @SuppressWarnings("unchecked")
    public NSDictionary(K k1, V v1, K k2, V v2, K k3, V v3) {
        super((SkipInit) null);
        initObject(init(new NSArray(v1, v2, v3), new NSArray(k1, k2, k3)));
    }

    @SuppressWarnings("unchecked")
    public NSDictionary(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
        super((SkipInit) null);
        initObject(init(new NSArray(v1, v2, v3, v4), new NSArray(k1, k2, k3, k4)));
    }

    @SuppressWarnings("unchecked")
    public NSDictionary(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
        super((SkipInit) null);
        initObject(init(new NSArray(v1, v2, v3, v4, v5), new NSArray(k1, k2, k3, k4, k5)));
    }

    public NSDictionary(Map m) {
        super((SkipInit) null);
        if (m instanceof NSDictionary) {
            initObject(init((NSDictionary) m));
        } else {
            Set keys = m.keySet();
            List objects = new ArrayList(keys.size());
            for (K key : keys) {
                V value = m.get(key);
                checkNull(key, value);
                objects.add(value);
            }
            initObject(init(new NSArray(objects), new NSArray(keys)));
        }
    }
    
    /**/
    @Property(selector = "count")
    protected native @MachineSizedUInt long getCount();
    @Property(selector = "allKeys")
    protected native NSArray getAllKeys();
    @Property(selector = "allValues")
    protected native NSArray getAllValues();
    @Property(selector = "descriptionInStringsFileFormat")
    public native String toFileFormatString();
    /**/
    /**//**/
    
    protected static void checkNull(Object key, Object value) {
        if (key == null || value == null) {
            throw new NullPointerException("null keys or values are not " 
                    + "allowed in NSDictionary. Use NSNull instead.");
        }
    }
    
    public boolean containsKey(Object key) {
        return get(key) != null;
    }
    public boolean containsValue(Object value) {
        if (!(value instanceof NSObject)) {
            return false;
        }
        NSArray values = getAllValues();
        int count = (int) values.getCount();
        for (int i = 0; i < count; i++) {
            NSObject o = values.getObjectAt(i);
            if (o.equals(value)) {
                return true;
            }
        }
        return false;
    }
    public Set> entrySet() {
        return new EntrySet(this);
    }
    @SuppressWarnings("unchecked")
    public V get(Object key) {
        return (V) getObjectForKey(key);
    }
    public boolean isEmpty() {
        return getCount() == 0;
    }
    public Set keySet() {
        return new KeySet(this);
    }
    public int size() {
        return (int) getCount();
    }
    public Collection values() {
        return getAllValues();
    }
    public void clear() {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public V put(K key, V value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void putAll(Map m) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public V remove(Object key) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    
    @Override
    public boolean equals(Object obj) {
        return obj instanceof NSDictionary && equalsTo((NSDictionary) obj);
    }
    
    public static NSDictionary read(java.io.File file) {
        return read(file.getAbsolutePath());
    }

    public boolean write(java.io.File file, boolean useAuxiliaryFile) {
        return writeFile(file.getAbsolutePath(), useAuxiliaryFile);
    }
    
    /**
     * Use this method to convert a NSDictionary with NSString keys to a Map with String keys. 
     * Keys of this NSDictionary must be of type NSString, otherwise an exception will be thrown.
     * @return
     * @throws UnsupportedOperationException when the dictionary keys are not of type NSString.
     */
    public Map asStringMap() {
        Map map = new HashMap<>();
        if (size() == 0) 
            return map;
        if (!(getAllKeys().get(0) instanceof NSString)) 
            throw new UnsupportedOperationException("keys must be of type NSString");
        
        for (java.util.Map.Entry e : entrySet()) {
            map.put(e.getKey().toString(), e.getValue());
        }
        return map;
    }
    
    public static  NSDictionary fromStringMap (Map map) {
        Map dictionary = new HashMap<>();
        for (Map.Entry entry : map.entrySet()) {
            dictionary.put(new NSString(entry.getKey()), entry.getValue());
        }
        return new NSDictionary(dictionary);
    }
    
    public boolean getBoolean(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.booleanValue();
    }
    public byte getByte(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.byteValue();
    }
    public short getShort(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.shortValue();
    }
    public char getChar(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.charValue();
    }
    public int getInt(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.intValue();
    }
    public long getLong(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.longValue();
    }
    public float getFloat(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.floatValue();
    }
    public double getDouble(Object key) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.doubleValue();
    }
    public String getString(Object key) {
        NSString val = (NSString)getObjectForKey(key);
        if (val == null) throw new NullPointerException("Value not found for key: " + key);
        return val.toString();
    }
    
    public boolean getBoolean(Object key, boolean defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.booleanValue();
    }
    public byte getByte(Object key, byte defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.byteValue();
    }
    public short getShort(Object key, short defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.shortValue();
    }
    public char getChar(Object key, char defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.charValue();
    }
    public int getInt(Object key, int defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.intValue();
    }
    public long getLong(Object key, long defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.longValue();
    }
    public float getFloat(Object key, float defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.floatValue();
    }
    public double getDouble(Object key, double defaultValue) {
        NSNumber val = (NSNumber)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.doubleValue();
    }
    public String getString(Object key, String defaultValue) {
        NSString val = (NSString)getObjectForKey(key);
        if (val == null) return defaultValue;
        return val.toString();
    }
    
    public void put(Object key, boolean value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, byte value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, short value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, char value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, int value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, long value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, float value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, double value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, String value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    public void put(Object key, NSObject value) {
        throw new UnsupportedOperationException("NSDictionary is immutable");
    }
    
    protected NSObject getObjectForKey(Object key) {
        if (key == null) throw new IllegalArgumentException("key cannot be null");
        if (key instanceof NSObject) {
            return objectForKey$(((NSObject)key).getHandle());
        } else {
            String strKey = String.valueOf(key);
            return objectForKey$(NSString.create(NSString.getChars(strKey), strKey.length()));
        }
    }
    @Method(selector = "objectForKey:")
    private native NSObject objectForKey$(@Pointer long key);
    
    /**/
    @Method(selector = "objectForKey:")
    protected native V getObject(K aKey);
    @Method(selector = "isEqualToDictionary:")
    protected native boolean equalsTo(NSDictionary otherDictionary);
    @Method(selector = "writeToFile:atomically:")
    protected native boolean writeFile(String path, boolean atomically);
    @Method(selector = "writeToURL:atomically:")
    public native boolean write(NSURL url, boolean atomically);
    @Method(selector = "initWithDictionary:")
    protected native @Pointer long init(NSDictionary otherDictionary);
    @Method(selector = "initWithObjects:forKeys:")
    protected native @Pointer long init(NSArray objects, NSArray keys);
    @Method(selector = "dictionaryWithContentsOfFile:")
    protected static native NSDictionary read(String path);
    @Method(selector = "dictionaryWithContentsOfURL:")
    public static native NSDictionary read(NSURL url);
    /**/
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy