KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > apiscan > packaging > DeweyDecimal


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 JavaDoc 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             //this.minor> another.minor && this.major==another.major, hence return true
65
return true;
66         }
67         //this.major> another.major, hence return true
68
return true;
69     }
70
71     public boolean isCompatible(String JavaDoc another) {
72         if (another == null) return false;
73         return isCompatible(new DeweyDecimal(another));
74     }
75
76     //provides value semantics, hence we should overrise hashCode and equals method.
77
public int hashCode() {
78         return major + minor + micro;
79     }
80
81     public boolean equals(Object JavaDoc 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 JavaDoc e) {
88             return false;
89         }
90     }
91
92     public String JavaDoc toString() {
93         return "" + major + "." + minor + "." + micro; // NOI18N
94
}
95
96     public static void main(String JavaDoc[] args) {
97         if (args.length != 2) {
98             System.out.println(
99                     "Usage: " + DeweyDecimal.class.getName() + // NOI18N
100
" <s1 in the format 1.2.3> <s2 in the format 5.5.6>"); // NOI18N
101
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( // NOI18N
106
d1));
107         System.out.println(d2 + ".isCompatible(" + d2 + ")=" + d2.isCompatible( // NOI18N
108
d2));
109         System.out.println(d1 + ".isCompatible(" + d2 + ")=" + d1.isCompatible( // NOI18N
110
d2));
111         System.out.println(d2 + ".isCompatible(" + d1 + ")=" + d2.isCompatible( // NOI18N
112
d1));
113         System.out.println(d1 + ".equals(" + d1 + ")=" + d1.equals(d1)); // NOI18N
114
System.out.println(d2 + ".equals(" + d2 + ")=" + d2.equals(d2)); // NOI18N
115
System.out.println(d1 + ".equals(" + d2 + ")=" + d1.equals(d2)); // NOI18N
116
System.out.println(d2 + ".equals(" + d1 + ")=" + d2.equals(d1)); // NOI18N
117
System.out.println(d1 + ".hashCode()=" + d1.hashCode()); // NOI18N
118
System.out.println(d2 + ".hashCode()=" + d2.hashCode()); // NOI18N
119
}
120 }
Popular Tags