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.talvish.tales.parts.RegularExpressionHelper.java

/**
 * Helper method that makes sure a user supplied regular expression
 * doesn't contain capturing groups, which would otherwise cause problems
 * if the regex is being used for needs where other capturing groups are
 * important//from w  w  w.  ja v a2  s . c  om
 * @param theRegEx the regex to escape
 * @return the non-capturing regex
 */
public final static String toNoncapturingExpression(String theRegEx) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(theRegEx), "need a regex");
    // I feel there is a better way, but this is good enough for now ...
    StringBuilder builder = new StringBuilder();

    int charClassCount = 0;

    int startOffset = 0;
    char currentChar;

    for (int offset = 0; offset < theRegEx.length(); offset += 1) {
        currentChar = theRegEx.charAt(offset);
        if (currentChar == '\\') { // ignore escaping character
            offset += 1;
        } else if (currentChar == '[') { // we are in a capturing group (java supports some nesting, though I don't fully here)
            charClassCount += 1;
        } else if (currentChar == ']') { // we are leaving one
            charClassCount -= 1;
        } else if (currentChar == '(' && charClassCount == 0) {
            if ((offset == theRegEx.length() - 1) || (theRegEx.charAt(offset + 1) != '?')) { // found at the end or next character isn't a quote/meaning non-capturing already
                builder.append(theRegEx.substring(startOffset, offset + 1));
                builder.append("?:"); // turn into a non capturing group
                startOffset = offset + 1;
            }
        }
    }
    builder.append(theRegEx.substring(startOffset, theRegEx.length()));
    return builder.toString();
}

From source file:com.jivesoftware.os.server.http.jetty.jersey.server.binding.Injectable.java

@SuppressWarnings("unchecked")
public static <T2> Injectable<T2> ofUnsafe(Class<T2> injectableType, Object instance) {
    Preconditions.checkNotNull(injectableType);
    Preconditions.checkNotNull(instance);
    Preconditions.checkArgument(injectableType.isAssignableFrom(instance.getClass()),
            "Injectable must be assignable to type '" + injectableType + "': " + instance);
    return new Injectable<>(injectableType, (T2) instance);
}

From source file:org.apache.mahout.df.data.DataUtils.java

/**
 * foreach i : array1[i] += array2[i]//  ww  w . j av a 2s  . co  m
 */
public static void add(int[] array1, int[] array2) {
    Preconditions.checkArgument(array1.length == array2.length, "array1.length != array2.length");
    for (int index = 0; index < array1.length; index++) {
        array1[index] += array2[index];
    }
}

From source file:com.metamx.druid.indexing.worker.TaskAnnouncement.java

public static TaskAnnouncement create(Task task, TaskStatus status) {
    Preconditions.checkArgument(status.getId().equals(task.getId()), "task id == status id");
    return new TaskAnnouncement(null, null, status, task.getTaskResource());
}

From source file:com.google.cloud.bigtable.grpc.ResultQueueEntry.java

public static <T> ResultQueueEntry<T> newThrowable(Throwable throwable) {
    Preconditions.checkArgument(throwable != null, "Throwable may not be null");
    return new ResultQueueEntry<>(throwable, null, false);
}

From source file:org.apache.gobblin.source.extractor.extract.kafka.KafkaUtils.java

/**
 * Get topic name from a {@link State} object. The {@link State} should contain property
 * {@link KafkaSource#TOPIC_NAME}.// w  ww .  j av  a2  s.c  o  m
 */
public static String getTopicName(State state) {
    Preconditions.checkArgument(state.contains(KafkaSource.TOPIC_NAME),
            "Missing configuration property " + KafkaSource.TOPIC_NAME);

    return state.getProp(KafkaSource.TOPIC_NAME);
}

From source file:org.apache.james.transport.mailets.remoteDelivery.Repeat.java

@SuppressWarnings("unchecked")
public static <T> List<T> repeat(T element, int times) {
    Preconditions.checkArgument(times >= 0, "Times argument should be strictly positive");
    return ImmutableList.copyOf(Iterables.limit(Iterables.cycle(element), times));
}

From source file:com.qcadoo.mes.materialFlowResources.constants.DocumentState.java

public static DocumentState of(final Entity entity) {
    Preconditions.checkArgument(entity != null, "Passed entity have to be non null");
    return parseString(entity.getStringField(DocumentFields.STATE));
}

From source file:com.facebook.presto.sql.planner.Symbol.java

public static Symbol fromQualifiedName(QualifiedName name) {
    Preconditions.checkArgument(!name.getPrefix().isPresent(),
            "Can't create a symbol from a qualified name with prefix");
    return new Symbol(name.getSuffix());
}

From source file:com.salesforce.jprotoc.ProtoTypeMap.java

/**
 * Returns an instance of {@link ProtoTypeMap} based on the given {@link DescriptorProtos.FileDescriptorProto}
 * instances.//  w  w w . ja v a2  s.  c om
 *
 * @param fileDescriptorProtos the full collection of files descriptors from the code generator request
 */
public static ProtoTypeMap of(@Nonnull Collection<DescriptorProtos.FileDescriptorProto> fileDescriptorProtos) {
    Preconditions.checkNotNull(fileDescriptorProtos, "fileDescriptorProtos");
    Preconditions.checkArgument(!fileDescriptorProtos.isEmpty(), "fileDescriptorProtos.isEmpty()");

    final ImmutableMap.Builder<String, String> types = ImmutableMap.builder();

    for (final DescriptorProtos.FileDescriptorProto fileDescriptor : fileDescriptorProtos) {
        final DescriptorProtos.FileOptions fileOptions = fileDescriptor.getOptions();

        final String protoPackage = fileDescriptor.hasPackage() ? "." + fileDescriptor.getPackage() : "";
        final String javaPackage = Strings.emptyToNull(
                fileOptions.hasJavaPackage() ? fileOptions.getJavaPackage() : fileDescriptor.getPackage());
        final String enclosingClassName = fileOptions.getJavaMultipleFiles() ? null
                : getJavaOuterClassname(fileDescriptor, fileOptions);

        fileDescriptor.getEnumTypeList().forEach(e -> types.put(protoPackage + "." + e.getName(),
                toJavaTypeName(e.getName(), enclosingClassName, javaPackage)));

        fileDescriptor.getMessageTypeList().forEach(m -> types.put(protoPackage + "." + m.getName(),
                toJavaTypeName(m.getName(), enclosingClassName, javaPackage)));
    }

    return new ProtoTypeMap(types.build());
}