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

org.apache.maven.api.settings.ActivationProperty Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta-5
Show newest version
// =================== DO NOT EDIT THIS FILE ====================
//  Generated by Modello Velocity from model.vm
//  template, any modifications will be overwritten.
// ==============================================================
package org.apache.maven.api.settings;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Generated;
import org.apache.maven.api.annotations.Immutable;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.NotThreadSafe;
import org.apache.maven.api.annotations.ThreadSafe;

/**
 * This is the property specification used to activate a profile. If the value field is empty,
 * then the existence of the named property will activate the profile, otherwise it does a case-sensitive
 * match against the property value as well.
 */
@Experimental
@Generated @ThreadSafe @Immutable
public class ActivationProperty
    implements Serializable, InputLocationTracker
{
    /**
     * The name of the property to be used to activate a profile.
     */
    final String name;
    /**
     * The value of the property to be used to activate a profile.
     */
    final String value;
    /** Locations */
    final Map locations;
    /** Location tracking */
    final InputLocation importedFrom;

    /**
      * Constructor for this class, to be called from its subclasses and {@link Builder}.
      * @see Builder#build()
      */
    protected ActivationProperty(Builder builder) {
        this.name = builder.name != null ? builder.name : (builder.base != null ? builder.base.name : null);
        this.value = builder.value != null ? builder.value : (builder.base != null ? builder.base.value : null);
        this.locations = builder.computeLocations();
        this.importedFrom = builder.importedFrom;
    }

    /**
     * The name of the property to be used to activate a profile.
     *
     * @return a {@code String}
     */
    public String getName() {
        return this.name;
    }

    /**
     * The value of the property to be used to activate a profile.
     *
     * @return a {@code String}
     */
    public String getValue() {
        return this.value;
    }

    /**
     * Gets the location of the specified field in the input source.
     */
    public InputLocation getLocation(Object key) {
        return locations.get(key);
    }

    /**
     * Gets the keys of the locations of the input source.
     */
    public Set getLocationKeys() {
        return locations.keySet();
    }

    protected Stream getLocationKeyStream() {
        return locations.keySet().stream();
    }

    /**
     * Gets the input location that caused this model to be read.
     */
    public InputLocation getImportedFrom()
    {
        return importedFrom;
    }

    /**
     * Creates a new builder with this object as the basis.
     *
     * @return a {@code Builder}
     */
    @Nonnull
    public Builder with() {
        return newBuilder(this);
    }
    /**
     * Creates a new {@code ActivationProperty} instance using the specified name.
     *
     * @param name the new {@code String} to use
     * @return a {@code ActivationProperty} with the specified name
     */
    @Nonnull
    public ActivationProperty withName(String name) {
        return newBuilder(this, true).name(name).build();
    }
    /**
     * Creates a new {@code ActivationProperty} instance using the specified value.
     *
     * @param value the new {@code String} to use
     * @return a {@code ActivationProperty} with the specified value
     */
    @Nonnull
    public ActivationProperty withValue(String value) {
        return newBuilder(this, true).value(value).build();
    }

    /**
     * Creates a new {@code ActivationProperty} instance.
     * Equivalent to {@code newInstance(true)}.
     * @see #newInstance(boolean)
     *
     * @return a new {@code ActivationProperty}
     */
    @Nonnull
    public static ActivationProperty newInstance() {
        return newInstance(true);
    }

    /**
     * Creates a new {@code ActivationProperty} instance using default values or not.
     * Equivalent to {@code newBuilder(withDefaults).build()}.
     *
     * @param withDefaults the boolean indicating whether default values should be used
     * @return a new {@code ActivationProperty}
     */
    @Nonnull
    public static ActivationProperty newInstance(boolean withDefaults) {
        return newBuilder(withDefaults).build();
    }

    /**
     * Creates a new {@code ActivationProperty} builder instance.
     * Equivalent to {@code newBuilder(true)}.
     * @see #newBuilder(boolean)
     *
     * @return a new {@code Builder}
     */
    @Nonnull
    public static Builder newBuilder() {
        return newBuilder(true);
    }

    /**
     * Creates a new {@code ActivationProperty} builder instance using default values or not.
     *
     * @param withDefaults the boolean indicating whether default values should be used
     * @return a new {@code Builder}
     */
    @Nonnull
    public static Builder newBuilder(boolean withDefaults) {
        return new Builder(withDefaults);
    }

    /**
     * Creates a new {@code ActivationProperty} builder instance using the specified object as a basis.
     * Equivalent to {@code newBuilder(from, false)}.
     *
     * @param from the {@code ActivationProperty} instance to use as a basis
     * @return a new {@code Builder}
     */
    @Nonnull
    public static Builder newBuilder(ActivationProperty from) {
        return newBuilder(from, false);
    }

    /**
     * Creates a new {@code ActivationProperty} builder instance using the specified object as a basis.
     *
     * @param from the {@code ActivationProperty} instance to use as a basis
     * @param forceCopy the boolean indicating if a copy should be forced
     * @return a new {@code Builder}
     */
    @Nonnull
    public static Builder newBuilder(ActivationProperty from, boolean forceCopy) {
        return new Builder(from, forceCopy);
    }

    /**
     * Builder class used to create ActivationProperty instances.
     * @see #with()
     * @see #newBuilder()
     */
    @NotThreadSafe
    public static class Builder
    {
        ActivationProperty base;
        String name;
        String value;
        Map locations;
        InputLocation importedFrom;

        protected Builder(boolean withDefaults) {
            if (withDefaults) {
            }
        }

        protected Builder(ActivationProperty base, boolean forceCopy) {
            if (forceCopy) {
                this.name = base.name;
                this.value = base.value;
                this.locations = base.locations;
                this.importedFrom = base.importedFrom;
            } else {
                this.base = base;
            }
        }

        @Nonnull
        public Builder name(String name) {
            this.name = name;
            return this;
        }

        @Nonnull
        public Builder value(String value) {
            this.value = value;
            return this;
        }


        @Nonnull
        public Builder location(Object key, InputLocation location) {
            if (location != null) {
                if (!(this.locations instanceof HashMap)) {
                    this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
                }
                this.locations.put(key, location);
            }
            return this;
        }

        @Nonnull
        public Builder importedFrom(InputLocation importedFrom) {
            this.importedFrom = importedFrom;
            return this;
        }

        @Nonnull
        public ActivationProperty build() {
            // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
            if (base != null
                    && (name == null || name == base.name)
                    && (value == null || value == base.value)
            ) {
                return base;
            }
            return new ActivationProperty(this);
        }

        Map computeLocations() {
            Map newlocs = locations != null ? locations : Map.of();
            Map oldlocs = base != null ? base.locations : Map.of();
            if (newlocs.isEmpty()) {
                return Map.copyOf(oldlocs);
            }
            if (oldlocs.isEmpty()) {
                return Map.copyOf(newlocs);
            }
            return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
                    // Keep value from newlocs in case of duplicates
                    .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
        }
    }

}