Here you can find the source of createUniqueName(String baseName, Collection
Parameter | Description |
---|---|
baseName | - The default name to give. |
names | - The names to compare with each other. |
public static String createUniqueName(String baseName, Collection<String> names)
//package com.java2s; //License from project: Open Source License import java.util.Collection; public class Main { /**/* ww w. jav a 2 s. c om*/ * Generate a unique name based on the given name and the name of already existing elements. * * @param baseName - The default name to give. * @param names - The names to compare with each other. * @return The newly created name. Of form baseName{int} */ public static String createUniqueName(String baseName, Collection<String> names) { String uniqueName = baseName; int idx = -1; for (String name : names) { if (!name.startsWith(baseName)) { continue; } String suffix = name.substring(baseName.length()); if (suffix.isEmpty()) { idx = Math.max(idx, 0); } else if (suffix.matches("\\d+")) { idx = Math.max(idx, Integer.parseInt(suffix)); } } if (idx < 0) { return uniqueName; } return uniqueName + (idx + 1); } }