Example usage for java.util.function BooleanSupplier getAsBoolean

List of usage examples for java.util.function BooleanSupplier getAsBoolean

Introduction

In this page you can find the example usage for java.util.function BooleanSupplier getAsBoolean.

Prototype

boolean getAsBoolean();

Source Link

Document

Gets a result.

Usage

From source file:Main.java

public static void main(String[] args) {
    BooleanSupplier bs = () -> true;
    System.out.println(bs.getAsBoolean());
}

From source file:Main.java

public static void main(String[] args) {
    BooleanSupplier bs = () -> true;
    System.out.println(bs.getAsBoolean());

    int x = 0, y = 1;
    bs = () -> x > y;/*from w  w w .ja v a2 s  . c  o m*/
    System.out.println(bs.getAsBoolean());
}

From source file:org.apache.ratis.RaftTestUtil.java

public static void block(BooleanSupplier isBlocked) throws InterruptedException {
    for (; isBlocked.getAsBoolean();) {
        Thread.sleep(RaftServerConfigKeys.RAFT_SERVER_RPC_TIMEOUT_MAX_MS_DEFAULT);
    }//from w  w  w  . jav a 2s  . c o m
}

From source file:org.apache.samza.zk.TestZkUtils.java

public static boolean testWithDelayBackOff(BooleanSupplier cond, long startDelayMs, long maxDelayMs) {
    long delay = startDelayMs;
    while (delay < maxDelayMs) {
        if (cond.getAsBoolean())
            return true;
        try {/*w  ww .j a v  a 2  s  .c om*/
            Thread.sleep(delay);
        } catch (InterruptedException e) {
            return false;
        }
        delay *= 2;
    }
    return false;
}

From source file:org.springframework.web.socket.sockjs.client.AbstractSockJsIntegrationTests.java

private static void awaitEvent(BooleanSupplier condition, long timeToWait, String description) {
    long timeToSleep = 200;
    for (int i = 0; i < Math.floor(timeToWait / timeToSleep); i++) {
        if (condition.getAsBoolean()) {
            return;
        }//ww w . j av  a2 s. co  m
        try {
            Thread.sleep(timeToSleep);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Interrupted while waiting for " + description, e);
        }
    }
    throw new IllegalStateException("Timed out waiting for " + description);
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

public static boolean processConditions(JsonArray conditions, JsonContext context) {
    for (int x = 0; x < conditions.size(); x++) {
        if (!conditions.get(x).isJsonObject())
            throw new JsonSyntaxException("Conditions must be an array of JsonObjects");

        JsonObject json = conditions.get(x).getAsJsonObject();
        BooleanSupplier cond = CraftingHelper.getCondition(json, context);
        if (!cond.getAsBoolean())
            return false;
    }/*from ww w . ja  v a 2 s.c  o  m*/
    return true;
}

From source file:com.evolveum.midpoint.schema.util.AdminGuiConfigTypeUtil.java

public static boolean isVisible(UserInterfaceElementVisibilityType visibility,
        BooleanSupplier automaticPredicate) {
    if (visibility == UserInterfaceElementVisibilityType.HIDDEN) {
        return false;
    }/*  ww w  .  j a v  a2  s. c o  m*/
    if (visibility == UserInterfaceElementVisibilityType.VISIBLE) {
        return true;
    }
    if (visibility == UserInterfaceElementVisibilityType.AUTOMATIC) {
        if (automaticPredicate == null) {
            return true;
        } else {
            return automaticPredicate.getAsBoolean();
        }
    }
    return false;
}

From source file:net.minecraftforge.common.crafting.CraftingHelper.java

private static void init() {
    conditions.clear();/* w w w .j  a v a2s . co  m*/
    ingredients.clear();
    recipes.clear();

    registerC("forge:mod_loaded", (context, json) -> {
        String modid = JsonUtils.getString(json, "modid");
        return () -> Loader.isModLoaded(modid);
    });
    registerC("minecraft:item_exists", (context, json) -> {
        String itemName = context.appendModId(JsonUtils.getString(json, "item"));
        return () -> ForgeRegistries.ITEMS.containsKey(new ResourceLocation(itemName));
    });
    registerC("forge:not", (context, json) -> {
        BooleanSupplier child = CraftingHelper.getCondition(JsonUtils.getJsonObject(json, "value"), context);
        return () -> !child.getAsBoolean();
    });
    registerC("forge:or", (context, json) -> {
        JsonArray values = JsonUtils.getJsonArray(json, "values");
        List<BooleanSupplier> children = Lists.newArrayList();
        for (JsonElement j : values) {
            if (!j.isJsonObject())
                throw new JsonSyntaxException("Or condition values must be an array of JsonObjects");
            children.add(CraftingHelper.getCondition(j.getAsJsonObject(), context));
        }
        return () -> children.stream().anyMatch(BooleanSupplier::getAsBoolean);
    });
    registerC("forge:and", (context, json) -> {
        JsonArray values = JsonUtils.getJsonArray(json, "values");
        List<BooleanSupplier> children = Lists.newArrayList();
        for (JsonElement j : values) {
            if (!j.isJsonObject())
                throw new JsonSyntaxException("And condition values must be an array of JsonObjects");
            children.add(CraftingHelper.getCondition(j.getAsJsonObject(), context));
        }
        return () -> children.stream().allMatch(c -> c.getAsBoolean());
    });
    registerC("forge:false", (context, json) -> {
        return () -> false;
    });

    registerR("minecraft:crafting_shaped", (context, json) -> {
        String group = JsonUtils.getString(json, "group", "");
        //if (!group.isEmpty() && group.indexOf(':') == -1)
        //    group = context.getModId() + ":" + group;

        Map<Character, Ingredient> ingMap = Maps.newHashMap();
        for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "key").entrySet()) {
            if (entry.getKey().length() != 1)
                throw new JsonSyntaxException("Invalid key entry: '" + entry.getKey()
                        + "' is an invalid symbol (must be 1 character only).");
            if (" ".equals(entry.getKey()))
                throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol.");

            ingMap.put(entry.getKey().toCharArray()[0],
                    CraftingHelper.getIngredient(entry.getValue(), context));
        }
        ingMap.put(' ', Ingredient.EMPTY);

        JsonArray patternJ = JsonUtils.getJsonArray(json, "pattern");

        if (patternJ.size() == 0)
            throw new JsonSyntaxException("Invalid pattern: empty pattern not allowed");
        if (patternJ.size() > 3)
            throw new JsonSyntaxException("Invalid pattern: too many rows, 3 is maximum");

        String[] pattern = new String[patternJ.size()];
        for (int x = 0; x < pattern.length; ++x) {
            String line = JsonUtils.getString(patternJ.get(x), "pattern[" + x + "]");
            if (line.length() > 3)
                throw new JsonSyntaxException("Invalid pattern: too many columns, 3 is maximum");
            if (x > 0 && pattern[0].length() != line.length())
                throw new JsonSyntaxException("Invalid pattern: each row must be the same width");
            pattern[x] = line;
        }

        NonNullList<Ingredient> input = NonNullList.withSize(pattern[0].length() * pattern.length,
                Ingredient.EMPTY);
        Set<Character> keys = Sets.newHashSet(ingMap.keySet());
        keys.remove(' ');

        int x = 0;
        for (String line : pattern) {
            for (char chr : line.toCharArray()) {
                Ingredient ing = ingMap.get(chr);
                if (ing == null)
                    throw new JsonSyntaxException(
                            "Pattern references symbol '" + chr + "' but it's not defined in the key");
                input.set(x++, ing);
                keys.remove(chr);
            }
        }

        if (!keys.isEmpty())
            throw new JsonSyntaxException("Key defines symbols that aren't used in pattern: " + keys);

        ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
        return new ShapedRecipes(group, pattern[0].length(), pattern.length, input, result);
    });
    registerR("minecraft:crafting_shapeless", (context, json) -> {
        String group = JsonUtils.getString(json, "group", "");

        NonNullList<Ingredient> ings = NonNullList.create();
        for (JsonElement ele : JsonUtils.getJsonArray(json, "ingredients"))
            ings.add(CraftingHelper.getIngredient(ele, context));

        if (ings.isEmpty())
            throw new JsonParseException("No ingredients for shapeless recipe");
        if (ings.size() > 9)
            throw new JsonParseException("Too many ingredients for shapeless recipe");

        ItemStack itemstack = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
        return new ShapelessRecipes(group, itemstack, ings);
    });
    registerR("forge:ore_shaped", ShapedOreRecipe::factory);
    registerR("forge:ore_shapeless", ShapelessOreRecipe::factory);

    registerI("minecraft:item",
            (context, json) -> Ingredient.fromStacks(CraftingHelper.getItemStackBasic(json, context)));
    registerI("minecraft:empty", (context, json) -> Ingredient.EMPTY);
    registerI("minecraft:item_nbt",
            (context, json) -> new IngredientNBT(CraftingHelper.getItemStack(json, context)));
    registerI("forge:ore_dict", (context, json) -> new OreIngredient(JsonUtils.getString(json, "ore")));
}

From source file:net.mozq.picto.core.ProcessCore.java

public static void processFiles(ProcessCondition processCondition,
        Function<Integer, ProcessData> processDataGetter, IntConsumer processDataUpdater,
        Function<ProcessData, ProcessDataStatus> overwriteConfirm, BooleanSupplier processStopper)
        throws IOException {

    int index = 0;
    while (!processStopper.getAsBoolean()) {

        ProcessData processData = processDataGetter.apply(index);
        if (processData == null) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // NOP
            }/*from w  w  w.  j  a va2  s  . c om*/
            continue;
        }

        processData.setStatus(ProcessDataStatus.Processing);
        processDataUpdater.accept(index);

        ProcessDataStatus status;
        try {
            if (processCondition.isDryRun()) {
                // NOP
                status = ProcessDataStatus.Success;
            } else {
                status = process(processCondition, processData, overwriteConfirm);
            }
            processData.setStatus(status);
        } catch (Exception e) {
            processData.setStatus(ProcessDataStatus.Error);
            processData.setMessage(e.getLocalizedMessage());
            App.handleWarn(e.getMessage(), e);
        }

        processDataUpdater.accept(index);
        if (processData.getStatus() == ProcessDataStatus.Error
                || processData.getStatus() == ProcessDataStatus.Terminated) {
            break;
        }

        index++;
    }
}

From source file:org.apache.bookkeeper.mledger.impl.OffloadPrefixTest.java

static void assertEventuallyTrue(BooleanSupplier predicate) throws Exception {
    // wait up to 3 seconds
    for (int i = 0; i < 30 && !predicate.getAsBoolean(); i++) {
        Thread.sleep(100);/*  ww w  .  j ava  2 s . com*/
    }
    Assert.assertTrue(predicate.getAsBoolean());
}