1 23 24 package com.sun.enterprise.tools.verifier.apiscan.packaging; 25 26 class DeweyDecimal { 27 28 private int major = 0, minor = 0, micro = 0; 29 30 public DeweyDecimal() { 31 } 32 33 public DeweyDecimal(String s) { 34 s = s.trim(); 35 int idxOfFirstDot = s.indexOf('.', 0); 36 if (idxOfFirstDot == -1) { 37 major = Integer.parseInt(s); 38 return; 39 } else { 40 major = Integer.parseInt(s.substring(0, idxOfFirstDot)); 41 } 42 int idxOfSecondDot = s.indexOf('.', idxOfFirstDot + 1); 43 if (idxOfSecondDot == -1) { 44 minor = Integer.parseInt(s.substring(idxOfFirstDot + 1)); 45 return; 46 } else { 47 minor = 48 Integer.parseInt( 49 s.substring(idxOfFirstDot + 1, idxOfSecondDot)); 50 } 51 micro = Integer.parseInt(s.substring(idxOfSecondDot + 1)); 52 } 53 54 public boolean isCompatible(DeweyDecimal another) { 55 if (another == null) return false; 56 if (major < another.major) { 57 return false; 58 } else if (major == another.major) { 59 if (minor < another.minor) { 60 return false; 61 } else if (minor == another.minor) { 62 return micro >= another.micro; 63 } 64 return true; 66 } 67 return true; 69 } 70 71 public boolean isCompatible(String another) { 72 if (another == null) return false; 73 return isCompatible(new DeweyDecimal(another)); 74 } 75 76 public int hashCode() { 78 return major + minor + micro; 79 } 80 81 public boolean equals(Object o) { 82 if (o == null) return false; 83 try { 84 DeweyDecimal other = (DeweyDecimal) o; 85 return major == other.major && minor == other.minor && 86 micro == other.micro; 87 } catch (ClassCastException e) { 88 return false; 89 } 90 } 91 92 public String toString() { 93 return "" + major + "." + minor + "." + micro; } 95 96 public static void main(String [] args) { 97 if (args.length != 2) { 98 System.out.println( 99 "Usage: " + DeweyDecimal.class.getName() + " <s1 in the format 1.2.3> <s2 in the format 5.5.6>"); System.exit(1); 102 } 103 DeweyDecimal d1 = new DeweyDecimal(args[0]); 104 DeweyDecimal d2 = new DeweyDecimal(args[1]); 105 System.out.println(d1 + ".isCompatible(" + d1 + ")=" + d1.isCompatible( d1)); 107 System.out.println(d2 + ".isCompatible(" + d2 + ")=" + d2.isCompatible( d2)); 109 System.out.println(d1 + ".isCompatible(" + d2 + ")=" + d1.isCompatible( d2)); 111 System.out.println(d2 + ".isCompatible(" + d1 + ")=" + d2.isCompatible( d1)); 113 System.out.println(d1 + ".equals(" + d1 + ")=" + d1.equals(d1)); System.out.println(d2 + ".equals(" + d2 + ")=" + d2.equals(d2)); System.out.println(d1 + ".equals(" + d2 + ")=" + d1.equals(d2)); System.out.println(d2 + ".equals(" + d1 + ")=" + d2.equals(d1)); System.out.println(d1 + ".hashCode()=" + d1.hashCode()); System.out.println(d2 + ".hashCode()=" + d2.hashCode()); } 120 } | Popular Tags |