001    /*
002     @license.text@
003     */
004    package biz.hammurapi.util;
005    
006    import java.util.StringTokenizer;
007    
008    /**
009     * Represents 3-digit version
010     * @author Pavel Vlasov
011     * @version $Revision: 1.2 $
012     */
013    public final class Version implements Comparable {
014            private int major;
015            private int minor;
016            private int micro;
017    
018            /**
019             * Create a new instance of a <code>Version</code> object with the
020             * specified version numbers.
021             *
022             * @param major Major version number.
023             * @param minor Minor version number.
024             * @param rev Micro version number.
025             */
026            public Version(int major, int minor, int micro) {
027                    this.major = major;
028                    this.minor = minor;
029                    this.micro = micro;
030            }
031            
032            public Version(String version) {
033                    StringTokenizer st=new StringTokenizer(version, ".");
034                    if (st.countTokens()==3) {
035                            major=Integer.parseInt(st.nextToken());
036                            minor=Integer.parseInt(st.nextToken());
037                            micro=Integer.parseInt(st.nextToken());
038                    } else {
039                            throw new IllegalArgumentException("Invalid version format: "+version);
040                    }
041            }
042    
043            /**
044             * 
045             * @param other
046             * @return
047             */
048            public boolean equals(Object o) {
049                    if (o instanceof Version) {
050                            Version v=(Version) o;
051                            return v.major==major && v.minor==minor && v.micro==micro;
052                    } else {
053                            return super.equals(o);
054                    }
055            }
056    
057            /**
058             * Overload toString to report version correctly.
059             *
060             * @return the dot seperated version string
061             */
062            public String toString() {
063                    return major + "." + minor + "." + micro;
064            }
065    
066            public int compareTo(Object o) {
067                    if (o instanceof Version) {
068                            Version v=(Version) o;
069                            if (v.major==major) {
070                                    if (v.minor==minor) {
071                                            return micro-v.micro;
072                                    } else {
073                                            return minor-v.minor;
074                                    }
075                            } else {
076                                    return major-v.major;
077                            }
078                    } else {
079                            return hashCode()-o.hashCode();
080                    }
081            }
082            
083            public int hashCode() {
084                    return major ^ minor ^ micro;
085            }
086    
087            public int getMajor() {
088                    return major;
089            }
090    
091            public int getMicro() {
092                    return micro;
093            }
094    
095            public int getMinor() {
096                    return minor;
097            }
098    }