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

com.mastfrog.annotation.registries.AnnotationIndexFactory Maven / Gradle / Ivy

There is a newer version: 2.9.7
Show newest version
/*
 * The MIT License
 *
 * Copyright 2018 Tim Boudreau.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mastfrog.annotation.registries;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

/**
 *
 * @author Tim Boudreau
 */
public abstract class AnnotationIndexFactory {

    protected final Map>> indexesByProcessor = new HashMap<>();

    public static LineIndexFactory lines() {
        return new LineIndexFactory();
    }

    public int totalSize() {
        int result = 0;
        for (Map.Entry>> indexes : indexesByProcessor.entrySet()) {
            for (Map.Entry> ixForPath : indexes.getValue().entrySet()) {
                result += ixForPath.getValue().size();
            }
        }
        return result;
    }

    public final AnnotationIndex get(String path, ProcessingEnvironment processingEnv) {
        if (path == null || path.isEmpty()) {
            throw new IllegalArgumentException("Path cannot be null or empty");
        }
        Filer filer = processingEnv.getFiler();
        Map> forFiler = indexesByProcessor.get(filer);
        if (forFiler == null) {
            forFiler = new HashMap<>();
            indexesByProcessor.put(filer, forFiler);
        }
        AnnotationIndex result = forFiler.get(path);
        if (result == null) {
            result = newIndex(path);
            forFiler.put(path, result);
        }
        return result;
    }

    public void write(ProcessingEnvironment processingEnv) {
        try {
            for (Map.Entry>> outputFiles : indexesByProcessor.entrySet()) {
                Filer filer = outputFiles.getKey();
                for (Map.Entry> entry : outputFiles.getValue().entrySet()) {
                    try {
                        List elements = new ArrayList<>();
                        for (T line : entry.getValue()) {
                            elements.addAll(Arrays.asList(line.elements()));
                        }
                        FileObject out = filer.createResource(StandardLocation.CLASS_OUTPUT, "", entry.getKey(),
                                elements.toArray(new Element[0]));
                        try (OutputStream os = out.openOutputStream()) {
                            entry.getValue().write(os, processingEnv);
                        }
                    } catch (IOException x) {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write to " + entry.getKey() + ": " + x.toString());
                    }
                }
            }
        } finally {
            indexesByProcessor.clear();
        }
    }

    public final boolean add(String path, T entry, ProcessingEnvironment processingEnv, Element... associatedWith) {
        AnnotationIndex index = get(path, processingEnv);
        int size = index.size();
        int newSize = index.add(entry);
        return newSize > size;
    }

    protected abstract AnnotationIndex newIndex(String path);

    public static abstract class AnnotationIndex implements Iterable {

//        private SortedSet entries = new TreeSet<>();
        private Set entries = new HashSet<>();

        public final int size() {
            return entries.size();
        }

        public int add(T entry) {
            int oldSize = entries.size();
            for (T existing : entries) {
                if (entry.equals(existing)) {
                    existing.addElements(entry.elements());
                    return oldSize;
                }
            }
            entries.add(entry);
            return entries.size();
        }

        public abstract void write(OutputStream out, ProcessingEnvironment processingEnv) throws IOException;

        @Override
        public final Iterator iterator() {
            List l = new ArrayList<>(entries);
            Collections.sort(l);
            return l.iterator();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy