statoolinfosweb/src/fr/devinsy/statoolinfos/checker/PropertyRule.java

206 lines
4.1 KiB
Java
Raw Normal View History

2020-10-17 17:57:10 +02:00
/*
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of StatoolInfos, simple service statistics tool.
*
* StatoolInfos is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* StatoolInfos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with StatoolInfos. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.statoolinfos.checker;
2020-10-24 01:54:15 +02:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-10-17 17:57:10 +02:00
/**
* The Class PropertyRule.
*/
public class PropertyRule
{
public enum PropertyMode
{
MANDATORY,
WISHED,
OPTIONAL
}
2020-10-24 01:54:15 +02:00
private Pattern pathPattern;
private Pattern valuePattern;
2020-10-17 17:57:10 +02:00
private PropertyMode mode;
/**
* Instantiates a new property rule.
*
2020-10-24 01:54:15 +02:00
* @param pathRegex
2020-10-17 17:57:10 +02:00
* the path
2020-10-24 01:54:15 +02:00
* @param valueRegex
2020-10-17 17:57:10 +02:00
* the pattern
* @param mode
* the mode
*/
2020-10-24 01:54:15 +02:00
public PropertyRule(final String pathRegex, final String valueRegex, final PropertyMode mode)
2020-10-17 17:57:10 +02:00
{
2020-10-24 01:54:15 +02:00
this.pathPattern = Pattern.compile(pathRegex);
this.valuePattern = Pattern.compile(valueRegex);
2020-10-17 17:57:10 +02:00
this.mode = mode;
}
2020-10-24 01:54:15 +02:00
/**
* @param value
* @return
*/
public boolean checkPath(final String value)
{
boolean result;
Matcher matcher = this.pathPattern.matcher(value);
result = matcher.matches();
//
return result;
}
/**
* Check.
*
* @param value
* the value
* @return true, if successful
*/
public boolean checkValue(final String value)
{
boolean result;
Matcher matcher = this.valuePattern.matcher(value);
result = matcher.matches();
//
return result;
}
2020-10-17 17:57:10 +02:00
public PropertyMode getMode()
{
return this.mode;
}
2020-10-24 01:54:15 +02:00
/**
* Gets the path pattern.
*
* @return the path pattern
*/
public String getPathPattern()
2020-10-17 17:57:10 +02:00
{
2020-10-24 01:54:15 +02:00
String result;
result = this.pathPattern.pattern();
//
return result;
2020-10-17 17:57:10 +02:00
}
2020-10-24 01:54:15 +02:00
/**
* Gets the pattern string.
*
* @return the pattern string
*/
public String getValuePattern()
2020-10-17 17:57:10 +02:00
{
2020-10-24 01:54:15 +02:00
String result;
result = this.valuePattern.pattern();
//
return result;
2020-10-17 17:57:10 +02:00
}
/**
* Checks if is mandatory.
*
* @return true, if is mandatory
*/
public boolean isMandatory()
{
boolean result;
if (this.mode == PropertyMode.MANDATORY)
{
result = true;
}
else
{
result = false;
}
//
return result;
}
/**
* Checks if is optional.
*
* @return true, if is optional
*/
public boolean isOptional()
{
boolean result;
if (this.mode == PropertyMode.OPTIONAL)
{
result = true;
}
else
{
result = false;
}
//
return result;
}
/**
* Checks if is wished.
*
* @return true, if is wished
*/
public boolean isWished()
{
boolean result;
if (this.mode == PropertyMode.WISHED)
{
result = true;
}
else
{
result = false;
}
//
return result;
}
public void setMode(final PropertyMode mode)
{
this.mode = mode;
}
2020-10-24 01:54:15 +02:00
/**
* Sets the pattern.
*
* @param valuePattern
* the new pattern
*/
public void setPattern(final String regex)
2020-10-17 17:57:10 +02:00
{
2020-10-24 01:54:15 +02:00
this.valuePattern = Pattern.compile(regex);
2020-10-17 17:57:10 +02:00
}
}