crwdns145873:0crwdne145873:0

crwdns145875:0crwdne145875:0

Tip

crwdns145877:0crwdne145877:0

  • crwdns145879:0crwdne145879:0

    • crwdns145881:0crwdne145881:0

      • crwdns145883:0crwdne145883:0

  • crwdns145885:0crwdne145885:0

    • crwdns145887:0crwdne145887:0

    • crwdns145889:0crwdne145889:0

    • crwdns145891:0crwdne145891:0

  • crwdns145893:0crwdne145893:0

    • crwdns145895:0crwdne145895:0

  • crwdns145897:0crwdne145897:0

    • crwdns145899:0crwdne145899:0

  • crwdns145901:0crwdne145901:0

    • crwdns145903:0crwdne145903:0

  • crwdns145905:0crwdne145905:0

    • crwdns145907:0crwdne145907:0

      • crwdns145909:0crwdne145909:0

      • crwdns145911:0crwdne145911:0

      • crwdns145913:0crwdne145913:0

      • crwdns145915:0crwdne145915:0

    • crwdns145917:0crwdne145917:0

  • crwdns145919:0crwdne145919:0

    • crwdns145921:0crwdne145921:0

  • crwdns145923:0crwdne145923:0

    • crwdns145925:0crwdne145925:0

  • crwdns145927:0crwdne145927:0

    • crwdns145929:0crwdne145929:0

    • crwdns145931:0crwdne145931:0

    • crwdns145933:0crwdne145933:0

  • crwdns145935:0crwdne145935:0

    • crwdns145937:0crwdne145937:0

crwdns145939:0crwdne145939:0

  • crwdns145941:0:doc:crwdne145941:0

  • crwdns145943:0crwdne145943:0

  • crwdns145945:0crwdne145945:0

  • crwdns145947:0crwdne145947:0

crwdns145949:0crwdne145949:0

crwdns145951:0crwdne145951:0

/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* 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 org.spongepowered.example;

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

import com.google.inject.Inject;
import org.slf4j.Logger;

import java.util.Optional;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import javax.annotation.Nullable;

/**
* An example class which generates a new ID based on a specified base string
* and a randomly generated integer.
*
* <p>There is a chance the integer purposely fails to generate, in which case
* you can choose to provide a backup integer.</p>
*/
public class Example {

    private static final long SEED = 4815162342L;

    @Inject
    private Logger logger;

    private final String base;
    private final Random random;

    public Example(String base) {
        checkNotNull(base, "The specified base string cannot be null!");
        this.base = base;
        this.random = ThreadLocalRandom.current();
        this.random.setSeed(SEED);
    }

    /**
    * Generates and returns an ID using the base string specified on creation
    * or the alternative string if specified as well as a randomly generated
    * integer, which purposely fails to generate around 50% of the time.
    *
    * <p>A {@link ThreadLocalRandom} is used to check if the integer should
    * be generated and generates the integer itself if so.</p>
    *
    * @param alternate An alternate base string which will be used if not null
    * @return The generated ID, if available
    */
    public Optional<String> generateId(@Nullable String alternate) {
        if (this.random.nextBoolean()) {
            return Optional.of(alternate == null ? this.base : alternate + " - " + this.random.nextInt());
        }

        return Optional.empty();
    }

    /**
    * Generates and returns an ID using the base string specified on creation,
    * using a randomly generated integer if it was generated successfully, or
    * using the backup integer you specify.
    *
    * <p>A {@link ThreadLocalRandom} is used to check if the integer should
    * be generated and generates the integer itself if so. If it was not
    * generated, that is when your backup integer will be used.</p>
    *
    * @param backup A backup integer to use to create the ID with
    * @return The generated ID using the generated integer or the ID created
    *     using the backup integer specified
    */
    public String generateId(int backup) {
        return generateId(null).orElse(this.base + " - " + backup);
    }

}