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:org.apache.flume.tools.TimestampRoundDownUtil.java

/**
 *
 * @param timestamp - The time stamp to be rounded down.
 * @param roundDownSec - The <tt>timestamp</tt> is rounded down to the largest
 * multiple of <tt>roundDownSec</tt> seconds
 * less than or equal to <tt>timestamp.</tt> Should be between 0 and 60.
 * @return - Rounded down timestamp//from w  w w .  j a  v a 2s. co m
 * @throws IllegalStateException
 */
public static long roundDownTimeStampSeconds(long timestamp, int roundDownSec) throws IllegalStateException {
    Preconditions.checkArgument(roundDownSec > 0 && roundDownSec <= 60, "RoundDownSec must be > 0 and <=60");
    Calendar cal = roundDownField(timestamp, Calendar.SECOND, roundDownSec);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

From source file:com.github.jeluard.guayaba.util.ServiceLoaders.java

/**
 * @param <T>/*w w w . ja va 2  s .c om*/
 * @param type
 * @param classLoader
 * @return first implementation of `type` as reported by {@link ServiceLoader#load(java.lang.Class, java.lang.ClassLoader)}, if any
 */
public static <T> T load(final Class<T> type, final ClassLoader classLoader) {
    Preconditions.checkNotNull(type, "null type");
    Preconditions.checkNotNull(classLoader, "null classLoader");

    final Iterator<T> iterator = ServiceLoader.load(type, classLoader).iterator();

    Preconditions.checkArgument(iterator.hasNext(), "empty result");

    return iterator.next();
}

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

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.metamx.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  w  w.  ja  v a2 s. c  o  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")) {
        return new Function<String, DateTime>() {
            @Override
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                return new DateTime(Long.parseLong(ParserUtils.stripQuotes(input)) * 1000);
            }
        };
    } else if (format.equalsIgnoreCase("ruby")) {
        return new Function<String, DateTime>() {
            @Override
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                Double ts = Double.parseDouble(ParserUtils.stripQuotes(input));
                Long jts = ts.longValue() * 1000; // ignoring milli secs
                return new DateTime(jts);
            }
        };
    } else if (format.equalsIgnoreCase("millis")) {
        return new Function<String, DateTime>() {
            @Override
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                return new DateTime(Long.parseLong(ParserUtils.stripQuotes(input)));
            }
        };
    } else if (format.equalsIgnoreCase("nano")) {
        return new Function<String, DateTime>() {
            @Override
            public DateTime apply(String input) {
                Preconditions.checkArgument(input != null && !input.isEmpty(), "null timestamp");
                long timeNs = Long.parseLong(ParserUtils.stripQuotes(input));
                // Convert to milliseconds, effectively: ms = floor(time in ns / 1000000)
                return new DateTime(timeNs / 1000000L);
            }
        };
    } 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:com.google.devtools.build.importdeps.ResolutionFailureChain.java

public static ResolutionFailureChain createWithParent(ClassInfo resolutionStartClass,
        ImmutableList<ResolutionFailureChain> parentChains) {
    Preconditions.checkArgument(!parentChains.isEmpty(), "The parentChains cannot be empty.");
    return new AutoValue_ResolutionFailureChain(
            parentChains.stream().flatMap(chain -> chain.missingClasses().stream()).sorted().distinct()
                    .collect(ImmutableList.toImmutableList()),
            resolutionStartClass, parentChains);
}

From source file:pt.up.fe.specs.eclipse.builder.ExecTaskConfig.java

public static ExecTaskConfig parseArguments(List<String> arguments) {
    String workingDir = null;/*from  www .  j a v a 2  s.c  om*/

    while (!arguments.isEmpty() && arguments.get(0).startsWith("[")) {
        String option = arguments.remove(0);
        // Remove square brackets
        Preconditions.checkArgument(option.endsWith("]"), "Expected option to end with ]: " + option);

        String workOption = option.substring(1, option.length() - 1);

        int equalIndex = workOption.indexOf('=');
        Preconditions.checkArgument(equalIndex != -1, "Expected an equals: " + option);

        String key = workOption.substring(0, equalIndex);
        String value = workOption.substring(equalIndex + 1, workOption.length());
        switch (key.toLowerCase()) {
        case "dir":
            workingDir = value;
            break;
        default:
            throw new RuntimeException(
                    "Property '" + key + "' not supported, supported properties: " + SUPPORTED_PROPERTIES);
        }
    }

    return new ExecTaskConfig(workingDir);
}

From source file:co.cask.cdap.internal.app.workflow.WorkflowNodeCreator.java

static WorkflowNode createWorkflowActionNode(String programName, SchedulableProgramType programType) {
    switch (programType) {
    case MAPREDUCE:
        Preconditions.checkNotNull(programName, "MapReduce name is null.");
        Preconditions.checkArgument(!programName.isEmpty(), "MapReduce name is empty.");
        break;//from  w  ww . j  a  v a2s  .c  o  m
    case SPARK:
        Preconditions.checkNotNull(programName, "Spark name is null.");
        Preconditions.checkArgument(!programName.isEmpty(), "Spark name is empty.");
        break;
    case CUSTOM_ACTION:
        //no-op
        break;
    default:
        break;
    }

    return new WorkflowActionNode(programName, new ScheduleProgramInfo(programType, programName));
}

From source file:org.thelq.stackexchange.api.queries.QueryUtils.java

public static void putIfNotNull(LinkedHashMap<String, String> finalParameters, String key, Object value) {
    if (value == null)
        return;//from   w  ww.  j a  v  a  2  s .  c  o  m
    String valueString = String.valueOf(value);
    Preconditions.checkArgument(StringUtils.isNotBlank(valueString), "Value cannot be blank");
    finalParameters.put(key, valueString);
}

From source file:io.druid.indexing.worker.TaskAnnouncement.java

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

From source file:eu.stratosphere.sopremo.operator.PactBuilderUtil.java

public static void addKeys(ReduceContract.Builder builder, Class<? extends Key>[] keyClasses,
        int[] keyIndices) {
    Preconditions.checkArgument(keyClasses.length == keyIndices.length,
            "Lenght of keyClasses and keyIndices must match.");
    for (int i = 0; i < keyClasses.length; ++i) {
        builder.keyField(keyClasses[i], keyIndices[i]);
    }//from  w w w .j  ava  2s  .co m
}