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

com.bugvm.apple.foundation.NSOrderedSet 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.*;
/**/

/**/
/**
 * @since Available in iOS 5.0 and later.
 */
/**/
/**/@Library("Foundation") @NativeClass/**/
/**/public/**/ class /**/NSOrderedSet/**/ 
    extends /**/NSObject/**/ 
    /**/implements NSFastEnumeration, Set, List/**/ {

    public static class NSOrderedSetPtr extends Ptr, NSOrderedSetPtr> {}
    
    static class SetAdapter extends AbstractSet {
        protected final NSOrderedSet set;

        SetAdapter(NSOrderedSet set) {
            this.set = set;
        }

        @Override
        public boolean contains(Object o) {
            if (o instanceof NSObject) {
                return set.containsObject((NSObject) o);
            }
            return false;
        }

        @Override
        public Iterator iterator() {
            return new NSEnumerator.Iterator(set.objectEnumerator());
        }

        @Override
        public int size() {
            return (int) set.getCount();
        }
    }
    
    static class ListAdapter extends AbstractList {
        protected final NSOrderedSet set;

        ListAdapter(NSOrderedSet array) {
            this.set = array;
        }

        @Override
        public U get(int index) {
            checkIndex(index);
            return (U) set.getObjectAt(index);
        }

        protected void checkIndex(int index) {
            int size = (int) set.getCount();
            if (index < 0 || index >= size) {
                throw new IndexOutOfBoundsException("index = " + index + ", size = " + size);
            }
        }

        @Override
        public int size() {
            return (int) set.getCount();
        }

        @Override
        public boolean contains(Object o) {
            if (o instanceof NSObject) {
                return set.containsObject((NSObject) o);
            }
            return false;
        }
        
        @Override
        public int indexOf(Object o) {
            if (o instanceof NSObject) {
                return (int) set.indexOfObject((NSObject) o);
            }
            return -1;
        }
    }
    
    /**/static { ObjCRuntime.bind(NSOrderedSet.class); }/**/
    /**//**/
    
    private AbstractSet setAdapter = createSetAdapter();
    private AbstractList listAdapter = createListAdapter();

    /**/
    public NSOrderedSet() {}
    protected NSOrderedSet(SkipInit skipInit) { super(skipInit); }
    /**/
    
    public NSOrderedSet(Collection c) {
        super((SkipInit) null);
        if (c instanceof NSArray) {
            initObject(init((NSArray) c));
        } else if (c instanceof NSSet) {
            initObject(init((NSOrderedSet) c));
        } else {
            NSObject[] objects = c.toArray(new NSObject[c.size()]);
            initWithObjects(objects);
        }
    }
    
    public NSOrderedSet(T ... objects) {
        super((SkipInit) null);
        initWithObjects(objects);
    }

    
    /**/
    @Property(selector = "count")
    protected native @MachineSizedUInt long getCount();
    @Property(selector = "firstObject")
    public native T getFirst();
    @Property(selector = "lastObject")
    public native T getLast();
    @Property(selector = "reversedOrderedSet")
    public native NSOrderedSet getReversedOrderedSet();
    @Property(selector = "array")
    public native NSArray getArray();
    @Property(selector = "set")
    public native NSSet getSet();
    /**/
    /**//**/
    
    protected static void checkNull(Object o) {
        if (o == null) {
            throw new NullPointerException("null values are not allowed in NSOrderedSet. Use NSNull instead.");
        }
    }
    
    private void initWithObjects(NSObject[] objects) {
        VoidPtr.VoidPtrPtr ptr = Struct.allocate(VoidPtr.VoidPtrPtr.class, objects.length);
        for (int i = 0; i < objects.length; i++) {
            checkNull(objects[i]);
            ptr.set(objects[i].getHandle());
            ptr = ptr.next();
        }
        ptr = ptr.previous(objects.length);
        initObject(init(ptr.getHandle(), objects.length));
    }
    
    protected AbstractSet createSetAdapter() {
        return new SetAdapter(this);
    }
    protected AbstractList createListAdapter() {
        return new ListAdapter(this);
    }
    
    @Override
    protected void afterMarshaled(int flags) {
        if (setAdapter == null) {
            setAdapter = createSetAdapter();
        }
        if (listAdapter == null) {
            listAdapter = createListAdapter();
        }
        super.afterMarshaled(flags);
    }
    
    public boolean remove(Object o) {
        return setAdapter.remove(o);
    }
    public boolean removeAll(Collection c) {
        return setAdapter.removeAll(c);
    }
    public boolean retainAll(Collection c) {
        return setAdapter.retainAll(c);
    }
    public void add(int index, T element) {
        listAdapter.add(index, element);
    }
    public boolean add(T e) {
        return listAdapter.add(e);
    }
    public boolean addAll(Collection c) {
        return listAdapter.addAll(c);
    }
    public boolean addAll(int index, Collection c) {
        return listAdapter.addAll(index, c);
    }
    public void clear() {
        listAdapter.clear();
    }
    public boolean contains(Object o) {
        return setAdapter.contains(o);
    }
    public boolean containsAll(Collection c) {
        return setAdapter.containsAll(c);
    }
    public T get(int index) {
        return listAdapter.get(index);
    }
    public int indexOf(Object o) {
        return listAdapter.indexOf(o);
    }
    public boolean isEmpty() {
        return listAdapter.isEmpty();
    }
    public Iterator iterator() {
        return listAdapter.iterator();
    }
    public int lastIndexOf(Object o) {
        return listAdapter.lastIndexOf(o);
    }
    public ListIterator listIterator() {
        return listAdapter.listIterator();
    }
    public ListIterator listIterator(int index) {
        return listAdapter.listIterator(index);
    }
    public T remove(int index) {
        return listAdapter.remove(index);
    }
    public T set(int index, T element) {
        return listAdapter.set(index, element);
    }
    public int size() {
        return listAdapter.size();
    }
    public NSOrderedSet subList(int start, int end) {
        if (start >= 0 && end <= size()) {
            if (start <= end) {
                return new NSOrderedSet(getObjectsAt(new NSIndexSet(new NSRange(start, end - start))));
            }
            throw new IllegalArgumentException();
        }
        throw new IndexOutOfBoundsException();
    }
    public Object[] toArray() {
        return listAdapter.toArray();
    }
    public  U[] toArray(U[] a) {
        return listAdapter.toArray(a);
    }    
    
    /**/
    @Method(selector = "objectAtIndex:")
    protected native T getObjectAt(@MachineSizedUInt long idx);
    @Method(selector = "indexOfObject:")
    protected native @MachineSizedUInt long indexOfObject(NSObject object);
    @Method(selector = "initWithObjects:count:")
    protected native @Pointer long init(@Pointer long objects, @MachineSizedUInt long cnt);
    @Method(selector = "objectsAtIndexes:")
    protected native NSArray getObjectsAt(NSIndexSet indexes);
    @Method(selector = "containsObject:")
    protected native boolean containsObject(NSObject object);
    @Method(selector = "objectEnumerator")
    protected native NSEnumerator objectEnumerator();
    @Method(selector = "initWithOrderedSet:")
    protected native @Pointer long init(NSOrderedSet set);
    @Method(selector = "initWithArray:")
    protected native @Pointer long init(NSArray array);
    /**/
    @Override
    public Spliterator spliterator() {
        return List.super.spliterator();
    }
}