Example usage for com.google.common.base Preconditions checkState

List of usage examples for com.google.common.base Preconditions checkState

Introduction

In this page you can find the example usage for com.google.common.base Preconditions checkState.

Prototype

public static void checkState(boolean expression) 

Source Link

Document

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

Usage

From source file:com.facebook.buck.rules.modern.impl.ValueTypeInfos.java

static ValueTypeInfo<?> forSimpleType(Type type) {
    Preconditions.checkState(type instanceof Class<?>);
    Class<?> rawClass = Primitives.wrap((Class<?>) type);
    if (rawClass == String.class) {
        return StringValueTypeInfo.INSTANCE;
    } else if (rawClass == Character.class) {
        return CharacterValueTypeInfo.INSTANCE;
    } else if (rawClass == Boolean.class) {
        return BooleanValueTypeInfo.INSTANCE;
    } else if (rawClass == Byte.class) {
        return ByteValueTypeInfo.INSTANCE;
    } else if (rawClass == Short.class) {
        return ShortValueTypeInfo.INSTANCE;
    } else if (rawClass == Integer.class) {
        return IntegerValueTypeInfo.INSTANCE;
    } else if (rawClass == Long.class) {
        return LongValueTypeInfo.INSTANCE;
    } else if (rawClass == Float.class) {
        return FloatValueTypeInfo.INSTANCE;
    } else if (rawClass == Double.class) {
        return DoubleValueTypeInfo.INSTANCE;
    } else if (rawClass == OptionalInt.class) {
        return OptionalIntValueTypeInfo.INSTANCE;
    }/*  w  ww.  j ava2  s .  c o m*/
    throw new RuntimeException();
}

From source file:org.haiku.pkg.model.PkgVersion.java

public PkgVersion(String major, String minor, String micro, String preRelease, Integer revision) {
    Preconditions.checkState(!Strings.isNullOrEmpty(major));
    this.major = major;
    this.minor = minor;
    this.micro = micro;
    this.preRelease = preRelease;
    this.revision = revision;
}

From source file:com.google.javascript.jscomp.jsonml.NodeUtil.java

/**
 * @return Whether the node is used as a statement.
 *///from  w w w  .  java 2s  .c om
static boolean isStatement(Node n) {
    Node parent = n.getParent();
    // It is not possible to determine definitely if a node is a statement
    // or not if it is not part of the AST.  A FUNCTION node can be
    // either part of an expression or a statement.
    Preconditions.checkState(parent != null);
    switch (parent.getType()) {
    case Token.SCRIPT:
    case Token.BLOCK:
    case Token.LABEL:
        return true;
    default:
        return false;
    }
}

From source file:com.facebook.buck.parser.cache.impl.ParserCacheStorageFactory.java

private static ParserCacheStorage createLocalParserStorage(AbstractParserCacheConfig parserCacheConfig,
        ProjectFilesystem filesystem) {//ww  w . j  a v a 2  s. co  m
    Preconditions.checkState(parserCacheConfig.isDirParserCacheEnabled());
    return LocalCacheStorage.of(parserCacheConfig, filesystem);
}

From source file:com.facebook.buck.rules.macros.StringWithMacrosUtils.java

/** @return a {@link StringWithMacros} object built with the given format strings and macros. */
public static StringWithMacros format(String format, MacroContainer... macros) {
    ImmutableList.Builder<Either<String, MacroContainer>> partsBuilder = ImmutableList.builder();

    List<String> stringParts = Splitter.on("%s").splitToList(format);
    Preconditions.checkState(stringParts.size() == macros.length + 1);

    if (!stringParts.get(0).isEmpty()) {
        partsBuilder.add(Either.ofLeft(stringParts.get(0)));
    }/*ww w .ja v a2s. c o  m*/

    for (int i = 0; i < macros.length; i++) {
        partsBuilder.add(Either.ofRight(macros[i]));
        if (!stringParts.get(i + 1).isEmpty()) {
            partsBuilder.add(Either.ofLeft(stringParts.get(i + 1)));
        }
    }

    return StringWithMacros.of(partsBuilder.build());
}

From source file:org.haiku.pkg.heap.HeapCoordinates.java

public HeapCoordinates(long offset, long length) {
    super();/*from   w ww  .j  a  va  2  s . c  o  m*/

    Preconditions.checkState(offset >= 0 && offset < Integer.MAX_VALUE);
    Preconditions.checkState(length >= 0 && length < Integer.MAX_VALUE);

    this.offset = offset;
    this.length = length;
}

From source file:com.google.javascript.jscomp.Es6TemplateLiterals.java

/**
 * Converts `${a} b ${c} d ${e}` to (a + " b " + c + " d " + e)
 *
 * @param n A TEMPLATELIT node that is not prefixed with a tag
 *//*from  w w  w. j a  va2s .  c om*/
static void visitTemplateLiteral(NodeTraversal t, Node n) {
    int length = n.getChildCount();
    if (length == 0) {
        n.getParent().replaceChild(n, IR.string("\"\""));
    } else {
        Node first = n.removeFirstChild();
        Preconditions.checkState(first.isString());
        if (length == 1) {
            n.getParent().replaceChild(n, first);
        } else {
            // Add the first string with the first substitution expression
            Node add = IR.add(first, n.removeFirstChild().removeFirstChild());
            // Process the rest of the template literal
            for (int i = 2; i < length; i++) {
                Node child = n.removeFirstChild();
                if (child.isString()) {
                    if (child.getString().isEmpty()) {
                        continue;
                    } else if (i == 2 && first.getString().isEmpty()) {
                        // So that `${hello} world` gets translated into (hello + " world")
                        // instead of ("" + hello + " world").
                        add = add.getChildAtIndex(1).detachFromParent();
                    }
                }
                add = IR.add(add, child.isString() ? child : child.removeFirstChild());
            }
            n.getParent().replaceChild(n, add.useSourceInfoIfMissingFromForTree(n));
        }
    }
    t.getCompiler().reportCodeChange();
}

From source file:org.haiku.pkg.model.StringTableRefAttribute.java

public StringTableRefAttribute(AttributeId attributeId, int index) {
    super(attributeId);
    Preconditions.checkState(index >= 0);
    this.index = index;
}

From source file:com.twitter.common.collections.Bits.java

/**
 * Tests whether a bit is set in an int value.
 *
 * @param value The bit field to test./* w ww.ja  v  a  2s  .  co m*/
 * @param bit The index of the bit to test, where bit 0 is the LSB.
 * @return {@code true} if the bit is set, {@code false} otherwise.
 */
public static boolean isBitSet(int value, int bit) {
    Preconditions.checkState(bit >= LSB);
    Preconditions.checkState(bit <= INT_MSB);
    int mask = 1 << bit;
    return (value & mask) != 0;
}

From source file:org.eclipse.buildship.core.configuration.GradleProjectBuilder.java

/**
 * Configures the builder on the target project if it was not added previously.
 * <p/>// w  w  w .j  a va2s.c o  m
 * This method requires the {@link org.eclipse.core.resources.IWorkspaceRoot} scheduling rule.
 *
 * @param project the target project
 */
public static void configureOnProject(IProject project) {
    try {
        Preconditions.checkState(project.isOpen());

        // check if the builder is already registered with the project
        IProjectDescription description = project.getDescription();
        List<ICommand> buildSpecs = Arrays.asList(description.getBuildSpec());
        boolean exists = FluentIterable.from(buildSpecs).anyMatch(new Predicate<ICommand>() {

            @Override
            public boolean apply(ICommand command) {
                return command.getBuilderName().equals(ID);
            }
        });

        // register the builder with the project if it is not already registered
        if (!exists) {
            ICommand buildSpec = description.newCommand();
            buildSpec.setBuilderName(ID);
            ImmutableList<ICommand> newBuildSpecs = ImmutableList.<ICommand>builder().addAll(buildSpecs)
                    .add(buildSpec).build();
            description.setBuildSpec(newBuildSpecs.toArray(new ICommand[newBuildSpecs.size()]));
            project.setDescription(description, new NullProgressMonitor());
        }
    } catch (CoreException e) {
        CorePlugin.logger()
                .error(String.format("Failed to add Gradle Project Builder to project %s.", project.getName()));
    }
}