All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dslplatform.json.processor.MinifiedNaming Maven / Gradle / Ivy

The newest version!
package com.dslplatform.json.processor;

import java.util.*;

public class MinifiedNaming implements NamingStrategy {

	private static String buildShortName(String name, Set names, Map counters) {
		String shortName = name.substring(0, 1);
		Character first = name.charAt(0);
		if (!names.contains(shortName)) {
			names.add(shortName);
			counters.put(first, 0);
			return shortName;
		}
		Integer next = counters.get(first);
		if (next == null) {
			next = 0;
		}
		do {
			shortName = first.toString() + next;
			next++;
		} while (names.contains(shortName));
		counters.put(first, next);
		names.add(shortName);
		return shortName;
	}

	public Map prepareNames(Map attributes) {
		Map result = new LinkedHashMap();
		Map counters = new HashMap();
		Set processed = new HashSet();
		Set names = new HashSet();
		for (AttributeInfo p : attributes.values()) {
			if (p.alias != null) {
				result.put(p.id, p.alias);
				processed.add(p.id);
				names.add(p.alias);
			}
		}
		for (AttributeInfo p : attributes.values()) {
			if (processed.contains(p.id)) {
				continue;
			}
			String shortName = buildShortName(p.id, names, counters);
			result.put(p.id, shortName);
		}
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy