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

com.softicar.platform.common.core.entity.EntityIdFromDisplayStringExtractor Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.core.entity;

import com.softicar.platform.common.core.i18n.IDisplayString;
import com.softicar.platform.common.core.number.parser.IntegerParser;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * Extractor for the {@link IEntity} ID from a given {@link IDisplayString}.
 *
 * @author Oliver Richers
 */
public class EntityIdFromDisplayStringExtractor {

	private static final Pattern PATTERN = Pattern.compile(".*\\[([0-9]+)\\]");
	private final String displayString;

	public EntityIdFromDisplayStringExtractor(IDisplayString displayString) {

		this(displayString.toString());
	}

	public EntityIdFromDisplayStringExtractor(String displayString) {

		this.displayString = displayString;
	}

	/**
	 * Returns the extracted ID.
	 * 

* If no ID could be extracted, an empty {@link Optional} is returned. * * @return the optionally extracted ID */ public Optional extractId() { var matcher = PATTERN.matcher(displayString); if (matcher.matches()) { return IntegerParser.parse(matcher.group(1)); } else { return Optional.empty(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy