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

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

There is a newer version: 1.2.9
Show 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.*;
/**/
import com.bugvm.apple.foundation.NSDictionary.NSDictionaryPtr;
import com.bugvm.apple.foundation.NSObject.SkipInit;

/**/
/**
 * @since Available in iOS 6.0 and later.
 */
/**/
/**/@Library("Foundation") @NativeClass/**/
/**/public/**/ class /**/NSMapTable/**/ 
    extends /**/NSObject/**/ 
    /**/implements NSCoding, NSFastEnumeration/**/, Map {

    public static class NSMapTablePtr extends Ptr, NSMapTablePtr> {}
    
    static class KeySet extends AbstractSet {
        private final NSMapTable map;
        
        KeySet(NSMapTable map) {
            this.map = map;
        }

        @Override
        public Iterator iterator() {
            return new NSEnumerator.Iterator(map.getKeyEnumerator()) {
                void remove(int index, K o) {
                    map.removeObject(o);
                }
            };
        }

        @Override
        public int size() {
            return (int) map.getCount();
        }
    }
    
    static class Entry implements Map.Entry {
        private final NSMapTable map;
        private final K key;
        private final V value;
        
        Entry(NSMapTable 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 NSMapTable map;

        EntrySet(NSMapTable 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(NSMapTable.class); }/**/
    /**//**/
    /**/
    public NSMapTable() {}
    protected NSMapTable(SkipInit skipInit) { super(skipInit); }
    public NSMapTable(NSMapTableOptions keyOptions, NSMapTableOptions valueOptions, @MachineSizedUInt long initialCapacity) { super((SkipInit) null); initObject(init(keyOptions, valueOptions, initialCapacity)); }
    public NSMapTable(NSCoder aDecoder) { super((SkipInit) null); initObject(init(aDecoder)); }
    /**/
    /**/
    @Property(selector = "count")
    protected native @MachineSizedUInt long getCount();
    /**/
    /**//**/
    
    private static void checkNull(Object key, Object value) {
        if (key == null || value == null) {
            throw new NullPointerException("null keys or values are not " 
                    + "allowed in NSMapTable. 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 = getObjectEnumerator().getAllObjects();
        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) {
        if (!(key instanceof NSObject)) {
            return null;
        }
        return (V) getObject((K) key);
    }
    public boolean isEmpty() {
        return getCount() == 0;
    }
    public Set keySet() {
        return new KeySet(this);
    }
    public int size() {
        return (int) getCount();
    }
    public Collection values() {
        return getObjectEnumerator().getAllObjects();
    }
    @Override
    public void clear() {
        removeAllObjects();
    }
    @Override
    public V remove(Object key) {
        if (!(key instanceof NSObject)) {
            return null;
        }
        V oldValue = get(key);
        removeObject((NSObject) key);
        return oldValue;
    }
    @Override
    public V put(K key, V value) {
        checkNull(key, value);
        V oldValue = get(key);
        setObject(value, key);
        return oldValue;
    }
    @Override
    public void putAll(Map m) {
        for (Map.Entry entry : m.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }
    
    public NSMapTable(Map m) {
        putAll(m);
    }
    
    public NSMapTable(Map m, NSMapTableOptions keyOptions, NSMapTableOptions valueOptions, @MachineSizedUInt long initialCapacity) {
        super((SkipInit) null);
        initObject(init(keyOptions, valueOptions, initialCapacity));
        putAll(m);
    }
    
    /**/
    @Method(selector = "initWithKeyOptions:valueOptions:capacity:")
    protected native @Pointer long init(NSMapTableOptions keyOptions, NSMapTableOptions valueOptions, @MachineSizedUInt long initialCapacity);
    @Method(selector = "objectForKey:")
    protected native NSObject getObject(NSObject aKey);
    @Method(selector = "removeObjectForKey:")
    protected native void removeObject(NSObject aKey);
    @Method(selector = "setObject:forKey:")
    protected native void setObject(NSObject anObject, NSObject aKey);
    @Method(selector = "keyEnumerator")
    protected native NSEnumerator getKeyEnumerator();
    @Method(selector = "objectEnumerator")
    protected native NSEnumerator getObjectEnumerator();
    @Method(selector = "removeAllObjects")
    protected native void removeAllObjects();
    @Method(selector = "dictionaryRepresentation")
    public native NSDictionary asDictionary();
    @Method(selector = "encodeWithCoder:")
    public native void encode(NSCoder coder);
    @Method(selector = "initWithCoder:")
    protected native @Pointer long init(NSCoder aDecoder);
    /**/
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy