001package biweekly.util; 002 003import java.util.Arrays; 004 005/* 006 Copyright (c) 2013-2015, Michael Angstadt 007 All rights reserved. 008 009 Redistribution and use in source and binary forms, with or without 010 modification, are permitted provided that the following conditions are met: 011 012 1. Redistributions of source code must retain the above copyright notice, this 013 list of conditions and the following disclaimer. 014 2. Redistributions in binary form must reproduce the above copyright notice, 015 this list of conditions and the following disclaimer in the documentation 016 and/or other materials provided with the distribution. 017 018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 019 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 020 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 022 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 024 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 025 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 026 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 027 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 028 */ 029 030/** 031 * Represents a software version number (e.g. "1.8.14"). 032 * @author Michael Angstadt 033 */ 034public class VersionNumber implements Comparable<VersionNumber> { 035 private final Integer parts[]; 036 037 /** 038 * Creates a new version number. 039 * @param version the version string (e.g. "1.8.14") 040 * @throws IllegalArgumentException if the version string is invalid 041 */ 042 public VersionNumber(String version) { 043 if (!version.matches("[0-9]+(\\.[0-9]+)*")) { 044 throw new IllegalArgumentException("Invalid version format."); 045 } 046 047 String parts[] = version.split("\\."); 048 this.parts = new Integer[parts.length]; 049 for (int i = 0; i < parts.length; i++) { 050 this.parts[i] = Integer.valueOf(parts[i]); 051 } 052 } 053 054 public int compareTo(VersionNumber that) { 055 int length = Math.max(this.parts.length, that.parts.length); 056 for (int i = 0; i < length; i++) { 057 int thisPart = (i < this.parts.length) ? this.parts[i] : 0; 058 int thatPart = (i < that.parts.length) ? that.parts[i] : 0; 059 060 if (thisPart < thatPart) { 061 return -1; 062 } 063 if (thisPart > thatPart) { 064 return 1; 065 } 066 } 067 return 0; 068 } 069 070 @Override 071 public int hashCode() { 072 final int prime = 31; 073 int result = 1; 074 result = prime * result + Arrays.hashCode(parts); 075 return result; 076 } 077 078 @Override 079 public boolean equals(Object obj) { 080 if (this == obj) return true; 081 if (obj == null) return false; 082 if (getClass() != obj.getClass()) return false; 083 VersionNumber other = (VersionNumber) obj; 084 if (!Arrays.equals(parts, other.parts)) return false; 085 return true; 086 } 087 088 @Override 089 public String toString() { 090 return StringUtils.join(Arrays.asList(parts), "."); 091 } 092}