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, @Nullable Object errorMessage) 

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.spectralogic.ds3client.helpers.strategy.transferstrategy.RangeHelper.java

protected static ImmutableCollection<Range> replaceRange(final ImmutableCollection<Range> existingRanges,
        final long numBytesTransferred, final long intendedNumBytesToTransfer) {
    Preconditions.checkState(numBytesTransferred >= 0, "numBytesTransferred must be >= 0.");
    Preconditions.checkState(intendedNumBytesToTransfer > 0, "intendedNumBytesToTransfer must be > 0.");
    Preconditions.checkState(intendedNumBytesToTransfer > numBytesTransferred,
            "intendedNumBytesToTransfer must be > numBytesTransferred");

    if (Guard.isNullOrEmpty(existingRanges)) {
        return ImmutableList
                .of(Range.byLength(numBytesTransferred, intendedNumBytesToTransfer - numBytesTransferred));
    }//from  w  w w  .  j a  v  a 2  s .  c  om

    final ImmutableList.Builder<Range> newRangesbuilder = ImmutableList.builder();

    final UnmodifiableIterator<Range> existingRangesIterator = existingRanges.iterator();

    long previousAccumulatedBytesInRanges = 0;
    long currentAccumulatedBytesInRanges = existingRanges.iterator().next().getLength();

    while (existingRangesIterator.hasNext()) {
        final Range existingRange = existingRangesIterator.next();

        if (numBytesTransferred < currentAccumulatedBytesInRanges) {
            final Range firstNewRange = Range.byPosition(
                    existingRange.getStart() - previousAccumulatedBytesInRanges + numBytesTransferred,
                    existingRange.getEnd());
            newRangesbuilder.add(firstNewRange);

            addRemainingRanges(existingRangesIterator, newRangesbuilder);
            break;
        }

        previousAccumulatedBytesInRanges += existingRange.getLength();
        currentAccumulatedBytesInRanges += existingRange.getLength();
    }

    return newRangesbuilder.build();
}

From source file:org.apache.james.modules.mailbox.ListenerConfiguration.java

public static ListenerConfiguration from(HierarchicalConfiguration configuration) {
    String listenerClass = configuration.getString("class");
    Preconditions.checkState(!Strings.isNullOrEmpty(listenerClass), "class name is mandatory");
    Optional<Boolean> isAsync = Optional.ofNullable(configuration.getBoolean("async", null));
    return new ListenerConfiguration(listenerClass, extractSubconfiguration(configuration), isAsync);
}

From source file:com.google.api.tools.framework.importers.swagger.SwaggerFileWriter.java

/** Saves the file contents on the disk and returns the saved file paths. */
public static Map<String, FileWrapper> saveFilesOnDisk(List<FileWrapper> inputFiles) {

    ImmutableMap.Builder<String, FileWrapper> savedFiles = ImmutableMap.builder();
    File tempDir = Files.createTempDir();
    String tmpDirLocation = tempDir.getAbsolutePath();
    for (FileWrapper inputFile : inputFiles) {
        String filePath = inputFile.getFilename().replaceAll("[\\\\/:]", "_");

        Preconditions.checkState(!Strings.isNullOrEmpty(inputFile.getFileContents().toString()),
                "swagger spec file contents empty");
        Preconditions.checkState(!Strings.isNullOrEmpty(filePath), "swagger spec file path not provided");

        String filePathToSave = File.separator + tmpDirLocation + File.separator + "swagger_spec_files"
                + File.separator + filePath;
        FileWrapper fileToSave = FileWrapper.create(filePathToSave, inputFile.getFileContents());
        try {/* www  . j a va  2s  .c o  m*/
            saveFileOnDisk(fileToSave);
            savedFiles.put(inputFile.getFilename(), fileToSave);
        } catch (IOException ex) {
            throw new IllegalStateException(
                    String.format("Unable to save the swagger spec contents on the disk at %s", filePathToSave),
                    ex);
        }
    }
    return savedFiles.build();
}

From source file:net.minecraftforge.server.permission.PermissionAPI.java

/**
 * <b>Only use this in PreInit state!</b>
 *///from   w  ww  . ja v a2 s . co  m
public static void setPermissionHandler(IPermissionHandler handler) {
    Preconditions.checkNotNull(handler, "Permission handler can't be null!");
    Preconditions.checkState(
            Loader.instance().getLoaderState().ordinal() <= LoaderState.PREINITIALIZATION.ordinal(),
            "Can't register after IPermissionHandler PreInit!");
    FMLLog.log(Level.WARN,
            "Replacing " + permissionHandler.getClass().getName() + " with " + handler.getClass().getName());
    permissionHandler = handler;
}

From source file:org.haiku.haikudepotserver.pkg.model.PkgIconConfiguration.java

public PkgIconConfiguration(MediaType mediaType, Integer size) {
    Preconditions.checkState(null != mediaType, "the media type is required");
    Preconditions.checkState(null == size || size > 0, "illegal size value for icon configuration");
    this.mediaType = mediaType;
    this.size = size;
}

From source file:com.google.cloud.bigtable.hbase.adapters.filters.FilterAdapterHelper.java

/**
 * Extract a single family name from a FilterAdapterContext. Throws if there
 * is not exactly 1 family present in the scan.
 *
 * @param context a {@link com.google.cloud.bigtable.hbase.adapters.filters.FilterAdapterContext} object.
 * @return a {@link java.lang.String} object.
 *///from   w w  w.  ja va 2s  .c  o  m
public static String getSingleFamilyName(FilterAdapterContext context) {
    Preconditions.checkState(context.getScan().numFamilies() == 1,
            "Cannot getSingleFamilyName() unless there is exactly 1 family.");
    return Bytes.toString(context.getScan().getFamilies()[0]);
}

From source file:com.enonic.cms.core.portal.rendering.tracing.RenderTrace.java

private static RenderTraceHistory getHistoryFromSession() {
    final UserKey userKey = PortalSecurityHolder.getLoggedInUser();
    final HttpSession session = getSession();

    final RenderTraceHistory history = RenderTraceHistory.getFromSession(session, userKey);
    Preconditions.checkState(history != null, "Could not find RenderTraceHistory for user [" + userKey
            + "] in session [" + session.getId() + "]");
    return history;
}

From source file:org.xacml4j.v30.marshal.jaxb.JAXBContextUtil.java

public static JAXBContext getInstance() {
    Preconditions.checkState(INSTANCE != null, "Failed to initialize JAXB context");
    return INSTANCE;
}

From source file:org.waveprotocol.box.server.authentication.AccountStoreHolder.java

synchronized public static void init(AccountStore store, String defaultDomain) {
    Preconditions.checkNotNull(store, "Account store cannot be null");
    Preconditions.checkNotNull(defaultDomain, "Default domain cannot be null");
    Preconditions.checkState(AccountStoreHolder.store == null, "Account store already set");
    AccountStoreHolder.store = store;//from w w w  .  j a  va2 s  .c o  m
    AccountStoreHolder.defaultDomain = defaultDomain.toLowerCase();
}

From source file:me.lucko.luckperms.common.api.delegates.model.ApiGroup.java

public static me.lucko.luckperms.common.model.Group cast(Group g) {
    Preconditions.checkState(g instanceof ApiGroup,
            "Illegal instance " + g.getClass() + " cannot be handled by this implementation.");
    return ((ApiGroup) g).getHandle();
}