Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: s IT Solutions AT Spardat GmbH - initial API and implementation /
 
 
 /*
  * @(#) $Id: VersionNumber.java 2548 2008-04-21 13:06:32Z gub $
  */
 package at.spardat.xma.boot.cache;
 
 import java.net.URL;
Support for version numbers in resource file names. It supports parsing of version numbers, insertion of version number for delta files, insertion of hash values into the filename and matching of file names that differ only in version numbers or hash values.

Author(s):
s2877
Since:
1.3.0
 
 public class VersionNumber implements Comparable {
     final static Pattern pattern = Pattern.compile("[_-](\\d+)\\.(\\d+)\\.(\\d+)\\.");
     final static Pattern hash = Pattern.compile("[0-9a-f]{32}");
 
     int major,minor,bugfix;

    
Constructs a VersionNumber with the given major, minor and bugfix number.

Parameters:
major part of the version
minor part of the version
bugfix part of the version
 
     public VersionNumber(int major,int minorint bugfix) {
         this.=major;
         this.=minor;
         this.=bugfix;
     }

    
Parses the version number out of the given filename.

Parameters:
filename the filename to parse
Returns:
the parsed version number or null if no version number is contained in the filename
 
     static public VersionNumber parse(String filename) {
         Matcher matcher = .matcher(filename);
         if(matcher.find()){
             return new VersionNumber(
                 Integer.parseInt(matcher.group(1)),
                 Integer.parseInt(matcher.group(2)),
                 Integer.parseInt(matcher.group(3))
             );
         } else return null;
     }

    
Creates a search pattern that matches all other versions of the file defined by the given filename. Files with and without contained hash value in its name are matched. The filename must not contain any path (/ or \ charchters)

Parameters:
filename to generate the search pattern for
Returns:
a pattern matching all other versions of the given file
 
     static public Pattern searchPattern(String filename) {
         Matcher matcher = .matcher(filename);
         if(matcher.find()) {
             Matcher ext = .matcher(filename);
             if(ext.find()) {  // cut out hash
                 filename = filename.substring(0,ext.start()-1)+filename.substring(ext.end(),filename.length());
             }
             String search = filename.substring(0,matcher.start()) + .pattern()
                 + filename.substring(matcher.end(),filename.length());
             // escape all unescaped "."
             search = search.replaceAll("([^\\\\])\\.","$1\\\\.");
             // optional hash inclusion befor last dot
             int lastdot = search.lastIndexOf('.');
             search = search.substring(0,lastdot+1) + "(" + .pattern() + "\\.)?"
                 + search.substring(lastdot+1,search.length());
             return Pattern.compile("^"+search+"$");
         } else {
             return null;
         }
     }

    
Insert this version number into the given url to make it specifie the delta file to the file given in the url. e.g. http://localhost/myapp/test_1.2.0.jar -> http://localhost/myapp/test_1.1.0-1.2.0.xdelta.jar

Parameters:
url to convert into a delta download url
Returns:
the modified url
 
     public URL insertAsDelta(URL url) {
        String filename = url.toString();
        Matcher matcher = .matcher(filename);
        if(!matcher.find()) return url;
        try {
            return new URL(
                filename.substring(0,matcher.start())+ "_"
                +  + "." +  + "." +  + "-"
                + filename.substring(matcher.start()+1,matcher.end()-1)
                + ".xdelta" + filename.substring(matcher.end()-1,filename.length())
            );
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    
Inserts the given hash value into the given filename. The hash value is inserted just bevor the last file extention. If the file has no extention the hash value is appended.

Parameters:
filename to insert the hash value into
hash to insert into the filename
Returns:
the modified filename
    static public String insertHash(String filename,String hash) {
        int lastdot = filename.lastIndexOf('.');
        if(lastdot>=0) {
            return filename.substring(0,lastdot) + "." + hash + filename.substring(lastdot,filename.length());
        } else {
            return filename + "." + hash;
        }
    }

    
Creates a search pattern that matches all file defined by the given filename regardless of any included hash value. The filename must not contain any path (/ or \ charchters)

Parameters:
filename to generate the search pattern for
Returns:
a pattern matching all other versions of the given file
    static public Pattern matchWithoutHashPattern(String filename) {
        Matcher matcher = .matcher(filename);
        if(matcher.find()) {
            filename = filename.substring(0,matcher.start())
                + "(" + .pattern() + "\\.)?"
                + (matcher.end()<filename.length()?filename.substring(matcher.end()+1,filename.length()):"");
        }
        // escape all unescaped "."
        filename = filename.replaceAll("([^\\\\])\\.","$1\\\\.");
        return Pattern.compile(filename);
    }

    
Gives this version number as string.

Returns:
major.minor.bugfix
    public String toString() {
        return  + "." +  + "." + ;
    }

    
Compares this VersionNumber to an other.

Parameters:
other the VersionNumber to compare to.
Returns:
-1 if this VersionNumber is less than the other 0 if this VersionNumber is equal to the other 1 if this VersionNumber is greater than the other
Throws:
java.lang.ClassCastException if the parameter is not a VersionNumber.
    public int compareTo(Object other) {
        if(other==nullthrow new NullPointerException();
        if(!(other instanceof VersionNumber)) throw new ClassCastException();
        VersionNumber number = (VersionNumberother;
        if(<number.majorreturn -1;
        if(>number.majorreturn 1;
        if(<number.minorreturn -1;
        if(>number.minorreturn 1;
        if(<number.bugfixreturn -1;
        if(>number.bugfixreturn 1;
        return 0;
    }

    
Tests if this VersionNumber is equal to an other

Parameters:
other the VersionNumber to compare to.
Returns:
true if major, minor and bugfix are equal to the respective fields of the other VersionNumber.
    public boolean equals(Object other) {
        if(!(other instanceof VersionNumber)) return false;
        VersionNumber number = (VersionNumberother;
        if(!=number.majorreturn false;
        if(!=number.minorreturn false;
        if(!=number.bugfixreturn false;
        return true;
    }

    
Gives a hash code value of this object. The hash value is determined by the values of major, minor and bugfix. If two VersionNumbers are equal, they have the same hash code value.
    public int hashCode() {
        return toString().hashCode();
    }
New to GrepCode? Check out our FAQ X