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

org.constretto.model.CPrimitive Maven / Gradle / Ivy

There is a newer version: 3.0.0-BETA4
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy