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

com.strobel.reflection.MemberList Maven / Gradle / Ivy

/*
 * MemberList.java
 *
 * Copyright (c) 2012 Mike Strobel
 *
 * This source code is subject to terms and conditions of the Apache License, Version 2.0.
 * A copy of the license can be found in the License.html file at the root of this distribution.
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the
 * Apache License, Version 2.0.
 *
 * You must not remove this notice, or any other, from this software.
 */

package com.strobel.reflection;

import com.strobel.annotations.NotNull;
import com.strobel.core.ArrayUtilities;
import com.strobel.core.ReadOnlyList;
import com.strobel.core.VerifyArgument;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mike Strobel
 */
public class MemberList extends ReadOnlyList {
    @SuppressWarnings("unchecked")
    private final static MemberList EMPTY = new MemberList(MemberInfo.class);
    private final Class _memberType;

    @SuppressWarnings("unchecked")
    public static  MemberList empty() {
        return (MemberList) EMPTY;
    }

    @SafeVarargs
    public MemberList(final Class memberType, final T... members) {
        super(VerifyArgument.noNullElements(members, "members"));
        _memberType = VerifyArgument.notNull(memberType, "memberType");
    }

    public MemberList(final Class memberType, final List members) {
        super(memberType, VerifyArgument.noNullElements(members, "members"));
        _memberType = memberType;
    }

    public MemberList(final Class memberType, final T[] members, final int offset, final int length) {
        super(VerifyArgument.noNullElements(members, offset, length, "members"), offset, length);
        _memberType = VerifyArgument.notNull(memberType, "memberType");
    }

    @NotNull
    @Override
    public MemberList subList(final int fromIndex, final int toIndex) {
        subListRangeCheck(fromIndex, toIndex, size());

        final int offset = getOffset() + fromIndex;
        final int length = toIndex - fromIndex;

        if (length == 0) {
            return empty();
        }

        return new MemberList<>(_memberType, getElements(), offset, length);
    }

    public static MemberList combine(final MemberList... lists) {
        if (ArrayUtilities.isNullOrEmpty(lists))
            return empty();

        final ArrayList members = new ArrayList<>();

        for (final MemberList list : lists) {
            if (list != null) {
                for (final MemberInfo member : list) {
                    members.add(member);
                }
            }
        }

        return new MemberList<>(MemberInfo.class, members);
    }

    Class getMemberType() {
        return _memberType;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy