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.controller.xml.codec;
10 import org.opendaylight.yangtools.yang.common.QName;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
16 import java.util.concurrent.ThreadLocalRandom;
19 * Picked from Yang tools because it was not public class
22 final class RandomPrefix {
23 final Map<URI, String> prefixes = new HashMap<>();
25 Iterable<Entry<URI, String>> getPrefixes() {
26 return prefixes.entrySet();
29 String encodeQName(final QName qname) {
30 String prefix = prefixes.get(qname.getNamespace());
32 final ThreadLocalRandom random = ThreadLocalRandom.current();
34 final StringBuilder sb = new StringBuilder();
35 for (int i = 0; i < 4; i++) {
36 sb.append((char)('a' + random.nextInt(25)));
39 prefix = sb.toString();
40 } while (prefixes.containsValue(prefix));
41 prefixes.put(qname.getNamespace(), prefix);
44 return prefix + ':' + qname.getLocalName();