ClientLibraryImpl.java 9.46 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.granite.ui.clientlibs.impl;

import com.adobe.granite.ui.clientlibs.ClientLibrary;
import com.adobe.granite.ui.clientlibs.LibraryType;
import com.adobe.granite.ui.clientlibs.impl.FileBundle;
import com.adobe.granite.ui.clientlibs.impl.ProcessorConfig;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ClientLibraryImpl
implements ClientLibrary {
    private static final String MINIFIED_SELECTOR = ".min";
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
    private final String path;
    private final Builder cfg;
    private final Map<LibraryType, FileBundle> bundles = new HashMap<LibraryType, FileBundle>();
    private final Map<String, ClientLibraryImpl> depLibs = new LinkedHashMap<String, ClientLibraryImpl>();
    private final Map<String, ClientLibraryImpl> embLibs = new LinkedHashMap<String, ClientLibraryImpl>();
    private final Set<String> embedders = new HashSet<String>();
    private Set<String> sourcePaths;

    private ClientLibraryImpl(Builder cfg) {
        this.cfg = cfg;
        this.path = cfg.path;
    }

    private static String[] trimAndClean(String[] strings) {
        int i;
        if (strings == null) {
            return new String[0];
        }
        int j = 0;
        for (i = 0; i < strings.length; ++i) {
            String s;
            String string = s = strings[i] == null ? "" : strings[i].trim();
            if (s.length() <= 0) continue;
            strings[j++] = s;
        }
        if (j != i) {
            String[] copy = new String[j];
            System.arraycopy(strings, 0, copy, 0, j);
            return copy;
        }
        return strings;
    }

    public Map<String, ClientLibraryImpl> getDependencies(boolean transitive) {
        LinkedHashMap<String, ClientLibraryImpl> deps = new LinkedHashMap<String, ClientLibraryImpl>();
        this.resolve(deps, transitive);
        if (transitive) {
            for (ClientLibraryImpl emb : this.embLibs.values()) {
                emb.resolve(deps, true);
            }
        }
        for (ClientLibraryImpl emb : this.embLibs.values()) {
            deps.remove(emb.getPath());
        }
        return deps;
    }

    public Map<String, ClientLibraryImpl> getEmbedded(LibraryType type) {
        if (this.embLibs.isEmpty()) {
            return this.embLibs;
        }
        LinkedHashMap<String, ClientLibraryImpl> libs = new LinkedHashMap<String, ClientLibraryImpl>();
        for (ClientLibraryImpl emb : this.embLibs.values()) {
            if (!emb.hasType(type) || this.cfg.themeName == null != (emb.cfg.themeName == null) || !Arrays.equals(this.cfg.channels, emb.cfg.channels)) continue;
            libs.put(emb.getPath(), emb);
        }
        return libs;
    }

    @Override
    public String getPath() {
        return this.path;
    }

    @Override
    public String getThemeName() {
        return this.cfg.themeName;
    }

    @Override
    public String getThemeLibId() {
        return this.cfg.libId;
    }

    @Override
    public String getIncludePath(LibraryType type) {
        return this.getIncludePath(type, false);
    }

    @Override
    public String getIncludePath(LibraryType type, boolean minified) {
        if (this.cfg.isLegacy || type == null) {
            return this.path;
        }
        if (this.bundles.containsKey((Object)type)) {
            StringBuilder ret = new StringBuilder(this.path);
            ret.append(this.cfg.cacheKey);
            if (minified) {
                ret.append(".min");
            }
            ret.append(type.extension);
            return ret.toString();
        }
        return null;
    }

    @Override
    public String[] getCategories() {
        return this.cfg.categories;
    }

    @Override
    public String[] getEmbeddedCategories() {
        return this.cfg.embeds;
    }

    @Override
    public String[] getDependentCategories() {
        return this.cfg.dependencies;
    }

    @Override
    public String[] getChannels() {
        return this.cfg.channels;
    }

    @Override
    public Set<LibraryType> getTypes() {
        return Collections.unmodifiableSet(this.bundles.keySet());
    }

    @Override
    public boolean allowProxy() {
        return this.cfg.allowProxy;
    }

    protected List<ProcessorConfig> getProcessorConfig(LibraryType type) {
        return type == LibraryType.CSS ? this.cfg.cssProcessors : this.cfg.jsProcessors;
    }

    protected void addDependency(ClientLibraryImpl lib) {
        this.depLibs.put(lib.getPath(), lib);
    }

    protected void addEmbedded(ClientLibraryImpl lib) {
        this.embLibs.put(lib.getPath(), lib);
        lib.embedders.add(this.path);
    }

    protected FileBundle getBundle(LibraryType type) {
        return this.bundles.get((Object)type);
    }

    protected void addBundle(LibraryType type, FileBundle bundle) {
        this.bundles.put(type, bundle);
    }

    protected void clear() {
        this.depLibs.clear();
        this.embLibs.clear();
        this.embedders.clear();
    }

    protected Set<String> getEmbedders() {
        return this.embedders;
    }

    protected ClientLibraryImpl createCopy() {
        ClientLibraryImpl lib = new ClientLibraryImpl(this.cfg);
        lib.bundles.putAll(this.bundles);
        lib.depLibs.putAll(this.depLibs);
        lib.embLibs.putAll(this.embLibs);
        lib.embedders.addAll(this.embedders);
        return lib;
    }

    protected void relink(Map<String, ClientLibraryImpl> libs) {
        HashSet<String> paths = new HashSet<String>(this.depLibs.keySet());
        for (String path2 : paths) {
            this.depLibs.put(path2, libs.get(path2));
        }
        paths.clear();
        paths.addAll(this.embLibs.keySet());
        for (String path2 : paths) {
            this.embLibs.put(path2, libs.get(path2));
        }
    }

    private void resolve(Map<String, ClientLibraryImpl> collect, boolean transitive) {
        for (ClientLibraryImpl dep : this.depLibs.values()) {
            if (collect.containsKey(dep.getPath())) continue;
            if (transitive) {
                dep.resolve(collect, true);
            }
            collect.put(dep.getPath(), dep);
        }
    }

    private boolean hasType(LibraryType type) {
        return this.bundles.isEmpty() || type == null || this.bundles.containsKey((Object)type);
    }

    public Set<String> getSourcePaths() {
        if (this.sourcePaths == null) {
            this.sourcePaths = new HashSet<String>();
            for (FileBundle bundle : this.bundles.values()) {
                bundle.addSourcePaths(this.sourcePaths);
            }
        }
        return this.sourcePaths;
    }

    public void invalidateSourcePaths() {
        this.sourcePaths = null;
    }

    static /* synthetic */ String[] access$000() {
        return EMPTY_STRING_ARRAY;
    }

    public static class Builder {
        private final String path;
        private String[] categories = ClientLibraryImpl.access$000();
        private String[] dependencies = ClientLibraryImpl.access$000();
        private String[] embeds = ClientLibraryImpl.access$000();
        private String[] channels = ClientLibraryImpl.access$000();
        private boolean isLegacy;
        private String themeName;
        private String libId;
        private boolean allowProxy;
        private String cacheKey = "";
        private List<ProcessorConfig> cssProcessors = Collections.emptyList();
        private List<ProcessorConfig> jsProcessors = Collections.emptyList();

        public Builder(String path) {
            this.path = path;
        }

        public Builder withCategories(String[] categories) {
            this.categories = ClientLibraryImpl.trimAndClean(categories);
            return this;
        }

        public Builder withDependencies(String[] dependencies) {
            this.dependencies = ClientLibraryImpl.trimAndClean(dependencies);
            return this;
        }

        public Builder withEmbeds(String[] embeds) {
            this.embeds = ClientLibraryImpl.trimAndClean(embeds);
            return this;
        }

        public Builder withChannels(String[] channels) {
            this.channels = ClientLibraryImpl.trimAndClean(channels);
            return this;
        }

        public Builder withIsLegacy(boolean isLegacy) {
            this.isLegacy = isLegacy;
            return this;
        }

        public Builder withThemeName(String themeName) {
            this.themeName = themeName;
            return this;
        }

        public Builder withLibId(String libId) {
            this.libId = libId;
            return this;
        }

        public Builder withAllowProxy(boolean allowProxy) {
            this.allowProxy = allowProxy;
            return this;
        }

        public Builder withCacheKey(String cacheKey) {
            this.cacheKey = cacheKey == null ? "" : "." + cacheKey;
            return this;
        }

        public Builder withCssProcessors(List<ProcessorConfig> config) {
            this.cssProcessors = config == null ? Collections.emptyList() : config;
            return this;
        }

        public Builder withJsProcessors(List<ProcessorConfig> config) {
            this.jsProcessors = config == null ? Collections.emptyList() : config;
            return this;
        }

        public ClientLibraryImpl build() {
            return new ClientLibraryImpl(this);
        }
    }

}