EMMA Coverage Report (generated Mon Aug 23 17:21:34 CEST 2010)
[all classes][diskCacheV111.util]

COVERAGE SUMMARY FOR SOURCE FILE [PnfsId.java]

nameclass, %method, %block, %line, %
PnfsId.java0%   (0/1)0%   (0/24)0%   (0/794)0%   (0/129)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PnfsId0%   (0/1)0%   (0/24)0%   (0/794)0%   (0/129)
<static initializer> 0%   (0/1)0%   (0/71)0%   (0/2)
PnfsId (String): void 0%   (0/1)0%   (0/7)0%   (0/2)
PnfsId (String, String): void 0%   (0/1)0%   (0/6)0%   (0/2)
PnfsId (byte []): void 0%   (0/1)0%   (0/5)0%   (0/2)
PnfsId (byte [], String): void 0%   (0/1)0%   (0/61)0%   (0/13)
_stringToBytes (String): byte [] 0%   (0/1)0%   (0/168)0%   (0/35)
byteToHexString (byte): String 0%   (0/1)0%   (0/25)0%   (0/4)
bytesToHexString (byte []): String 0%   (0/1)0%   (0/49)0%   (0/6)
compareTo (PnfsId): int 0%   (0/1)0%   (0/73)0%   (0/8)
equals (Object): boolean 0%   (0/1)0%   (0/37)0%   (0/6)
getBytes (): byte [] 0%   (0/1)0%   (0/16)0%   (0/3)
getDatabaseId (): int 0%   (0/1)0%   (0/16)0%   (0/1)
getDomain (): String 0%   (0/1)0%   (0/3)0%   (0/1)
getId (): String 0%   (0/1)0%   (0/4)0%   (0/1)
hashCode (): int 0%   (0/1)0%   (0/13)0%   (0/1)
isValid (String): boolean 0%   (0/1)0%   (0/7)0%   (0/2)
main (String []): void 0%   (0/1)0%   (0/104)0%   (0/17)
stringToDomain (String): String 0%   (0/1)0%   (0/20)0%   (0/4)
stringToId (String): byte [] 0%   (0/1)0%   (0/15)0%   (0/4)
toBinPnfsId (): byte [] 0%   (0/1)0%   (0/13)0%   (0/4)
toCompleteId (String): String 0%   (0/1)0%   (0/4)0%   (0/1)
toIdString (): String 0%   (0/1)0%   (0/3)0%   (0/1)
toShortString (): String 0%   (0/1)0%   (0/51)0%   (0/8)
toString (): String 0%   (0/1)0%   (0/23)0%   (0/1)

1// $Id: PnfsId.java 14245 2010-07-20 09:36:48Z pmillar $
2 
3package diskCacheV111.util;
4 
5import java.io.Serializable;
6import java.util.Arrays;
7import java.util.regex.Matcher;
8import java.util.regex.Pattern;
9 
10/**
11 * Immutable representation of a pnfsId
12 */
13public class PnfsId implements Serializable, Comparable<PnfsId> {
14 
15    private static final String SIMPLE_PNFS_ID_REGEX = "\\p{XDigit}{1,24}";
16    // FIXME STAR_PNFS_ID_REGEX is too permissive; overall length should be 24 or less.
17    private static final String STAR_PNFS_ID_REGEX = "\\p{XDigit}{1,22}\\*\\p{XDigit}{1,22}";
18    private static final String PNFS_ID_REGEX = "("+SIMPLE_PNFS_ID_REGEX+"|"+STAR_PNFS_ID_REGEX+")";
19    private static final String PNFS_STRING_REGEX = "("+PNFS_ID_REGEX+"(\\..*)?)";
20    private static final String CHIMERA_ID_REGEX = "\\p{XDigit}{36}";
21    private static final String VALID_ID_REGEX = "^("+PNFS_STRING_REGEX+"|"+CHIMERA_ID_REGEX+")$";
22    private static final Pattern VALID_ID_PATTERN = Pattern.compile( VALID_ID_REGEX);
23 
24    private final static int OLD_ID_SIZE = 12; // original pnfs
25    private final static int NEW_ID_SIZE = 18; // chimera
26 
27    private final byte[] _a;
28    @Deprecated
29    private final String _idString;
30    private final String _domain;
31    @Deprecated
32    private final String _toString;
33 
34    private static final long serialVersionUID = -112220393521303857L;
35 
36    public static boolean isValid( String id) {
37        Matcher m = VALID_ID_PATTERN.matcher( id);
38        return m.matches();
39    }
40 
41    public PnfsId(String id, String domain) {
42        this(_stringToBytes(id), domain);
43    }
44 
45    public PnfsId(String s) {
46        this(stringToId(s), stringToDomain(s));
47    }
48 
49    public PnfsId(byte[] id) {
50        this(id, null);
51    }
52 
53    public PnfsId(byte[] id, String domain) {
54        int length = id.length;
55        if (length != OLD_ID_SIZE && length != NEW_ID_SIZE) {
56            throw new IllegalArgumentException("Illegal pnfsid string length");
57        }
58        _a = new byte[length];
59        System.arraycopy(id, 0, _a, 0, length);
60 
61        _idString = bytesToHexString(_a);
62        if (domain != null) {
63            _domain = domain.intern();
64            _toString = _idString + "." + _domain;
65        } else {
66            _domain = null;
67            _toString = _idString;
68        }
69    }
70 
71    @Override
72    public boolean equals(Object o) {
73        if (this == o) {
74            return true;
75        }
76 
77        if (!(o instanceof PnfsId)) {
78            return false;
79        }
80 
81        PnfsId other = (PnfsId) o;
82        return Arrays.equals(_a, other._a)
83            && (_domain == other._domain
84                || (_domain != null && _domain.equals(other._domain)));
85    }
86 
87    @Override
88    public int hashCode() {
89        return Arrays.hashCode(_a) ^ ((_domain == null) ? 0 : _domain.hashCode());
90    }
91 
92    public int compareTo(PnfsId pnfsId) {
93        if( pnfsId == this ) return 0;
94 
95        int i = 0;
96        for (i = 0; (i < _a.length) && (_a[i] == pnfsId._a[i]); i++)
97            ;
98        if (i == _a.length)
99            return 0;
100        int t = _a[i] < 0 ? 256 + _a[i] : _a[i];
101        int o = pnfsId._a[i] < 0 ? 256 + pnfsId._a[i] : pnfsId._a[i];
102 
103        return t < o ? -1 : 1;
104    }
105 
106    public int getDatabaseId() {
107        return (((_a[0]) & 0xFF) << 8) | ((_a[1]) & 0xFF);
108    }
109 
110    public String getDomain() {
111        return _domain;
112    }
113 
114    public String getId() {
115        return bytesToHexString(_a);
116    }
117 
118    @Override
119    public String toString() {
120        return getId() + ((_domain != null) ? "." + _domain : "");
121    }
122 
123    public String toIdString() {
124        return getId();
125    }
126 
127    public static String toCompleteId(String shortId) {
128        return bytesToHexString(_stringToBytes(shortId));
129    }
130 
131    public byte[] getBytes() {
132        byte[] x = new byte[_a.length];
133        System.arraycopy(_a, 0, x, 0, _a.length);
134        return x;
135    }
136 
137    public String toShortString() {
138        StringBuilder sb = new StringBuilder();
139        int i = 0;
140        for (i = 0; i < 2; i++)
141            sb.append(byteToHexString(_a[i]));
142        for (; (i < _a.length) && (_a[i] == 0); i++)
143            ;
144        for (; i < _a.length; i++)
145            sb.append(byteToHexString(_a[i]));
146        return sb.toString();
147    }
148 
149    private static final byte[] stringToId(String s) {
150        int i = s.indexOf('.');
151        if (i < 0) {
152            return  _stringToBytes(s);
153        } else {
154            return _stringToBytes(s.substring(0, i));
155        }
156    }
157 
158    private static final String stringToDomain(String s) {
159        int i = s.indexOf('.');
160        if (i < 0 || i == s.length() - 1) {
161            return null;
162        } else {
163            return s.substring(i + 1);
164        }
165    }
166 
167    private static String byteToHexString(byte b) {
168        String s = Integer.toHexString((b < 0) ? (256 + b) : (int) b)
169            .toUpperCase();
170        if (s.length() == 1)
171            return "0" + s;
172        else
173            return s;
174    }
175 
176    /**
177     * Translation table used by bytesToHexString.
178     */
179    private static final char valueToHex[] = {
180        '0', '1', '2', '3', '4', '5',
181        '6', '7', '8', '9', 'A', 'B',
182        'C', 'D', 'E', 'F' };
183 
184    /**
185     * Converts a byte array into the string representation in base 16.
186     */
187    private static String bytesToHexString(byte[] b) {
188        char result[] = new char[2 * b.length];
189        for (int i = 0; i < b.length; i++) {
190            int value = (b[i] + 0x100) & 0xFF;
191            result[2 * i] = valueToHex[value >> 4];
192            result[2 * i + 1] = valueToHex[value & 0x0F];
193        }
194        return new String(result);
195    }
196 
197    private static byte[] _stringToBytes(String idString) {
198 
199        int len = idString.length();
200        int idSize = 0;
201 
202        switch (len) {
203        case OLD_ID_SIZE * 2: // old pnfsid
204            idSize = OLD_ID_SIZE;
205            break;
206        case NEW_ID_SIZE * 2: // himera
207            idSize = NEW_ID_SIZE;
208            break;
209        default:
210            // all id's shorter than 24 characters will be extended to 24
211            if ((len > OLD_ID_SIZE * 2) || (len == 0)) {
212                throw new IllegalArgumentException(
213                                                   "Illegal pnfsid string length");
214            }
215            idSize = OLD_ID_SIZE;
216        }
217 
218        byte[] a = new byte[idSize];
219        int p = idString.indexOf('*');
220        if (p > -1) {
221            if ((p == 0) || (p == (idString.length() - 1))) {
222                throw new IllegalArgumentException("Illegal use of *");
223            }
224            int diff = 2 * OLD_ID_SIZE - idString.length() + 1;
225            StringBuilder sb = new StringBuilder();
226            sb.append(idString.substring(0, p));
227            for (int i = 0; i < diff; i++)
228                sb.append("0");
229            sb.append(idString.substring(p + 1));
230            idString = sb.toString();
231        }
232        if (idString.length() > (2 * a.length)) {
233            throw new IllegalArgumentException("Illegal pnfsid string length");
234        } else if (idString.length() < (2 * a.length)) {
235            StringBuilder sb = new StringBuilder();
236            int m = 2 * OLD_ID_SIZE - idString.length();
237            for (int i = 0; i < m; i++)
238                sb.append("0");
239            sb.append(idString);
240            idString = sb.toString();
241        }
242        for (int i = 0; i < idSize; i++) {
243            int l = Integer
244                .parseInt(idString.substring(2 * i, 2 * (i + 1)), 16);
245            a[i] = (byte) ((l < 128) ? l : (l - 256));
246        }
247        return a;
248    }
249 
250    /**
251     * Converts string representation of pnfsid into its internal binary form
252     *
253     * @return pnfsid as byte array
254     */
255    public byte[] toBinPnfsId() {
256        switch (_a.length) {
257        case OLD_ID_SIZE: // old pnfsid
258            return PnfsIdUtil.toBinPnfsId(getId());
259        case NEW_ID_SIZE: // himera
260            return getBytes();
261        default:
262            return null;
263        }
264    }
265 
266    public static void main(String[] args) {
267        if (args.length < 1) {
268            System.out.println("USAGE : ... <pnfsId>");
269            System.exit(4);
270        }
271        try {
272            PnfsId id = new PnfsId(args[0]);
273            System.out.println("id.toString()      " + id);
274            System.out.println("id.getId()         " + id.getId());
275            System.out.println("db.getDatabaseId() " + id.getDatabaseId());
276            System.out.println("db.getDomain()     " + id.getDomain());
277            System.out.println("id.getBytes()      " + java.util.Arrays.toString(id.getBytes()));
278            System.out.println("id.toBinPnfsId()   " + java.util.Arrays.toString(id.toBinPnfsId()));
279            System.out.println("toStringPnfsId(id.toBinPnfsId()) " + PnfsIdUtil.toStringPnfsId(id.toBinPnfsId()));
280            System.exit(0);
281        } catch (Exception e) {
282            e.printStackTrace();
283            System.exit(4);
284        }
285    }
286}

[all classes][diskCacheV111.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov