cc76f371ab1bcb0844aeca977d0e3f6353e0eeab
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / retest / RandomPrefix.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec.xml.retest;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.HashBiMap;
14 import java.net.URI;
15 import java.util.Map;
16 import javax.xml.XMLConstants;
17 import javax.xml.namespace.NamespaceContext;
18
19 /**
20  * @deprecated Used for interim testing, to be removed in near future.
21  */
22 @Deprecated
23 class RandomPrefix {
24     // 32 characters, carefully chosen
25     private static final String LOOKUP = "abcdefghiknoprstABCDEFGHIKNOPRST";
26     private static final int MASK = 0x1f;
27     private static final int SHIFT = 5;
28
29     private int counter = 0;
30
31     // BiMap to make values lookup faster
32     private final BiMap<URI, String> prefixes = HashBiMap.create();
33     private final NamespaceContext context;
34
35     RandomPrefix() {
36         this.context = null;
37     }
38
39     RandomPrefix(final NamespaceContext context) {
40         this.context = Preconditions.checkNotNull(context);
41     }
42
43     Iterable<Map.Entry<URI, String>> getPrefixes() {
44         return prefixes.entrySet();
45     }
46
47     String encodePrefix(final URI namespace) {
48         String prefix = prefixes.get(namespace);
49         if (prefix != null) {
50             return prefix;
51         }
52
53         do {
54             prefix = encode(counter);
55             counter++;
56         } while (alreadyUsedPrefix(prefix));
57
58         prefixes.put(namespace, prefix);
59         return prefix;
60     }
61
62     private boolean alreadyUsedPrefix(final String prefix) {
63         if (context == null) {
64             return false;
65         }
66
67         final String str = context.getNamespaceURI(prefix);
68         return !XMLConstants.NULL_NS_URI.equals(str);
69     }
70
71     @VisibleForTesting
72     static int decode(final String str) {
73         int ret = 0;
74         for (char c : str.toCharArray()) {
75             int idx = LOOKUP.indexOf(c);
76             Preconditions.checkArgument(idx != -1, "Invalid string %s", str);
77             ret = (ret << SHIFT) + idx;
78         }
79
80         return ret;
81     }
82
83     @VisibleForTesting
84     static String encode(int num) {
85         final StringBuilder sb = new StringBuilder();
86
87         do {
88             sb.append(LOOKUP.charAt(num & MASK));
89             num >>>= SHIFT;
90         } while (num != 0);
91
92         return sb.reverse().toString();
93     }
94 }