crwdns135579:0crwdne135579:0

Warning

crwdns135581:0crwdne135581:0

crwdns135583:0crwdne135583:0

Tip

crwdns135585:0:doc:crwdne135585:0

  • crwdns135587:0crwdne135587:0

    • crwdns135589:0crwdne135589:0

      • crwdns135591:0crwdne135591:0

  • crwdns135593:0crwdne135593:0

    • crwdns135595:0crwdne135595:0

    • crwdns135597:0crwdne135597:0

    • crwdns135599:0crwdne135599:0

  • crwdns135601:0crwdne135601:0

    • crwdns135603:0crwdne135603:0

  • crwdns135605:0crwdne135605:0

    • crwdns135607:0crwdne135607:0

  • crwdns135609:0crwdne135609:0

    • crwdns135611:0crwdne135611:0

  • crwdns135613:0crwdne135613:0

    • crwdns135615:0crwdne135615:0

      • crwdns135617:0crwdne135617:0

      • crwdns135619:0crwdne135619:0

      • crwdns135621:0crwdne135621:0

      • crwdns135623:0crwdne135623:0

    • crwdns135625:0crwdne135625:0

  • crwdns135627:0crwdne135627:0

    • crwdns135629:0crwdne135629:0

  • crwdns135631:0crwdne135631:0

    • crwdns135633:0crwdne135633:0

  • crwdns135635:0crwdne135635:0

    • crwdns135637:0crwdne135637:0

    • crwdns135639:0crwdne135639:0

    • crwdns135641:0crwdne135641:0

crwdns135643:0crwdne135643:0

  • crwdns135645:0:doc:crwdne135645:0

  • crwdns135647:0crwdne135647:0

  • crwdns135649:0crwdne135649:0

crwdns135651:0crwdne135651:0

crwdns135653:0crwdne135653:0

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;
import java.util.Optional;

public class Example {

    private static final Logger log = LoggerFactory.getLogger(Example.class);
    private static final Random random = new Random();
    private final String id = "test";

    /**
     * Returns an identifier approximately half of the time.
     *
     * <p>A static instance of {@link Random} is used to calculate the
     * outcome with a 50% chance.</p>
     *
     * @return The ID, if available
     */
    public Optional<String> resolveId() {
        log.info("ID requested");

        if (random.nextBoolean()) {
            return Optional.of(this.id);
        } else {
            return Optional.empty();
        }
    }

    /**
     * Returns an identifier approximately half of the time.
     *
     * <p>A static instance of {@link Random} is used to calculate the
     * outcome with a 50% chance. If the outcome is to not return the ID,
     * the given fallback ID is returned.</p>
     *
     * @param fallback A fallback name to return
     * @return The ID half of the time, the given fallback the other half
     */
    public String resolveId(String fallback) {
        return resolveId().orElse(fallback);
    }

}