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

org.revapi.simple.SimpleElementForest Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
/*
 * Copyright 2014 Lukas Krejci
 *
 * 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 org.revapi.simple;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.revapi.API;
import org.revapi.Element;
import org.revapi.ElementForest;
import org.revapi.query.Filter;

/**
 * A simple element forest of {@link org.revapi.simple.SimpleElement}s.
 *
 * @author Lukas Krejci
 * @since 0.1
 */
public class SimpleElementForest implements ElementForest {
    private SortedSet roots;
    private final API api;

    protected SimpleElementForest(@Nonnull API api) {
        this.api = api;
    }

    @Override
    @Nonnull
    public API getApi() {
        return api;
    }

    @Override
    @Nonnull
    public SortedSet getRoots() {
        if (roots == null) {
            roots = new TreeSet<>();
        }
        return roots;
    }

    @Override
    @Nonnull
    public  List search(@Nonnull Class resultType, boolean recurse,
        @Nullable Filter filter,
        @Nullable Element root) {

        List results = new ArrayList<>();
        search(results, resultType, root == null ? getRoots() : root.getChildren(), recurse, filter);
        return results;
    }

    public  void search(@Nonnull List results, @Nonnull Class resultType,
        @Nonnull SortedSet currentLevel, boolean recurse, @Nullable Filter filter) {

        for (Element e : currentLevel) {
            if (resultType.isAssignableFrom(e.getClass())) {
                @SuppressWarnings("unchecked")
                T te = (T) e;

                if (filter == null || filter.applies(te)) {
                    results.add(te);
                }
            }

            if (recurse && (filter == null || filter.shouldDescendInto(e))) {
                search(results, resultType, e.getChildren(), true, filter);
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder bld = new StringBuilder(getClass().getSimpleName());

        addToString(bld, 1, getRoots());

        return bld.toString();
    }

    private void addToString(StringBuilder bld, int indent, SortedSet elements) {
        for (Element e : elements) {
            bld.append("\n");
            for (int i = 0; i < indent; ++i) {
                bld.append("    ");
            }
            bld.append(e.toString());
            addToString(bld, indent + 1, e.getChildren());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy