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

org.gradle.internal.exceptions.FormattingDiagnosticsVisitor Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.internal.exceptions;

import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Formats candidates as a list.
 */
public class FormattingDiagnosticsVisitor implements DiagnosticsVisitor {
    private final Map candidates = new LinkedHashMap();
    private Candidate current;

    public List getCandidates() {
        return format(candidates);
    }

    private List format(Map candidates) {
        List formatted = new ArrayList();
        for (Candidate candidate : candidates.values()) {
            if (candidate.examples.isEmpty()) {
                formatted.add(candidate.description);
            } else {
                formatted.add(String.format("%s, for example %s.", candidate.description, Joiner.on(", ").join(candidate.examples)));
            }
        }
        return formatted;
    }

    @Override
    public DiagnosticsVisitor candidate(String displayName) {
        Candidate candidate = candidates.get(displayName);
        if (candidate == null) {
            candidate = new Candidate(displayName);
            candidates.put(displayName, candidate);
        }
        current = candidate;
        return this;
    }

    @Override
    public DiagnosticsVisitor example(String example) {
        current.examples.add(example);
        return this;
    }

    @Override
    public DiagnosticsVisitor values(Iterable values) {
        return this;
    }

    private static class Candidate {
        final String description;
        final List examples = new ArrayList();

        public Candidate(String description) {
            this.description = description;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy