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

org.echocat.jomon.resources.optimizing.CombinedResourcesOptimizer Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****
 *
 * Version: MPL 2.0
 *
 * echocat Jomon, Copyright (c) 2012-2013 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/

package org.echocat.jomon.resources.optimizing;

import org.echocat.jomon.cache.management.CacheProvider;
import org.echocat.jomon.resources.Resource;
import org.echocat.jomon.resources.ResourceType;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import static java.util.Collections.unmodifiableSet;

public class CombinedResourcesOptimizer extends CachedResourcesOptimizer {

    private final Iterable _resourceOptimizers;
    
    private Set _types;
    
    private Iterable _preResourcesOptimizers;
    private Iterable _postResourcesOptimizers;

    public CombinedResourcesOptimizer(@Nonnull Iterable resourceOptimizers, @Nonnull CacheProvider cacheProvider) {
        super(cacheProvider);
        _resourceOptimizers = resourceOptimizers;
        rebuildTypes();
    }

    public Iterable getPreResourcesOptimizers() {
        return _preResourcesOptimizers;
    }

    public void setPreResourcesOptimizers(Iterable preResourcesOptimizers) {
        _preResourcesOptimizers = preResourcesOptimizers;
        rebuildTypes();
    }

    public Iterable getPostResourcesOptimizers() {
        return _postResourcesOptimizers;
    }

    public void setPostResourcesOptimizers(Iterable postResourcesOptimizers) {
        _postResourcesOptimizers = postResourcesOptimizers;
        rebuildTypes();
    }

    @Nonnull
    @Override
    public Collection optimizeIfAbsent(@Nonnull Collection inputs, @Nonnull OptimizationContext context) throws Exception {
        final Collection preProcessed = preProcess(inputs, context);
        final Collection processed = process(preProcessed, context);
        final Collection postProcessed = postProcess(processed, context);
        return postProcessed;
    }

    @Override
    public boolean isSupporting(@Nonnull ResourceType type) {
        return _types.contains(type);
    }

    @Nonnull
    @Override
    public Set getSupportedResourceTypes() {
        return unmodifiableSet(_types);
    }

    @Nonnull
    protected Collection preProcess(@Nonnull Collection inputs, @Nonnull OptimizationContext context) throws Exception {
        Collection outputs = inputs;
        if (_preResourcesOptimizers != null) {
            for (ResourcesOptimizer preResourcesOptimizer : _preResourcesOptimizers) {
                outputs = preResourcesOptimizer.optimize(outputs, context);
            }
        }
        return outputs;
    }

    @Nonnull
    protected Collection process(@Nonnull Collection inputs, @Nonnull OptimizationContext context) throws Exception {
        final Collection outputs = new ArrayList<>();
        for (Resource input : inputs) {
            Resource resource = input;
            for (ResourceOptimizer resourceOptimizer : _resourceOptimizers) {
                if (resourceOptimizer.isSupporting(resource.getType())) {
                    resource = resourceOptimizer.optimize(resource, context);
                }
            }
            outputs.add(resource);
        }
        return outputs;
    }

    @Nonnull
    protected Collection postProcess(@Nonnull Collection inputs, @Nonnull OptimizationContext context) throws Exception {
        Collection outputs = inputs;
        if (_postResourcesOptimizers != null) {
            for (ResourcesOptimizer postResourcesOptimizer : _postResourcesOptimizers) {
                outputs = postResourcesOptimizer.optimize(outputs, context);
            }
        }
        return outputs;
    }

    protected void rebuildTypes() {
        final Set types = new HashSet<>();
        for (ResourceOptimizer resourceOptimizer : _resourceOptimizers) {
            types.addAll(resourceOptimizer.getSupportedResourceTypes());
        }
        if (_preResourcesOptimizers != null) {
            for (ResourcesOptimizer preResourcesOptimizer : _preResourcesOptimizers) {
                types.addAll(preResourcesOptimizer.getSupportedResourceTypes());
            }
        }
        if (_postResourcesOptimizers != null) {
            for (ResourcesOptimizer postResourcesOptimizer : _postResourcesOptimizers) {
                types.addAll(postResourcesOptimizer.getSupportedResourceTypes());
            }
        }
        _types = types;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy