KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > TokenValue


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 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /*
30  * TokenValue.java
31  *
32  * Created on March 6, 2003, 1:25 PM
33  */

34
35 package com.sun.enterprise.admin.util;
36
37 /**
38  *
39  * @author kedar
40  */

41 public final class TokenValue implements Comparable JavaDoc {
42     
43     public final String JavaDoc token;
44     public final String JavaDoc value;
45     public final String JavaDoc delimiter;
46     public final String JavaDoc delimitedToken;
47     
48     public static final String JavaDoc DEFAULT_DELIMITER = "%%%";
49     
50     /** Creates a new instance of TokenValue - with default delimiter.
51      * Also note that if the value contains any '\' characters, then these
52      * are appended to by another '\' character to work around the Java
53      * byte code interpretation. Note that none of the arguments can be null.
54      * The value of delimiter is given by DEFAULT_DELIMITER.
55      * @param token a String that is the name of the token in this TokenValue.
56      * @param value a String that is the value of the token.
57      * @throws IllegalArgumentException in case of null values.
58      * @see #TokenValue(java.lang.String, java.lang.String, java.lang.String)
59      * @see #DEFAULT_DELIMITER
60      * */

61     
62     public TokenValue(String JavaDoc token, String JavaDoc value) {
63         this(token, value, DEFAULT_DELIMITER);
64     }
65     
66     public TokenValue(String JavaDoc token, String JavaDoc value, String JavaDoc delimiter) {
67         if (token == null || value == null || delimiter == null) {
68             throw new IllegalArgumentException JavaDoc("Null Argument");
69         }
70         this.token = token;
71     /* Because of escaping process of a '\' by Java's bytecode
72      * interpreter in string literals */

73         this.value = escapeBackslashes(value);
74         this.delimiter = delimiter;
75         this.delimitedToken = delimiter + token + delimiter;
76     }
77     
78     public TokenValue(TokenValue other) {
79         this.token = other.token;
80         this.value = other.value;
81         this.delimiter = other.delimiter;
82         this.delimitedToken = other.delimitedToken;
83     }
84     
85     public int compareTo(Object JavaDoc other) {
86         final TokenValue otherTokenValue = (TokenValue) other;
87         return (this.token.compareTo(otherTokenValue.token));
88     }
89
90     public boolean equals(Object JavaDoc other) {
91         boolean same = false;
92         if (other instanceof TokenValue) {
93             same = token.equals(((TokenValue)other).token) &&
94                    delimiter.equals(((TokenValue)other).value);
95         }
96         return (same);
97     }
98     
99     public int hashCode() {
100         int result = 43;
101         result = 17 * result + token.hashCode();
102         result = 17 * result + delimiter.hashCode();
103         result = 17 * result + value.hashCode();
104         
105         return ( result );
106     }
107     
108     public String JavaDoc toString() {
109         return ( delimiter + token + delimiter + "=" + value);
110     }
111
112     /** Just appends additional '\' characters in the passed string. */
113     private String JavaDoc escapeBackslashes(String JavaDoc anyString) {
114         final char BACK_SLASH = '\\';
115         final StringBuffer JavaDoc escaped = new StringBuffer JavaDoc();
116         for(int i = 0 ; i < anyString.length() ; i++) {
117             final char ch = anyString.charAt(i);
118             escaped.append(ch);
119             if (ch == BACK_SLASH) {
120                 escaped.append(BACK_SLASH);
121             }
122         }
123         return ( escaped.toString() );
124     }
125 }
126
Popular Tags