org.constretto.model.CPrimitive Maven / Gradle / Ivy
package org.constretto.model;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Kaare Nilsen
*/
public class CPrimitive extends CValue {
private static final Pattern VARIABLE_PATTERN = Pattern.compile("#\\{(.*?)}");
private String value;
public CPrimitive(String value) {
if(value == null) {
throw new NullPointerException("The \"value\" argument can not be null");
}
this.value = value;
}
public String value() {
return value;
}
@Override
public Set referencedKeys() {
Set referencedKeys = new HashSet();
Matcher matcher = VARIABLE_PATTERN.matcher(value);
while (matcher.find()) {
String group = matcher.group(1);
referencedKeys.add(group);
}
return referencedKeys;
}
@Override
public void replace(String key, String resolvedValue) {
if (value != null) {
value = value.replaceAll("#\\{" + key + "\\}", Matcher.quoteReplacement(resolvedValue));
}
}
@Override
public String toString() {
return value;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final CPrimitive that = (CPrimitive) o;
if (value != null ? !value.equals(that.value) : that.value != null) return false;
return true;
}
@Override
public int hashCode() {
return value != null ? value.hashCode() : 0;
}
}