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

org.zodiac.template.base.impl.TemplateKey Maven / Gradle / Ivy

The newest version!
package org.zodiac.template.base.impl;

import java.util.Arrays;

import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.sdk.toolkit.util.file.FileToolUtil;
import org.zodiac.sdk.toolkit.util.file.FileToolUtil.FileInfo;
import org.zodiac.sdk.toolkit.util.lang.ArrayUtil;
import org.zodiac.sdk.toolkit.util.lang.ObjUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

public final class TemplateKey {

    private final String templateNameWithoutExtension;
    private final String extension;
    private final Object[] strategyKeys;

    public TemplateKey(String templateName, TemplateSearchingStrategy[] strategies) {
        templateName = AssertUtil.assertNotNull(StrUtil.trimToNull(FileToolUtil.normalizeAbsolutePath(templateName)),
            "illegal templateName: %s", templateName);

        FileInfo names = FileToolUtil.getFileInfo(templateName, true);

        this.templateNameWithoutExtension = names.getFileName();
        this.extension = names.getExtension();

        if (ArrayUtil.isEmptyArray(strategies)) {
            this.strategyKeys = ArrayUtil.EMPTY_OBJECT_ARRAY;
        } else {
            this.strategyKeys = new Object[strategies.length];

            for (int i = 0; i < strategies.length; i++) {
                strategyKeys[i] = strategies[i].getKey(getTemplateName());
            }
        }
    }

    public String getTemplateName() {
        return getTemplateName(templateNameWithoutExtension, extension);
    }

    public String getTemplateNameWithoutExtension() {
        return templateNameWithoutExtension;
    }

    public String getExtension() {
        return extension;
    }

    public Object[] getStrategyKeys() {
        return strategyKeys.clone();
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }

        if (other == null) {
            return false;
        }

        if (!(other instanceof TemplateKey)) {
            return false;
        }

        TemplateKey otherKey = (TemplateKey)other;

        if (ObjUtil.notEqualsObject(templateNameWithoutExtension, otherKey.templateNameWithoutExtension)) {
            return false;
        }

        if (ObjUtil.notEqualsObject(extension, otherKey.extension)) {
            return false;
        }

        if (!Arrays.equals(strategyKeys, otherKey.strategyKeys)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;

        result = prime * result + (extension == null ? 0 : extension.hashCode());
        result = prime * result + Arrays.hashCode(strategyKeys);
        result = prime * result + (templateNameWithoutExtension == null ? 0 : templateNameWithoutExtension.hashCode());

        return result;
    }

    @Override
    public String toString() {
        return getTemplateName() + Arrays.toString(strategyKeys);
    }

    public static String getTemplateName(String templateNameWithoutExtension, String extension) {
        String templateName = templateNameWithoutExtension;

        if (StrUtil.isNotEmpty(extension)) {
            templateName = templateNameWithoutExtension + "." + extension;
        }

        return templateName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy