2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
11 import java.util.HashMap;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ThreadLocalRandom;
16 import org.opendaylight.yangtools.yang.common.QName;
18 final class RandomPrefix {
19 final Map<URI, String> prefixes = new HashMap<>();
21 Iterable<Entry<URI, String>> getPrefixes() {
22 return prefixes.entrySet();
25 String encodeQName(final QName qname) {
26 String prefix = prefixes.get(qname.getNamespace());
28 prefix = qname.getPrefix();
29 if (prefix == null || prefix.isEmpty() || prefixes.containsValue(prefix)) {
30 final ThreadLocalRandom random = ThreadLocalRandom.current();
32 final StringBuilder sb = new StringBuilder();
33 for (int i = 0; i < 4; i++) {
34 sb.append((char)('a' + random.nextInt(25)));
37 prefix = sb.toString();
38 } while (prefixes.containsValue(prefix));
41 prefixes.put(qname.getNamespace(), prefix);
44 return prefix + ':' + qname.getLocalName();