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

com.reandroid.arsc.group.ItemGroup Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
/*
 *  Copyright (C) 2022 github.com/REAndroid
 *
 *  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.reandroid.arsc.group;

import com.reandroid.arsc.base.Block;
import com.reandroid.arsc.base.BlockArrayCreator;
import com.reandroid.utils.collection.ArrayIterator;
import com.reandroid.utils.collection.EmptyIterator;

import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

public class ItemGroup {
    private final BlockArrayCreator mBlockArrayCreator;
    private final String name;
    private T[] items;
    public ItemGroup(BlockArrayCreator blockArrayCreator, String name, T firstItem){
        this.mBlockArrayCreator = blockArrayCreator;
        this.name = name;
        T[] items;
        if(firstItem != null){
            items = blockArrayCreator.newInstance(1);
            items[0] = firstItem;
        }else {
            items = blockArrayCreator.newInstance(0);
        }
        this.items = items;
    }
    public ItemGroup(BlockArrayCreator blockArrayCreator, String name){
        this(blockArrayCreator, name, null);
    }

    public Iterator iterator(){
        return iterator(false);
    }
    public Iterator iterator(boolean skipNullBlock){
        if(size() == 0){
            return EmptyIterator.of();
        }
        if(!skipNullBlock){
            return new ArrayIterator<>(this.items);
        }
        return new ArrayIterator<>(this.items, new Predicate() {
            @Override
            public boolean test(T item) {
                return !item.isNull();
            }
        });
    }
    public Iterator iterator(Predicate tester){
        if(size() == 0){
            return EmptyIterator.of();
        }
        return new ArrayIterator<>(this.items, tester);
    }
    public List listItems(){
        return new AbstractList() {
            private final int mSize = ItemGroup.this.size();
            @Override
            public T get(int i) {
                return ItemGroup.this.get(i);
            }

            @Override
            public int size() {
                return mSize;
            }
        };
    }
    public T get(int i){
        if(i<0||i>= size()){
            return null;
        }
        return items[i];
    }
    public int size(){
        if(items==null){
            return 0;
        }
        return items.length;
    }
    public boolean contains(T block){
        if(block==null){
            return false;
        }
        int len=items.length;
        for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy