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

org.semver.Comparer Maven / Gradle / Ivy

There is a newer version: 0.9.33
Show newest version
/**
 * This software is licensed under the Apache 2 license, quoted below.
 *
 * Copyright 2010 Julien Eluard
 *
 * 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.semver;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import javax.annotation.concurrent.NotThreadSafe;

import org.osjava.jardiff.DiffException;
import org.osjava.jardiff.JarDiff;
import org.osjava.jardiff.SimpleDiffCriteria;
import org.semver.jardiff.DifferenceAccumulatingHandler;

/**
 * 
 * Allows to compare content of JARs.
 * 
 */
@NotThreadSafe
public class Comparer {

    private final File previousJAR;
    private final File currentJAR;
    private final Set includes;
    private final Set excludes;
    
    public Comparer(final File previousJAR, final File currentJAR, final Set includes, final Set excludes) {
        if (!previousJAR.isFile()) {
            throw new IllegalArgumentException("<"+previousJAR+"> is not a valid file");
        }
        if (!currentJAR.isFile()) {
            throw new IllegalArgumentException("<"+currentJAR+"> is not a valid file");
        }
        
        this.previousJAR = previousJAR;
        this.currentJAR = currentJAR;
        this.includes = includes;
        this.excludes = excludes;
    }

    /**
     * @return all {@link Difference} between both JARs
     * @throws IOException
     */
    public final Delta diff() throws IOException {        
        try {
            final JarDiff jarDiff = new JarDiff();
            jarDiff.loadOldClasses(this.previousJAR);
            jarDiff.loadNewClasses(this.currentJAR);
            final DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler(this.includes, this.excludes);
            jarDiff.diff(handler, new SimpleDiffCriteria());
            return handler.getDelta();
        } catch (DiffException e) {
            throw new RuntimeException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy