[Change the serial number rule] JAVA serial number generation rule (increment according to the default rule, the number is not enough to add letters, and the number of digits is not enough to automatically add 1)

The serial number generation rules write
1. Set the default serial number limit length rule
2. When the serial number reaches the maximum limit, the length is +1, and the serial number starts over from 1.
3. When the incremental part of the serial number reaches 99...9, if it does not exceed the serial number The maximum range of the number length, then increase the letters and add the serial number until the letters are ZZ..Z, the length is +1, and the serial number starts from 1 again
. 4. The serial number can limit the length of the letter running water.

Expected expectation:
running water Number rule:
Maximum length of serial number: 5
letters Limited length: 2

00001~99999
A0001~A9999
....
Z0001~Z9999
ZA001~ZA999
...
ZZ001~ZZ999
000001~999999
 /**
 *
 */
package com.mw.framework.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Project MeWeb
 * @Copyright © 2008-2014 SPRO Technology Consulting Limited. All rights reserved.
 * @fileName com.mw.framework.util.SerialNumberUtils.java
 * @Version 1.0.0
 * @author Allan Ai
 * @time 2014-7-22
 *
 */
public class SerialNumberUtils {
	
	public static String[] sn(int len,Integer i,String driver,String prefix,int limited){
		String[] result = new String[4];
		AtomicInteger z = new AtomicInteger(i);
		AtomicInteger x = new AtomicInteger(i);
		driver = driver==null?"":driver;
		prefix = prefix==null||prefix.equals("null")?"":prefix;
		//1. Determine whether the length of the serial number is greater than the agreed length
		if(z.toString().length()<=(len-driver.length())){
			if(Integer.valueOf(x.incrementAndGet()).toString().length()>(len-driver.length())||(driver.length()>limited && limited!=-1)){
				System.out.println("The default index is out of range, and the serial number needs to be restarted from the letter A");
				//2. If the serial number + letter length exceeds the maximum modification new length
				//len=String.valueOf(x.get()).length();
				int temp  = String.valueOf(x.get()).length();
				
				AtomicInteger atomicInteger = new AtomicInteger(0);
				char [] charArray = driver.toCharArray ();
				
				for (char c : charArray) {
					if(c=='Z'){
						atomicInteger.getAndIncrement();
					}
				}
				
				if((temp+driver.length()>len && atomicInteger.intValue()==driver.length() && driver.length()>0) ||(driver.length()>limited && limited!=-1)){
					System.out.println("If the length of the serial number + letter exceeds the maximum length, modify the new length, and start a new round of serial number + 1");
					len ++;
					driver="";
				}else{
					driver = driverCheck(driver,len,limited);
					if(driver.equals(".N")){
						driver = "";
					}
				}
				z.set(0);
			}
			
			
			
			String current = String.format("%0"+(len-driver.length())+"d",z.incrementAndGet());
			result[0]=prefix+driver+current;
			result[1]=driver;
			result[2]=String.valueOf(len);
			result[3]=String.valueOf(z.get());
			System.out.println(prefix+driver+current);
		}else{
			System.out.println("Illegal input!");
		}
		
		return result;
	}
	
	/**
	 * Letter valid check
	 * 1. Check if the letters are all Z
	 * 2. Check the letter length
	 * @param driver
	 * @param len
	 * @return
	 */
	public static String driverCheck(String driver,int len,int limited){
		if(driver!=null && driver.length()>0){
			AtomicInteger z = new AtomicInteger(0);
			char [] charArray = driver.toCharArray ();
			
			for (char c : charArray) {
				if(c=='Z'){
					z.getAndIncrement();
				}
			}
			
			if(z.intValue()==driver.length()){
				String result = "";
				if(z.intValue()==len && z.intValue()>limited){//If all letters are Z and the length reaches the limited length, return .N
					System.out.println("If all letters are Z and the length reaches the limit, return .N");
					result = ".N";
				}else if(z.intValue()<len-1 && z.intValue()<=limited){
					System.out.println("If all letters are Z, but the length does not reach the limit, directly increment");
					result = driver(driver+"@");
				}
				return result;
			}else{
				System.out.println("If neither of the above two conditions are met, then increment directly");
				return driver(driver);
			}
			/*if(z.intValue()==driver.length() && z.intValue()==len){//If all letters are Z and the length reaches the limit, return .N
				return ".N";
			}else if(z.intValue()==driver.length() && z.intValue()<len){//If all letters are Z, but the length does not reach the limit, add it before calling the letter increment method @ is used to increment A
				System.out.println(z.intValue());
				System.out.println(driver.length());
				return driver2(driver);
				
			}else{//The above two conditions are not satisfied, then increase directly
				return driver2(driver);
			}*/
		}else{
			return "A";
		}
	}
	
	/**
	 * alphabetical increments
	 * @param driver
	 * @return
	 */
	public static String driver(String driver){
		
		if(driver!=null && driver.length()>0){
			char [] charArray = driver.toCharArray ();
			boolean bool = false;
			for(int i = charArray.length-1;i>-1;i--){
				if(charArray[i]=='Z'){
					if(i==charArray.length-1){
						charArray[i]='A';
						bool = true;
					}else{
						if(bool==true){
							AtomicInteger atomic = new AtomicInteger(charArray[i]);
							int val = atomic.incrementAndGet();
							if(90>=val){
								charArray [i] = (char) val;
								bool = false;
							}else{
								charArray[i]='A';
								bool = true;
							}
						}
					}
				}else{
					if(bool==true || (i==charArray.length-1)){
						AtomicInteger atomic = new AtomicInteger(charArray[i]);
						int val = atomic.incrementAndGet();
						if(90>=val){
							charArray [i] = (char) val;
							bool = false;
						}else{
							charArray[i]='A';
							bool = true;
						}
					}
				}
				
			}
			return String.valueOf(charArray);
		}else{
			return "A";
		}
	}
	
 
	public static void main(String[] args) {
		String[] s_= sn(5,9, "ZYAS","",-1);
		for (String string : s_) {
			System.out.println(string);
		}
		
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326488515&siteId=291194637