package com.atlassian.stash.internal.scm.git.command.config;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @see ConfigOutputHandler
 * @since 2.3
 */
public class ConfigSection {

    private final String name;
    private final String subsection;
    private final Multimap<String, String> entries;

    public ConfigSection(String name, String subsection, Multimap<String, String> entries) {
        this.name = checkNotNull(name, "name");
        this.subsection = subsection;
        this.entries = checkNotNull(entries, "entries");
    }

    @Nonnull
    public String getName() {
        return name;
    }

    @Nullable
    public String getSubsection() {
        return subsection;
    }

    @Nonnull
    public Multimap<String, String> getEntries() {
        return entries;
    }

    public static class Builder {
        private final ImmutableMultimap.Builder<String, String> entries;
        private String section;
        private String subsection;

        public Builder(String section, String subsection) {
            this.section = section;
            this.subsection = subsection;
            this.entries = ImmutableMultimap.builder();
        }

        public Builder entry(String name, String value) {
            entries.put(name, value);
            return this;
        }

        public ConfigSection build() {
            return new ConfigSection(section, subsection, entries.build());
        }
    }
}
