KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > util > HexUtil


1 /*
2  * @(#)HexUtil.java
3  *
4  * Copyright (C) 2004 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.util;
28
29 import java.util.Comparator JavaDoc;
30 import java.util.zip.CRC32 JavaDoc;
31 import java.util.zip.Checksum JavaDoc;
32
33
34 /**
35  * A simple utility to provide the Hexidecimal conversions for the log files.
36  * This contains the string-length checking and other verification steps
37  * that might be left out of some implementations.
38  *
39  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
40  * @version $Date: 2004/07/08 05:29:20 $
41  * @since July 7, 2004
42  */

43 public class HexUtil
44 {
45     protected static HexUtil s_instance = new HexUtil();
46     
47     
48     public static class TwoShorts {
49         public short a;
50         public short b;
51         
52         public TwoShorts() {}
53         public TwoShorts( short a, short b )
54         {
55             this.a = a;
56             this.b = b;
57         }
58     }
59     
60     
61     /**
62      * Creates a new finder using the default (system) classpath.
63      */

64     protected HexUtil()
65     {
66         // do nothing
67
}
68     
69     
70     public static final HexUtil getInstance()
71     {
72         return s_instance;
73     }
74     
75     
76     /**
77      * Parses two hex strings separated by a character. The string should
78      * not start with the separator char after the startPos.
79      *
80      * @return <code>true</code> if <code>ts</code> was populated correctly,
81      * or <code>false</code> if there was an error parsing the string.
82      */

83     public boolean parseTwoHex( String JavaDoc text, final TwoShorts ts,
84             final char sep, final int startPos )
85     {
86         if (text == null || ts == null || startPos < 0)
87         {
88             return false;
89         }
90         
91         int sepPos = text.indexOf( sep, startPos );
92         if (sepPos <= 0 || sepPos >= text.length())
93         {
94             return false;
95         }
96         
97         try
98         {
99             ts.a = (short)Integer.parseInt(
100                 text.substring( startPos, sepPos ), 16 );
101             ts.b = (short)Integer.parseInt(
102                 text.substring( sepPos + 1 ).trim(), 16 );
103         }
104         catch (NumberFormatException JavaDoc e)
105         {
106             return false;
107         }
108         
109         return true;
110     }
111     
112     
113     /* This logic for creating the HEX string will be contained only in the
114     DirectoryChannelLogger class.
115     
116      * Make static final so that the invocation time is minimized.
117      * <p>
118      * This now returns a character array, for performance reasons. The
119      * array's format is in hexidecimal.
120     public static final char[] createCoverString(
121             short methodIndex, short markIndex )
122     {
123         char c[] = new char[10];
124         c[9] = '\n';
125         
126         // unroll the loop
127         c[8] = HEX[ methodIndex & 0xf ]; methodIndex >>= 4;
128         c[7] = HEX[ methodIndex & 0xf ]; methodIndex >>= 4;
129         c[6] = HEX[ methodIndex & 0xf ]; methodIndex >>= 4;
130         c[5] = HEX[ methodIndex & 0xf ];
131         
132         c[4] = ' ';
133         
134         c[3] = HEX[ markIndex & 0xf ]; markIndex >>= 4;
135         c[2] = HEX[ markIndex & 0xf ]; markIndex >>= 4;
136         c[1] = HEX[ markIndex & 0xf ]; markIndex >>= 4;
137         c[0] = HEX[ markIndex & 0xf ];
138         
139         return c;
140     }
141     */

142 }
143
Popular Tags