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

com.reandroid.dex.common.SectionTool Maven / Gradle / Ivy

The 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.dex.common;

import com.reandroid.arsc.base.Block;
import com.reandroid.dex.key.Key;
import com.reandroid.dex.pool.DexSectionPool;
import com.reandroid.dex.sections.Section;
import com.reandroid.dex.sections.SectionList;
import com.reandroid.dex.sections.SectionType;

import java.util.Iterator;

public interface SectionTool {

    default T1 createSectionItem(SectionType sectionType) {
        Section section = getOrCreateSection(sectionType);
        if(section != null){
            return section.createItem();
        }
        return null;
    }
    default T1[] getSectionItem(SectionType sectionType, int[] indexes){
        if(indexes == null || indexes.length == 0){
            return null;
        }
        Section section = getSection(sectionType);
        if(section == null){
            return null;
        }
        return section.getSectionItems(indexes);
    }
    default T1 getSectionItem(SectionType sectionType, Key key){
        Section section = getSection(sectionType);
        if(section != null){
            return section.getSectionItem(key);
        }
        return null;
    }
    default T1 getSectionItem(SectionType sectionType, int i){
        Section section = getSection(sectionType);
        if(section != null){
            return section.getSectionItem(i);
        }
        return null;
    }
    default Section getSection(SectionType sectionType){
        SectionList sectionList = getSectionList();
        if(sectionList != null){
            return sectionList.getSection(sectionType);
        }
        return null;
    }
    default boolean isReading(){
        SectionList sectionList = getSectionList();
        return sectionList == null || sectionList.isReading();
    }
    default SectionList getSectionList(){
        return ((Block) this).getParent(SectionList.class);
    }
    default Section getOrCreateSection(SectionType sectionType){
        SectionList sectionList = getSectionList();
        if(sectionList != null){
            return sectionList.getOrCreateSection(sectionType);
        }
        return null;
    }
    default T1 getOrCreateSectionItem(SectionType sectionType, Key key){
        if(key == null){
            return null;
        }
        Section section = getOrCreateSection(sectionType);
        if(section == null){
            return null;
        }
        DexSectionPool pool = section.getPool();
        if(pool != null){
            return pool.getOrCreate(key);
        }
        return null;
    }
    default DexSectionPool getPool(SectionType sectionType){
        Section section = getSection(sectionType);
        if(section != null){
            return section.getPool();
        }
        return null;
    }
    default DexSectionPool getLoadedPool(SectionType sectionType){
        Section section = getSection(sectionType);
        if(section != null){
            return section.getLoadedPool();
        }
        return null;
    }
    default DexSectionPool getOrCreatePool(SectionType sectionType){
        Section section = getOrCreateSection(sectionType);
        if(section != null){
            return section.getPool();
        }
        return null;
    }
    static int compareIdx(Iterator iterator1, Iterator iterator2){
        while (iterator1.hasNext() && iterator2.hasNext()){
            int i = compareIdx(iterator1.next(), iterator2.next());
            if(i != 0){
                return i;
            }
        }
        if(iterator1.hasNext()){
            return 1;
        }
        if(iterator2.hasNext()){
            return -1;
        }
        return 0;
    }
    static int compareIdx(T item1, T item2){
        if(item1 == item2){
            return 0;
        }
        if(item1 == null){
            return 1;
        }
        if(item2 == null){
            return -1;
        }
        int i1 = item1.getIdx();
        int i2 = item2.getIdx();
        if(i1 == i2){
            return 0;
        }
        if(i1 > i2){
            return 1;
        }
        return -1;
    }
    static int compareIndex(T item1, T item2){
        if(item1 == item2){
            return 0;
        }
        if(item1 == null){
            return 1;
        }
        if(item2 == null){
            return -1;
        }
        int i1 = item1.getIndex();
        int i2 = item2.getIndex();
        if(i1 == i2){
            return 0;
        }
        if(i1 > i2){
            return 1;
        }
        return -1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy