Skip to content

Package: UUIDEncodingAlgorithm

UUIDEncodingAlgorithm

nameinstructionbranchcomplexitylinemethod
UUIDEncodingAlgorithm()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
convertFromCharacters(char[], int, int)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
convertToCharacters(Object, StringBuffer)
M: 46 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
digits(long, int)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
fromUUIDString(String)
M: 104 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
getPrimtiveLengthFromOctetLength(int)
M: 14 C: 8
36%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 2
50%
M: 0 C: 1
100%
toUUIDString(long, long)
M: 44 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3: *
4: * Copyright (c) 2004-2018 Oracle and/or its affiliates. All rights reserved.
5: *
6: * Oracle licenses this file to You under the Apache License, Version 2.0
7: * (the "License"); you may not use this file except in compliance with
8: * the License. You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.sun.xml.fastinfoset.algorithm;
20:
21: import java.util.ArrayList;
22: import java.util.List;
23: import java.nio.CharBuffer;
24: import org.jvnet.fastinfoset.EncodingAlgorithmException;
25: import com.sun.xml.fastinfoset.CommonResourceBundle;
26:
27: public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm {
28:
29: public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
30:• if (octetLength % (LONG_SIZE * 2) != 0) {
31: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
32: getString("message.lengthNotMultipleOfUUID",new Object[]{Integer.valueOf(LONG_SIZE * 2)}));
33: }
34:
35: return octetLength / LONG_SIZE;
36: }
37:
38: public final Object convertFromCharacters(char[] ch, int start, int length) {
39: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
40: final List longList = new ArrayList();
41:
42: matchWhiteSpaceDelimnatedWords(cb,
43: new WordListener() {
44: public void word(int start, int end) {
45: String uuidValue = cb.subSequence(start, end).toString();
46: fromUUIDString(uuidValue);
47: longList.add(Long.valueOf(_msb));
48: longList.add(Long.valueOf(_lsb));
49: }
50: }
51: );
52:
53: return generateArrayFromList(longList);
54: }
55:
56: public final void convertToCharacters(Object data, StringBuffer s) {
57:• if (!(data instanceof long[])) {
58: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray"));
59: }
60:
61: final long[] ldata = (long[])data;
62:
63: final int end = ldata.length - 2;
64:• for (int i = 0; i <= end; i += 2) {
65: s.append(toUUIDString(ldata[i], ldata[i + 1]));
66:• if (i != end) {
67: s.append(' ');
68: }
69: }
70: }
71:
72:
73: private long _msb;
74: private long _lsb;
75:
76: final void fromUUIDString(String name) {
77: String[] components = name.split("-");
78:• if (components.length != 5)
79: throw new IllegalArgumentException(CommonResourceBundle.getInstance().
80: getString("message.invalidUUID", new Object[]{name}));
81:
82:• for (int i=0; i<5; i++)
83: components[i] = "0x"+components[i];
84:
85: _msb = Long.parseLong(components[0], 16);
86: _msb <<= 16;
87: _msb |= Long.parseLong(components[1], 16);
88: _msb <<= 16;
89: _msb |= Long.parseLong(components[2], 16);
90:
91: _lsb = Long.parseLong(components[3], 16);
92: _lsb <<= 48;
93: _lsb |= Long.parseLong(components[4], 16);
94: }
95:
96: final String toUUIDString(long msb, long lsb) {
97:         return (digits(msb >> 32, 8) + "-" +
98:                 digits(msb >> 16, 4) + "-" +
99:                 digits(msb, 4) + "-" +
100:                 digits(lsb >> 48, 4) + "-" +
101:                 digits(lsb, 12));
102: }
103:
104: final String digits(long val, int digits) {
105:         long hi = 1L << (digits * 4);
106:         return Long.toHexString(hi | (val & (hi - 1))).substring(1);
107: }
108:
109: }