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

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

Introduction

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

Prototype

public static void checkArgument(boolean expression, @Nullable Object errorMessage) 

Source Link

Document

Ensures the truth of an expression involving one or more parameters to the calling method.

Usage

From source file:com.google.devtools.build.android.resources.IntArrayFieldInitializer.java

public static FieldInitializer of(String name, String value) {
    Preconditions.checkArgument(value.startsWith("{ "), "Expected list starting with { ");
    Preconditions.checkArgument(value.endsWith(" }"), "Expected list ending with } ");
    // Check for an empty list, which is "{ }".
    if (value.length() < 4) {
        return new IntArrayFieldInitializer(name, ImmutableList.<Integer>of());
    }//from   w  w w .ja  va2 s  . c om
    ImmutableList.Builder<Integer> intValues = ImmutableList.builder();
    String trimmedValue = value.substring(2, value.length() - 2);
    Iterable<String> valueStrings = Splitter.on(',').trimResults().split(trimmedValue);
    for (String valueString : valueStrings) {
        intValues.add(Integer.decode(valueString));
    }
    return new IntArrayFieldInitializer(name, intValues.build());
}

From source file:com.qq.tars.service.SetTriple.java

public static SetTriple parseSet(String set) {
    Preconditions.checkArgument(null != set, "set is null");

    String[] tokens = StringUtils.split(set, '.');
    if (tokens.length != 3 || StringUtils.isAnyBlank(tokens)) {
        throw new IllegalArgumentException(String.format("set=%s", set));
    }/*  w ww  . jav  a2  s . c  o  m*/
    return SetTriple.formatSet(tokens[0], tokens[1], tokens[2]);
}

From source file:com.cloudera.flume.handlers.text.output.RawOutputFormat.java

public static OutputFormatBuilder builder() {
    return new OutputFormatBuilder() {

        @Override//from   w  w  w.  j  av  a 2  s.  c  om
        public OutputFormat build(String... args) {
            Preconditions.checkArgument(args.length == 0, "usage: raw");

            OutputFormat format = new RawOutputFormat();
            format.setBuilder(this);

            return format;
        }

        @Override
        public String getName() {
            return NAME;
        }

    };
}

From source file:com.jivesoftware.os.amza.api.wal.WALKey.java

public static byte[] compose(byte[] prefix, byte[] key) {
    Preconditions.checkNotNull(key, "Key cannot be null");

    int prefixLength = prefix != null ? prefix.length : 0;
    Preconditions.checkArgument(prefixLength <= Short.MAX_VALUE, "Max prefix length is 32767");

    byte[] pk = new byte[2 + prefixLength + key.length];
    UIO.shortBytes((short) prefixLength, pk, 0);
    if (prefix != null) {
        System.arraycopy(prefix, 0, pk, 2, prefixLength);
    }/*from  w  ww .ja v a 2  s . c o  m*/
    System.arraycopy(key, 0, pk, 2 + prefixLength, key.length);
    return pk;
}

From source file:org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser.java

public static List<byte[]> parseMessages(final File file) throws IOException {
    Preconditions.checkArgument(file != null, "Filename cannot be null");
    return parseMessages(new FileInputStream(file));
}

From source file:org.zalando.crypto.Decrypters.java

public static List<Decrypter> findByPrefix(List<Decrypter> decrypters, String prefix) {
    Preconditions.checkNotNull(decrypters, "'Decrypter's-list should not be null");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(prefix), "'prefix' should never be null or empty.");
    return Lists.newArrayList(Iterables.filter(decrypters,
            Predicates.and(new PrefixedDecrypterPredicate(), new PrefixPredicate(prefix))));
}

From source file:io.druid.java.util.common.parsers.TimestampParser.java

public static Function<String, DateTime> createTimestampParser(final String format) {
    if (format.equalsIgnoreCase("auto")) {
        // Could be iso or millis
        return new Function<String, DateTime>() {
            @Override/*  w ww  .  j a  v a  2s. co  m*/
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");

                for (int i = 0; i < input.length(); i++) {
                    if (input.charAt(i) < '0' || input.charAt(i) > '9') {
                        return new DateTime(ParserUtils.stripQuotes(input));
                    }
                }

                return new DateTime(Long.parseLong(input));
            }
        };
    } else if (format.equalsIgnoreCase("iso")) {
        return new Function<String, DateTime>() {
            @Override
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                return new DateTime(ParserUtils.stripQuotes(input));
            }
        };
    } else if (format.equalsIgnoreCase("posix") || format.equalsIgnoreCase("millis")
            || format.equalsIgnoreCase("nano")) {
        final Function<Number, DateTime> numericFun = createNumericTimestampParser(format);
        return new Function<String, DateTime>() {
            @Override
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                return numericFun.apply(Long.parseLong(ParserUtils.stripQuotes(input)));
            }
        };
    } else if (format.equalsIgnoreCase("ruby")) {
        // Numeric parser ignores millis for ruby.
        final Function<Number, DateTime> numericFun = createNumericTimestampParser(format);
        return new Function<String, DateTime>() {
            @Override
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                return numericFun.apply(Double.parseDouble(ParserUtils.stripQuotes(input)));
            }
        };
    } else {
        try {
            final DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
            return new Function<String, DateTime>() {
                @Override
                public DateTime apply(String input) {
                    Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                    return formatter.parseDateTime(ParserUtils.stripQuotes(input));
                }
            };
        } catch (Exception e) {
            throw new IAE(e, "Unable to parse timestamps with format [%s]", format);
        }
    }
}

From source file:org.zanata.client.commands.DocNameWithExt.java

public static DocNameWithExt from(String filename) {
    String extension = FilenameUtils.getExtension(filename);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "expected a full filename (with extension)");
    return new DocNameWithExt(filename);
}

From source file:org.haiku.haikudepotserver.dataobjects.PkgCategory.java

/**
 * <p>Given the codes supplied, this method will return all of the categories for
 * those codes.</p>//from   w  w w .  j  av a2s .  co  m
 */

public static List<PkgCategory> getByCodes(ObjectContext context, final Collection<String> codes) {
    Preconditions.checkArgument(null != context, "the context must be provided");
    Preconditions.checkArgument(null != codes, "the codes must be provided");
    return getAll(context).stream().filter(pc -> codes.contains(pc.getCode())).collect(Collectors.toList());
}

From source file:org.excalibur.service.manager.NodeManagerFactory.java

public static NodeManager getManagerReference() {
    try {//from  w w w.  ja va 2 s  . com
        LATCH.await();
        NodeManager manager = manager_.get();
        Preconditions.checkArgument(manager != null, "call setManager(manager)");

        return manager_.get();

    } catch (InterruptedException e) {
        Thread.interrupted();
        AnyThrow.throwUncheked(e);
    }

    return null;
}