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

dz.jtsgen.processor.nsmap.NameSpaceHelper Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
/*
 * Copyright (c) 2017 Dragan Zuvic
 *
 * This file is part of jtsgen.
 *
 * jtsgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jtsgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jtsgen.  If not, see http://www.gnu.org/licenses/
 *
 */

package dz.jtsgen.processor.nsmap;

import dz.jtsgen.processor.util.Tuple;

import javax.lang.model.element.Element;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static dz.jtsgen.processor.util.StringUtils.*;

/**
 * Some name space helper
 */
public final class NameSpaceHelper {

    /**
     *
     * @param packageNames List of package names
     * @return the top level packages, meaning, the one with shortest path length
     */
    public static Set topPackages(Collection packageNames) {
        Set result = new HashSet<>();
        int min=Integer.MAX_VALUE;

        List> nsPathLengths= packageNames.stream().map(x-> new Tuple<>(x, pathLength(x) )).collect(Collectors.toList());

        for (Tuple i: nsPathLengths){
            if (i.getSecond() < min) {
                result = new HashSet<>();
                min=i.getSecond();
                result.add(i.getFirst());
            } else if (i.getSecond().equals(min)) result.add(i.getFirst());
        }
        return result;
    }

    /**
     * @param annotatedElements a Element list
     * @return a list of tuples with first: package name second: simple name
     */
    public static List> typesWithPackageNames(Collection annotatedElements) {
        return annotatedElements.stream()
                    .map(x -> new Tuple<>(untill(x.toString()), lastOf(x.toString())))
                    .collect(Collectors.toList());
    }

    private static Integer pathLength(String x) {
        return "".equals(x)? 0 : countMatches(x) + 1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy