@Test public void testItNowBye() { Map<String, String> ret = PsbAppServiceIdGenerator.generatePsbAppServiceIdsByAppSvcName( "cool app", new HashSet<>( Arrays.asList("svc a", "svc b", "svc c")), 12 + 11 ); ret.values().forEach(s -> Assert.assertTrue(s + " too long", s.length() <= 12 + 11)); Assert.assertEquals("cool-a-svc-a", ret.get("svc a").substring(0, "cool-a-svc-a".length())); Assert.assertEquals("cool-a-svc-b", ret.get("svc b").substring(0, "cool-a-svc-b".length())); Assert.assertEquals("cool-a-svc-c", ret.get("svc c").substring(0, "cool-a-svc-c".length())); ret = PsbAppServiceIdGenerator.generatePsbAppServiceIdsByAppSvcName( "cool app", new HashSet<>( Arrays.asList("svc long test name a", "svc long test name b")), 12 + 11 ); ret.values().forEach(s -> Assert.assertTrue(s + " too long", s.length() <= 12 + 11)); System.out.println(ret); Assert.assertEquals("cool-a-svc-l", ret.get("svc long test name a").substring(0, "cool-a-svc-l".length())); Assert.assertEquals("cool-a-svc-1", ret.get("svc long test name b").substring(0, "cool-a-svc-1".length())); }
public static Map<String, String> generatePsbAppServiceIdsByAppSvcName( final String appInstanceName, final Set<String> appServiceNames, int psbMaxAppIdLength) { Map<String, String> psbAppServiceIds = new HashMap<>(appServiceNames.size()); String timeSuffix = String.valueOf(System.currentTimeMillis()); if (timeSuffix.length() > 11) { timeSuffix = timeSuffix.substring(timeSuffix.length() - 11); } int charactersLeftToUse = psbMaxAppIdLength - timeSuffix.length() - 1; final String encodedAppInstanceName = encodeName(appInstanceName); final int appInstanceNameLength = encodedAppInstanceName.length(); for (String currAppServiceName : appServiceNames) { final String encodedAppServiceName = encodeName(currAppServiceName); if (appInstanceNameLength + encodedAppServiceName.length() > charactersLeftToUse) { if (appServiceNames.size() == 1) { final String singleServicePsbAppId = encodedAppInstanceName + "-" + encodedAppServiceName; String appNameAndSvcName = singleServicePsbAppId .substring( 0, Math.min( singleServicePsbAppId.length(), psbMaxAppIdLength - timeSuffix.length())); psbAppServiceIds.put( currAppServiceName, appNameAndSvcName + timeSuffix); } else { StringBuilder appInstanceNamePrefix = new StringBuilder( encodedAppInstanceName .substring( 0, Math.min( encodedAppInstanceName.length(), charactersLeftToUse / 2 + charactersLeftToUse % 2) )); if (appInstanceNamePrefix.charAt(appInstanceNamePrefix.length() - 1) != '-') { appInstanceNamePrefix.append('-'); } String psbAppServiceIdAttempt = appInstanceNamePrefix.append( encodedAppServiceName .substring( 0, Math.min( charactersLeftToUse / 2, encodedAppServiceName.length()))).toString(); int duplicateIdx = 1; while (psbAppServiceIds.containsValue(psbAppServiceIdAttempt + timeSuffix)) { psbAppServiceIdAttempt = psbAppServiceIdAttempt .substring(0, psbAppServiceIdAttempt.length() - String.valueOf(duplicateIdx).length()) + String.valueOf(duplicateIdx); duplicateIdx++; } psbAppServiceIds.put(currAppServiceName, psbAppServiceIdAttempt + timeSuffix); } } else { psbAppServiceIds.put( currAppServiceName, encodedAppInstanceName + "-" + encodedAppServiceName + timeSuffix); } } return psbAppServiceIds; }
PsbAppServiceIdGenerator { public static Map<String, String> generatePsbAppServiceIdsByAppSvcName( final String appInstanceName, final Set<String> appServiceNames, int psbMaxAppIdLength) { Map<String, String> psbAppServiceIds = new HashMap<>(appServiceNames.size()); String timeSuffix = String.valueOf(System.currentTimeMillis()); if (timeSuffix.length() > 11) { timeSuffix = timeSuffix.substring(timeSuffix.length() - 11); } int charactersLeftToUse = psbMaxAppIdLength - timeSuffix.length() - 1; final String encodedAppInstanceName = encodeName(appInstanceName); final int appInstanceNameLength = encodedAppInstanceName.length(); for (String currAppServiceName : appServiceNames) { final String encodedAppServiceName = encodeName(currAppServiceName); if (appInstanceNameLength + encodedAppServiceName.length() > charactersLeftToUse) { if (appServiceNames.size() == 1) { final String singleServicePsbAppId = encodedAppInstanceName + "-" + encodedAppServiceName; String appNameAndSvcName = singleServicePsbAppId .substring( 0, Math.min( singleServicePsbAppId.length(), psbMaxAppIdLength - timeSuffix.length())); psbAppServiceIds.put( currAppServiceName, appNameAndSvcName + timeSuffix); } else { StringBuilder appInstanceNamePrefix = new StringBuilder( encodedAppInstanceName .substring( 0, Math.min( encodedAppInstanceName.length(), charactersLeftToUse / 2 + charactersLeftToUse % 2) )); if (appInstanceNamePrefix.charAt(appInstanceNamePrefix.length() - 1) != '-') { appInstanceNamePrefix.append('-'); } String psbAppServiceIdAttempt = appInstanceNamePrefix.append( encodedAppServiceName .substring( 0, Math.min( charactersLeftToUse / 2, encodedAppServiceName.length()))).toString(); int duplicateIdx = 1; while (psbAppServiceIds.containsValue(psbAppServiceIdAttempt + timeSuffix)) { psbAppServiceIdAttempt = psbAppServiceIdAttempt .substring(0, psbAppServiceIdAttempt.length() - String.valueOf(duplicateIdx).length()) + String.valueOf(duplicateIdx); duplicateIdx++; } psbAppServiceIds.put(currAppServiceName, psbAppServiceIdAttempt + timeSuffix); } } else { psbAppServiceIds.put( currAppServiceName, encodedAppInstanceName + "-" + encodedAppServiceName + timeSuffix); } } return psbAppServiceIds; } }
PsbAppServiceIdGenerator { public static Map<String, String> generatePsbAppServiceIdsByAppSvcName( final String appInstanceName, final Set<String> appServiceNames, int psbMaxAppIdLength) { Map<String, String> psbAppServiceIds = new HashMap<>(appServiceNames.size()); String timeSuffix = String.valueOf(System.currentTimeMillis()); if (timeSuffix.length() > 11) { timeSuffix = timeSuffix.substring(timeSuffix.length() - 11); } int charactersLeftToUse = psbMaxAppIdLength - timeSuffix.length() - 1; final String encodedAppInstanceName = encodeName(appInstanceName); final int appInstanceNameLength = encodedAppInstanceName.length(); for (String currAppServiceName : appServiceNames) { final String encodedAppServiceName = encodeName(currAppServiceName); if (appInstanceNameLength + encodedAppServiceName.length() > charactersLeftToUse) { if (appServiceNames.size() == 1) { final String singleServicePsbAppId = encodedAppInstanceName + "-" + encodedAppServiceName; String appNameAndSvcName = singleServicePsbAppId .substring( 0, Math.min( singleServicePsbAppId.length(), psbMaxAppIdLength - timeSuffix.length())); psbAppServiceIds.put( currAppServiceName, appNameAndSvcName + timeSuffix); } else { StringBuilder appInstanceNamePrefix = new StringBuilder( encodedAppInstanceName .substring( 0, Math.min( encodedAppInstanceName.length(), charactersLeftToUse / 2 + charactersLeftToUse % 2) )); if (appInstanceNamePrefix.charAt(appInstanceNamePrefix.length() - 1) != '-') { appInstanceNamePrefix.append('-'); } String psbAppServiceIdAttempt = appInstanceNamePrefix.append( encodedAppServiceName .substring( 0, Math.min( charactersLeftToUse / 2, encodedAppServiceName.length()))).toString(); int duplicateIdx = 1; while (psbAppServiceIds.containsValue(psbAppServiceIdAttempt + timeSuffix)) { psbAppServiceIdAttempt = psbAppServiceIdAttempt .substring(0, psbAppServiceIdAttempt.length() - String.valueOf(duplicateIdx).length()) + String.valueOf(duplicateIdx); duplicateIdx++; } psbAppServiceIds.put(currAppServiceName, psbAppServiceIdAttempt + timeSuffix); } } else { psbAppServiceIds.put( currAppServiceName, encodedAppInstanceName + "-" + encodedAppServiceName + timeSuffix); } } return psbAppServiceIds; } private  PsbAppServiceIdGenerator(); }
PsbAppServiceIdGenerator { public static Map<String, String> generatePsbAppServiceIdsByAppSvcName( final String appInstanceName, final Set<String> appServiceNames, int psbMaxAppIdLength) { Map<String, String> psbAppServiceIds = new HashMap<>(appServiceNames.size()); String timeSuffix = String.valueOf(System.currentTimeMillis()); if (timeSuffix.length() > 11) { timeSuffix = timeSuffix.substring(timeSuffix.length() - 11); } int charactersLeftToUse = psbMaxAppIdLength - timeSuffix.length() - 1; final String encodedAppInstanceName = encodeName(appInstanceName); final int appInstanceNameLength = encodedAppInstanceName.length(); for (String currAppServiceName : appServiceNames) { final String encodedAppServiceName = encodeName(currAppServiceName); if (appInstanceNameLength + encodedAppServiceName.length() > charactersLeftToUse) { if (appServiceNames.size() == 1) { final String singleServicePsbAppId = encodedAppInstanceName + "-" + encodedAppServiceName; String appNameAndSvcName = singleServicePsbAppId .substring( 0, Math.min( singleServicePsbAppId.length(), psbMaxAppIdLength - timeSuffix.length())); psbAppServiceIds.put( currAppServiceName, appNameAndSvcName + timeSuffix); } else { StringBuilder appInstanceNamePrefix = new StringBuilder( encodedAppInstanceName .substring( 0, Math.min( encodedAppInstanceName.length(), charactersLeftToUse / 2 + charactersLeftToUse % 2) )); if (appInstanceNamePrefix.charAt(appInstanceNamePrefix.length() - 1) != '-') { appInstanceNamePrefix.append('-'); } String psbAppServiceIdAttempt = appInstanceNamePrefix.append( encodedAppServiceName .substring( 0, Math.min( charactersLeftToUse / 2, encodedAppServiceName.length()))).toString(); int duplicateIdx = 1; while (psbAppServiceIds.containsValue(psbAppServiceIdAttempt + timeSuffix)) { psbAppServiceIdAttempt = psbAppServiceIdAttempt .substring(0, psbAppServiceIdAttempt.length() - String.valueOf(duplicateIdx).length()) + String.valueOf(duplicateIdx); duplicateIdx++; } psbAppServiceIds.put(currAppServiceName, psbAppServiceIdAttempt + timeSuffix); } } else { psbAppServiceIds.put( currAppServiceName, encodedAppInstanceName + "-" + encodedAppServiceName + timeSuffix); } } return psbAppServiceIds; } private  PsbAppServiceIdGenerator(); static Map<String, String> generatePsbAppServiceIdsByAppSvcName(
            final String appInstanceName,
            final Set<String> appServiceNames,
            int psbMaxAppIdLength); }
PsbAppServiceIdGenerator { public static Map<String, String> generatePsbAppServiceIdsByAppSvcName( final String appInstanceName, final Set<String> appServiceNames, int psbMaxAppIdLength) { Map<String, String> psbAppServiceIds = new HashMap<>(appServiceNames.size()); String timeSuffix = String.valueOf(System.currentTimeMillis()); if (timeSuffix.length() > 11) { timeSuffix = timeSuffix.substring(timeSuffix.length() - 11); } int charactersLeftToUse = psbMaxAppIdLength - timeSuffix.length() - 1; final String encodedAppInstanceName = encodeName(appInstanceName); final int appInstanceNameLength = encodedAppInstanceName.length(); for (String currAppServiceName : appServiceNames) { final String encodedAppServiceName = encodeName(currAppServiceName); if (appInstanceNameLength + encodedAppServiceName.length() > charactersLeftToUse) { if (appServiceNames.size() == 1) { final String singleServicePsbAppId = encodedAppInstanceName + "-" + encodedAppServiceName; String appNameAndSvcName = singleServicePsbAppId .substring( 0, Math.min( singleServicePsbAppId.length(), psbMaxAppIdLength - timeSuffix.length())); psbAppServiceIds.put( currAppServiceName, appNameAndSvcName + timeSuffix); } else { StringBuilder appInstanceNamePrefix = new StringBuilder( encodedAppInstanceName .substring( 0, Math.min( encodedAppInstanceName.length(), charactersLeftToUse / 2 + charactersLeftToUse % 2) )); if (appInstanceNamePrefix.charAt(appInstanceNamePrefix.length() - 1) != '-') { appInstanceNamePrefix.append('-'); } String psbAppServiceIdAttempt = appInstanceNamePrefix.append( encodedAppServiceName .substring( 0, Math.min( charactersLeftToUse / 2, encodedAppServiceName.length()))).toString(); int duplicateIdx = 1; while (psbAppServiceIds.containsValue(psbAppServiceIdAttempt + timeSuffix)) { psbAppServiceIdAttempt = psbAppServiceIdAttempt .substring(0, psbAppServiceIdAttempt.length() - String.valueOf(duplicateIdx).length()) + String.valueOf(duplicateIdx); duplicateIdx++; } psbAppServiceIds.put(currAppServiceName, psbAppServiceIdAttempt + timeSuffix); } } else { psbAppServiceIds.put( currAppServiceName, encodedAppInstanceName + "-" + encodedAppServiceName + timeSuffix); } } return psbAppServiceIds; } private  PsbAppServiceIdGenerator(); static Map<String, String> generatePsbAppServiceIdsByAppSvcName(
            final String appInstanceName,
            final Set<String> appServiceNames,
            int psbMaxAppIdLength);  }