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

com.github.javaclub.cdl.client.group.ReadWeight Maven / Gradle / Ivy

package com.github.javaclub.cdl.client.group;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadWeight {
	private static final Pattern weightPattern_r = Pattern.compile("[R](\\d*)");

	/**
	 */
	public final int r;

	public ReadWeight(String weightStr) {
		if (weightStr == null) {
			r = 10;
		} else {
			weightStr = weightStr.trim().toUpperCase();
			r = getUnitWeight(weightStr, 'R', weightPattern_r, 0, 10);
		}
	}

	private static int getUnitWeight(String weightStr, char c, Pattern p, int defaultValue1, int defaultValue2) {
		if (weightStr.indexOf(c) == -1) {
			return defaultValue1;
		} else {
			Matcher m = p.matcher(weightStr);
			m.find();

			if (m.group(1).length() == 0) {
				return defaultValue2;
			} else {
				return Integer.parseInt(m.group(1));
			}
		}
	}

	@Override
	public String toString() {
		return "Weight [r=" + r + "]";
	}

	public static void main(String[] args) {
		System.out.println(new ReadWeight("wr0i1"));
		System.out.println(new ReadWeight("wr0i0I1"));
		System.out.println(new ReadWeight("i0w10I1r20"));
		System.out.println(new ReadWeight("i0w10I1r20i3"));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy