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

net.datafaker.providers.base.Domain Maven / Gradle / Ivy

Go to download

This library is a improved port of JavaFaker (as well as Ruby's stympy/faker gem and Perl's Data::Faker library) that generates fake data. It's useful when you're developing a new project and need some pretty data for showcase.

There is a newer version: 2.4.2
Show newest version
package net.datafaker.providers.base;


import net.datafaker.service.RandomService;

/**
 * A domain name generator.
 *
 * @since 0.9.0
 */
public class Domain extends AbstractProvider {

    /**
     * Instantiates a new Domain.
     *
     * @param faker the faker
     */
    protected Domain(BaseProviders faker) {
        super(faker);
    }

    /**
     * First level domain string. Such as example.com
     *
     * @param name the company name
     * @return the
     */
    public String firstLevelDomain(String name) {
        String top = resolve("domain.top");
        return String.join(".",
            name,
            top
        );
    }

    /**
     * Second level domain string. Such as example.com.uk
     *
     * @param name the company name
     * @return the second level domain with company name
     */
    public String secondLevelDomain(String name) {
        String top = resolve("domain.top");
        String suffix = resolve("domain.country");
        return String.join(".",
            name,
            top,
            suffix
        );
    }

    /**
     * Full domain string. Such as www.example.com.uk
     *
     * @param name the company name
     * @return the full domain name
     */
    public String fullDomain(String name) {
        String prefix = resolve("domain.prefix");
        String top = resolve("domain.top");
        String suffix = resolve("domain.country");
        return String.join(".",
            prefix,
            name,
            top,
            suffix
        );
    }


    /**
     * Return a random valid domain.
     *
     * @param name the company name
     * @return A valid domain
     */
    public String validDomain(String name) {
        final RandomService random = faker.random();

        boolean hasPrefix = random.nextInt(3) == 1;
        boolean hasSuffix = random.nextInt(2) == 1;

        String res = name + "." + resolve("domain.top");
        String prefix = null;
        String suffix = null;
        if (hasPrefix) {
            prefix = resolve("domain.prefix");
        }
        if (hasSuffix) {
            suffix = resolve("domain.country");
        }
        return prefix == null
            ? suffix == null
                ? res : res + "." + suffix
            : suffix == null
                ? prefix + "." + res
                    : prefix + "." + res + "." + suffix;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy