LongCacheConfig.java 2.84 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.ui.clientlibs.impl;

import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LongCacheConfig {
    private static final Logger log = LoggerFactory.getLogger(LongCacheConfig.class);
    public static final String AUTO_SHARED_CACHE_KEY = "auto";
    public static final String NONE_SHARED_CACHE_KEY = "none";
    private final Entry[] longCacheEntries;
    private final String longCacheAutoValue;
    private String keyFormat = "lc-%s-lc";

    public LongCacheConfig() {
        this.longCacheAutoValue = String.valueOf(System.currentTimeMillis());
        this.longCacheEntries = new Entry[0];
    }

    public LongCacheConfig(String[] patterns, String auto, String format) {
        String string = this.longCacheAutoValue = auto == null ? String.valueOf(System.currentTimeMillis()) : auto;
        if (format != null && format.length() > 0) {
            this.keyFormat = format;
        }
        LinkedList<Entry> entries = new LinkedList<Entry>();
        if (patterns != null) {
            for (String p : patterns) {
                int idx = p.indexOf(59);
                String key = "auto";
                if (idx > 0) {
                    key = p.substring(idx + 1).trim();
                    p = p.substring(0, idx);
                }
                try {
                    entries.add(new Entry(Pattern.compile(p), this.formatCacheKey(key)));
                    continue;
                }
                catch (PatternSyntaxException e) {
                    log.error("Error in long cache regexp: " + p, (Throwable)e);
                }
            }
        }
        this.longCacheEntries = entries.toArray(new Entry[entries.size()]);
    }

    public String formatCacheKey(String rawKey) {
        if (rawKey == null || "none".equals(rawKey)) {
            return null;
        }
        if ("auto".equals(rawKey)) {
            rawKey = this.longCacheAutoValue;
        }
        return String.format(this.keyFormat, rawKey);
    }

    public String getCacheKey(String path) {
        String key = null;
        for (Entry entry : this.longCacheEntries) {
            key = entry.getKeyIfMatches(path, key);
        }
        return key;
    }

    private static final class Entry {
        private final Pattern pattern;
        private final String key;

        private Entry(Pattern pattern, String key) {
            this.pattern = pattern;
            this.key = key;
        }

        private String getKeyIfMatches(String path, String defaultValue) {
            return this.pattern.matcher(path).matches() ? this.key : defaultValue;
        }
    }

}