crwdns126649:0crwdne126649:0

crwdns126651:0crwdne126651:0

Tip

crwdns126653:0crwdne126653:0

  • crwdns126655:0crwdne126655:0

    • crwdns126657:0crwdne126657:0

      • crwdns126659:0crwdne126659:0

  • crwdns126661:0crwdne126661:0

    • crwdns126663:0crwdne126663:0

    • crwdns126665:0crwdne126665:0

    • crwdns126667:0crwdne126667:0

  • crwdns126669:0crwdne126669:0

    • crwdns126671:0crwdne126671:0

  • crwdns126673:0crwdne126673:0

    • crwdns126675:0crwdne126675:0

  • crwdns126677:0crwdne126677:0

    • crwdns126679:0crwdne126679:0

  • crwdns126681:0crwdne126681:0

    • crwdns126683:0crwdne126683:0

      • crwdns126685:0crwdne126685:0

      • crwdns126687:0crwdne126687:0

      • crwdns126689:0crwdne126689:0

      • crwdns126691:0crwdne126691:0

    • crwdns126693:0crwdne126693:0

  • crwdns126695:0crwdne126695:0

    • crwdns126697:0crwdne126697:0

  • crwdns126699:0crwdne126699:0

    • crwdns126701:0crwdne126701:0

  • crwdns126703:0crwdne126703:0

    • crwdns126705:0crwdne126705:0

    • crwdns126707:0crwdne126707:0

    • crwdns126709:0crwdne126709:0

  • crwdns126711:0crwdne126711:0

    • crwdns126713:0crwdne126713:0

crwdns126715:0crwdne126715:0

  • crwdns126717:0:doc:crwdne126717:0

  • crwdns126719:0crwdne126719:0

  • crwdns126721:0crwdne126721:0

crwdns126723:0crwdne126723:0

crwdns126725:0crwdne126725: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);
    }

}