crwdns154999:0crwdne154999:0

Warning

crwdns155001:0crwdne155001:0

crwdns155003:0crwdne155003:0

Tip

crwdns155005:0:doc:crwdne155005:0

  • crwdns155007:0crwdne155007:0

    • crwdns155009:0crwdne155009:0

      • crwdns155011:0crwdne155011:0

  • crwdns155013:0crwdne155013:0

    • crwdns155015:0crwdne155015:0

    • crwdns155017:0crwdne155017:0

    • crwdns155019:0crwdne155019:0

  • crwdns155021:0crwdne155021:0

    • crwdns155023:0crwdne155023:0

  • crwdns155025:0crwdne155025:0

    • crwdns155027:0crwdne155027:0

  • crwdns155029:0crwdne155029:0

    • crwdns155031:0crwdne155031:0

  • crwdns155033:0crwdne155033:0

    • crwdns155035:0crwdne155035:0

      • crwdns155037:0crwdne155037:0

      • crwdns155039:0crwdne155039:0

      • crwdns155041:0crwdne155041:0

      • crwdns155043:0crwdne155043:0

    • crwdns155045:0crwdne155045:0

  • crwdns155047:0crwdne155047:0

    • crwdns155049:0crwdne155049:0

  • crwdns155051:0crwdne155051:0

    • crwdns155053:0crwdne155053:0

  • crwdns155055:0crwdne155055:0

    • crwdns155057:0crwdne155057:0

    • crwdns155059:0crwdne155059:0

    • crwdns155061:0crwdne155061:0

crwdns155063:0crwdne155063:0

  • crwdns155065:0:doc:crwdne155065:0

  • crwdns155067:0crwdne155067:0

  • crwdns155069:0crwdne155069:0

crwdns155071:0crwdne155071:0

crwdns155073:0crwdne155073: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);
    }

}