Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.util.Set;
 
Encapsulates differences between two sets of classes.
Provides convenient methods to validate that chosen Version are correct.
 
 public final class Delta {

    
Library compatibility type. From most compatible to less compatible.
 
     public enum CompatibilityType {
         
         BACKWARD_COMPATIBLE_IMPLEMENTER,
         
         BACKWARD_COMPATIBLE_USER,
 
         NON_BACKWARD_COMPATIBLE
     }
 
     @Immutable
     public static class Difference implements Comparable<Difference> {
         
         private final String className;
         private final AbstractInfo info;
         
         public Difference(@Nonnull final String className, @Nonnull final AbstractInfo info) {
             if (className == null) {
                 throw new IllegalArgumentException("null className");
             }
             if (info == null) {
                 throw new IllegalArgumentException("null info");
             }
             
             this. = className;
             this. = info;
         }
 
         @Nonnull
         public String getClassName() {
             return this.;
         }
 
         @Nonnull
         public AbstractInfo getInfo() {
             return ;
         }
 
         @Override
         public int compareTo(final Difference other) {
             return getClassName().compareTo(other.getClassName());
         }
         
     }
 
     @Immutable
     public static class Add extends Difference {
         
         public Add(@Nonnull final String className, @Nonnull final AbstractInfo info) {
             super(classNameinfo);
         }
         
     }
 
     @Immutable
     public static class Change extends Difference {
         
         private final AbstractInfo modifiedInfo;
         
         public Change(@Nonnull final String className, @Nonnull final AbstractInfo info, @Nonnull final AbstractInfo modifiedInfo) {
            super(classNameinfo);
            
            this. = modifiedInfo;
        }
        public AbstractInfo getModifiedInfo() {
            return this.;
        }
        
    }
    @Immutable
    public static class Remove extends Difference {
        
        public Remove(@Nonnull final String className, @Nonnull final AbstractInfo info) {
            super(classNameinfo);
        }
        
    }
    private final Set<Differencedifferences;
    
    public Delta(@Nonnull final Set<? extends Differencedifferences) {
        this. = Collections.unmodifiableSet(differences);
    }
    @Nonnull
    public final Set<DifferencegetDifferences() {
        return this.;
    }

    

Parameters:
differences
Returns:
Delta.CompatibilityType based on specified Delta.Difference
    @Nonnull
    public final CompatibilityType computeCompatibilityType() {
        if (contains(this.Change.class) ||
            contains(this.Remove.class)) {
            return .;
        } else if (contains(this.Add.class)) {
            return .;
        } else {
            return .;
        }
    }
    protected final boolean contains(final Set<Differencedifferencesfinal Class<? extends Differencetype) {
        for (final Difference difference : differences) {
            if (type.isInstance(difference)) {
                return true;
            }
        }
        return false;
    }
    
    
Infers next Version depending on provided Delta.CompatibilityType.

Parameters:
version
compatibilityType
Returns:
    @Nonnull
    public static Version inferNextVersion(@Nonnull final Version version, @Nonnull final CompatibilityType compatibilityType) {
        if (version == null) {
            throw new IllegalArgumentException("null version");
        }
        if (compatibilityType == null) {
            throw new IllegalArgumentException("null compatibilityType");
        }
        
        switch (compatibilityType) {
            case :
                return version.next(..);
            case :
                return version.next(..);
            case :
                return version.next(..);
            default:
                throw new IllegalArgumentException("Unknown type <"+compatibilityType+">");
        }
    }    
    
    

Parameters:
previous
Returns:
an inferred Version for current JAR based on previous JAR content/version.
Throws:
IOException
    @Nonnull
    public final Version infer(@Nonnull final Version previous) {
        if (previous == null) {
            throw new IllegalArgumentException("null previous");
        }
        if (previous.isInDevelopment()) {
            throw new IllegalArgumentException("Cannot infer for in development version <"+previous+">");
        }
        
        final CompatibilityType compatibilityType = computeCompatibilityType();
        return inferNextVersion(previouscompatibilityType);
    }

    

Parameters:
previous
current
Returns:
true if Version provided for current JAR is compatible with previous JAR content/version.
Throws:
IOException
    public final boolean validate(@Nonnull final Version previous, @Nonnull final Version current) {
        if (previous == null) {
            throw new IllegalArgumentException("null previous");
        }
        if (current == null) {
            throw new IllegalArgumentException("null current");
        }
        if (previous.compareTo(current) <= 0) {
            throw new IllegalArgumentException("Current version <"+previous+"> must be more recent than previous version <"+current+">.");
        }
        //When in development public API is not considered stable
        if (current.isInDevelopment()) {
            return true;
        }
        //Current version must be superior or equals to inferred version
        final Version inferredVersion = infer(previous);
        return inferredVersion.compareTo(current) >= 0;
    }
    
New to GrepCode? Check out our FAQ X