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

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

Introduction

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

Prototype

public static <T> T checkNotNull(T reference) 

Source Link

Document

Ensures that an object reference passed as a parameter to the calling method is not null.

Usage

From source file:org.haiku.haikudepotserver.support.web.NaturalLanguageWebHelper.java

/**
 * <p>This will look at parameters on the supplied request and will return a natural language.  It will
 * resort to English language if no other language is able to be derived.</p>
 *//*from w w w  .java  2s  . c o  m*/

public static NaturalLanguage deriveNaturalLanguage(ObjectContext context, HttpServletRequest request) {
    Preconditions.checkNotNull(context);

    if (null != request) {
        String naturalLanguageCode = request.getParameter(WebConstants.KEY_NATURALLANGUAGECODE);

        if (!Strings.isNullOrEmpty(naturalLanguageCode)) {
            Optional<NaturalLanguage> naturalLanguageOptional = NaturalLanguage.getByCode(context,
                    naturalLanguageCode);

            if (naturalLanguageOptional.isPresent()) {
                return naturalLanguageOptional.get();
            } else {
                LOGGER.info("the natural language '{}' was specified, but was not able to be found",
                        naturalLanguageCode);
            }
        }

        // see if we can deduce it from the locale.

        Locale locale = request.getLocale();

        if (null != locale) {
            Iterator<String> langI = Splitter.on(Pattern.compile("[-_]")).split(locale.toLanguageTag())
                    .iterator();

            if (langI.hasNext()) {
                Optional<NaturalLanguage> naturalLanguageOptional = NaturalLanguage.getByCode(context,
                        langI.next());

                if (naturalLanguageOptional.isPresent() && naturalLanguageOptional.get().getIsPopular()) {
                    return naturalLanguageOptional.get();
                }
            }

        }
    }

    return NaturalLanguage.getByCode(context, NaturalLanguage.CODE_ENGLISH).get();
}

From source file:org.opendaylight.controller.netconf.util.messages.NetconfMessageAdditionalHeader.java

public static String toString(String userName, String hostAddress, String port, String transport,
        Optional<String> sessionIdentifier) {
    Preconditions.checkNotNull(userName);
    Preconditions.checkNotNull(hostAddress);
    Preconditions.checkNotNull(port);/*from   w w w  . j  av a  2 s.  c  o m*/
    Preconditions.checkNotNull(transport);
    String identifier = sessionIdentifier.isPresent() ? sessionIdentifier.get() : "";
    return "[" + userName + SC + hostAddress + ":" + port + SC + transport + SC + identifier + SC + "]"
            + System.lineSeparator();
}

From source file:com.visural.common.collection.FluentLists.java

public static <T> List<T> add(List<T> list, int index, T element) {
    Preconditions.checkNotNull(list);
    index = basicAdjustIndex(index, list);
    list.add(index, element);/*from  w ww .j a  v  a2 s  .  co m*/
    return list;
}

From source file:com.google.template.soy.types.VeType.java

public static VeType of(String dataType) {
    Preconditions.checkNotNull(dataType);
    if (NullType.getInstance().toString().equals(dataType)) {
        return NO_DATA;
    }//w w w .  j a v a  2  s  . c  o  m
    return new VeType(Optional.of(dataType));
}

From source file:org.eclipse.buildship.core.util.gradle.GradleDistributionFormatter.java

/**
 * Converts the given {@link GradleDistribution} to a human-readable {@link String}.
 *
 * @param gradleDistribution the Gradle distribution to stringify
 * @return the resulting string//w  w  w  . j ava 2  s  . co m
 */
public static String toString(GradleDistribution gradleDistribution) {
    Preconditions.checkNotNull(gradleDistribution);

    GradleDistributionWrapper gradleDistributionWrapper = GradleDistributionWrapper.from(gradleDistribution);
    return toString(gradleDistributionWrapper);
}

From source file:com.facebook.buck.util.MoreStreams.java

public static void copyExactly(InputStream source, OutputStream destination, long bytesToRead)
        throws IOException {
    Preconditions.checkNotNull(source);
    Preconditions.checkNotNull(destination);
    byte[] buffer = new byte[READ_BUFFER_SIZE_BYTES];

    long totalReadByteCount = 0;
    while (totalReadByteCount < bytesToRead) {
        int maxBytesToReadNext = (int) Math.min(bytesToRead - totalReadByteCount, buffer.length);
        int lastReadByteCount = source.read(buffer, 0, maxBytesToReadNext);

        if (lastReadByteCount == -1) {
            String msg = String.format(
                    "InputStream was missing [%d] bytes. Expected to read a total of [%d] bytes.",
                    bytesToRead - totalReadByteCount, bytesToRead);
            LOG.error(msg);/*from  ww  w .  j  av a  2s .  com*/
            throw new IOException(msg);
        }

        destination.write(buffer, 0, lastReadByteCount);
        totalReadByteCount += lastReadByteCount;
    }
}

From source file:org.opendaylight.iotdm.onem2m.core.utils.JsonUtils.java

public static JSONObject append(JSONObject jsonObject, String key, Object value) {
    Preconditions.checkNotNull(key);
    try {/*from w  w  w .  j a  va 2  s. co m*/
        jsonObject.append(key, value);
    } catch (JSONException e) {
        // TODO Determine when this can happen
    }
    return jsonObject;
}

From source file:com.google.devtools.build.xcode.util.Interspersing.java

/**
 * Inserts {@code what} before each item in {@code sequence}, returning a lazy sequence of twice
 * the length.//from ww  w . j  ava2  s . c  o  m
 */
public static <E> Iterable<E> beforeEach(final E what, Iterable<E> sequence) {
    Preconditions.checkNotNull(what);
    return Iterables.concat(Iterables.transform(sequence, new Function<E, Iterable<E>>() {
        @Override
        public Iterable<E> apply(E element) {
            return ImmutableList.of(what, element);
        }
    }));
}

From source file:org.chiefly.nautilus.Closeables.java

/**
 * @param closeables The {@link Closeable}s to be closed.
 * @throws IOException if an I/O error occurs.
 */// www . j a v a  2 s .  c o  m
public static void closeAll(final Closeable... closeables) throws IOException {

    /* Arguments checks */
    Preconditions.checkNotNull(closeables);
    if (closeables.length == 0) {
        return;
    }

    /* Start closing */
    closeAll(0, closeables);
}

From source file:org.ros.message.MessageIdentifier.java

public static MessageIdentifier of(String type) {
    Preconditions.checkNotNull(type);
    // We're not using Preconditions.checkArgument() here because we want a
    // useful error message without paying the performance penalty of
    // constructing it every time.
    if (!type.contains("/")) {
        throw new IllegalArgumentException(
                String.format("Type name is invalid or not fully qualified: \"%s\"", type));
    }/*w  w w  .  j av  a2 s  .co m*/
    return new MessageIdentifier(type);
}