001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023
002package io.cui.tools.formatting.template.token;
003
004import static io.cui.tools.base.Preconditions.checkArgument;
005import static java.util.Objects.requireNonNull;
006import java.io.Serializable;
007import java.util.Arrays;
008import java.util.List;
009import java.util.Map;
010import io.cui.tools.formatting.template.FormatterSupport;
011
012/**
013 * String Action Token store template and attribute name, and replace attribute with his value
014 * inside the template on execution of {@linkplain #substituteAttribute(FormatterSupport)}
015 *
016 * @author Eugen Fischer
017 */
018public class ActionToken implements Token {
019    private static final long serialVersionUID = -6329721490557755853L;
020    private final String before;
021    private final String attribute;
022    private final String after;
023
024    /**
025     * @param template
026     * @param token
027     */
028    public ActionToken(final String template, final String token) {
029        checkArgument(template.contains(token), "\'" + template + " must contain \'" + token + "\'");
030        final List<String> splitted = Arrays.asList(template.split(token));
031        before = extractSurrounding(splitted, 0);
032        attribute = token;
033        after = extractSurrounding(splitted, 1);
034    }
035
036    private static String extractSurrounding(final List<String> splitted, final int index) {
037        var result = "";
038        if (!splitted.isEmpty() && splitted.size() > index) {
039            result = splitted.get(index);
040        }
041        return result;
042    }
043
044    @Override
045    public String substituteAttribute(final FormatterSupport content) {
046        requireNonNull(content, "Content must not be null. ");
047        final Map<String, Serializable> attributeValues = requireNonNull(content.getAvailablePropertyValues(), "AvailablePropertyValues must not be null. ");
048        final var result = new StringBuilder();
049        if (attributeValues.containsKey(attribute)) {
050            if (attributeValues.keySet().size() > 1) {
051                result.append(before).append(attributeValues.get(attribute)).append(after);
052            } else {
053                // special case : if only one value exists no stored before + after are needed
054                result.append(attributeValues.get(attribute).toString());
055            }
056        }
057        return result.toString();
058    }
059
060    @Override
061    public boolean isStringToken() {
062        return false;
063    }
064
065    @java.lang.Override
066    @java.lang.SuppressWarnings("all")
067    @lombok.Generated
068    public java.lang.String toString() {
069        return "ActionToken(before=" + this.before + ", attribute=" + this.attribute + ", after=" + this.after + ")";
070    }
071
072    @java.lang.Override
073    @java.lang.SuppressWarnings("all")
074    @lombok.Generated
075    public boolean equals(final java.lang.Object o) {
076        if (o == this) return true;
077        if (!(o instanceof ActionToken)) return false;
078        final ActionToken other = (ActionToken) o;
079        if (!other.canEqual((java.lang.Object) this)) return false;
080        final java.lang.Object this$before = this.before;
081        final java.lang.Object other$before = other.before;
082        if (this$before == null ? other$before != null : !this$before.equals(other$before)) return false;
083        final java.lang.Object this$attribute = this.attribute;
084        final java.lang.Object other$attribute = other.attribute;
085        if (this$attribute == null ? other$attribute != null : !this$attribute.equals(other$attribute)) return false;
086        final java.lang.Object this$after = this.after;
087        final java.lang.Object other$after = other.after;
088        if (this$after == null ? other$after != null : !this$after.equals(other$after)) return false;
089        return true;
090    }
091
092    @java.lang.SuppressWarnings("all")
093    @lombok.Generated
094    protected boolean canEqual(final java.lang.Object other) {
095        return other instanceof ActionToken;
096    }
097
098    @java.lang.Override
099    @java.lang.SuppressWarnings("all")
100    @lombok.Generated
101    public int hashCode() {
102        final int PRIME = 59;
103        int result = 1;
104        final java.lang.Object $before = this.before;
105        result = result * PRIME + ($before == null ? 43 : $before.hashCode());
106        final java.lang.Object $attribute = this.attribute;
107        result = result * PRIME + ($attribute == null ? 43 : $attribute.hashCode());
108        final java.lang.Object $after = this.after;
109        result = result * PRIME + ($after == null ? 43 : $after.hashCode());
110        return result;
111    }
112}