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

leap.lang.resource.SimpleResourceSet Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 the original author or authors.
 *
 * 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 leap.lang.resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;

import leap.lang.Args;
import leap.lang.Arrays2;
import leap.lang.Classes;
import leap.lang.Strings;
import leap.lang.asm.ClassReader;
import leap.lang.collection.ArrayIterable;
import leap.lang.exception.NestedIOException;
import leap.lang.io.IO;
import leap.lang.logging.Log;
import leap.lang.logging.LogFactory;

public class SimpleResourceSet extends ArrayIterable implements ResourceSet {

    private static final Log log = LogFactory.get(SimpleResourceSet.class);

    public static SimpleResourceSet EMPTY = new SimpleResourceSet(new Resource[0]);
	
	protected Set> classes;
	
	public SimpleResourceSet(Resource[] values) {
	    super(values);
    }
	
	@Override
    public Resource[] toResourceArray() {
	    return values;
    }

	@Override
    public Resource getClasspathResource(String name) {
		Args.notEmpty(name,"name");
		
		for(Resource r : values){
			if(Strings.equals(r.getClasspath(), name)){
				return r;
			}
		}
		
	    return null;
    }

	@Override
    public Resource[] search(Predicate predicate) {
	    return Arrays2.filter(values, predicate).toArray(new Resource[]{});
    }

	@Override
    public Resource[] searchUrls(String... locationPatterns) {
		Args.notNull(locationPatterns,"location patterns");
		
		Set resources = new LinkedHashSet();
		
		for(String locationPattern : locationPatterns){
			try {
		        for(int i=0;i resources = new LinkedHashSet();
		
		for(String classpathPattern : classpathPatterns){
			if(classpathPattern.startsWith("/")){
				classpathPattern = classpathPattern.substring(1);
			}
			
	        for(int i=0;i[] searchClasses() {
		loadAllClasses();
	    return classes.toArray(new Class[classes.size()]);
    }

	@Override
    public Class[] searchClasses(String basePackage) {
		Args.notEmpty(basePackage,"base package");

	    loadAllClasses();
	    
	    String basePackageEndsWithDot = null;
		
		if(basePackage.endsWith(".")){
			basePackageEndsWithDot  = basePackage;
			basePackage = basePackage.substring(0,basePackage.length()-1);
		}else{
			basePackageEndsWithDot = basePackage + ".";
		}
		
		Set> classes = new LinkedHashSet>(16);
		
		for(Class clazz : this.classes){
			String packageName = Classes.getPackageName(clazz);
			if(packageName.equals(basePackage) || packageName.startsWith(basePackageEndsWithDot) ){
				classes.add(clazz);
			}
		}
		
	    return classes.toArray(new Class[classes.size()]);
    }

	@Override
    public Class[] searchClasses(String basePackage, Predicate> predicate) {
		Args.notEmpty(basePackage,"base package");
		Args.notNull(predicate);
		
		loadAllClasses();
		
	    String basePackageEndsWithDot = null;
		
		if(basePackage.endsWith(".")){
			basePackageEndsWithDot  = basePackage;
			basePackage = basePackage.substring(0,basePackage.length()-1);
		}else{
			basePackageEndsWithDot = basePackage + ".";
		}
		
		Set> classes = new LinkedHashSet>(16);
		
		for(Class clazz : this.classes){
			String packageName = Classes.getPackageName(clazz);
			if((packageName.equals(basePackage) || packageName.startsWith(basePackageEndsWithDot)) && predicate.test(clazz)){
				classes.add(clazz);
			}
		}
		
	    return classes.toArray(new Class[classes.size()]);
    }
	
	@Override
    public Class[] searchClasses(Predicate> predicate) {
		Args.notNull(predicate);
		
		loadAllClasses();
		
		Set> classes = new LinkedHashSet>(16);
		
		for(Class clazz : this.classes){
			if(predicate.test(clazz)){
				classes.add(clazz);
			}
		}
		
	    return classes.toArray(new Class[classes.size()]);
    }
	
	@Override
    public void process(Consumer action) {
		Args.notNull(action);
		
		for(Resource r : values){
			action.accept(r);
		}
    }

	@Override
    public void processClasses(Consumer> action) {
		Args.notNull(action);
		
		loadAllClasses();
		
		for(Class clazz : this.classes){
			action.accept(clazz);
		}
    }

	protected void loadAllClasses() {
		if(null == classes){
			classes = new LinkedHashSet<>();
			
	        for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy