Example usage for com.google.common.primitives UnsignedInts parseUnsignedInt

List of usage examples for com.google.common.primitives UnsignedInts parseUnsignedInt

Introduction

In this page you can find the example usage for com.google.common.primitives UnsignedInts parseUnsignedInt.

Prototype

public static int parseUnsignedInt(String string, int radix) 

Source Link

Document

Returns the unsigned int value represented by a string with the given radix.

Usage

From source file:io.opencensus.contrib.http.util.CloudTraceFormat.java

@Override
public <C /*>>> extends @NonNull Object*/> SpanContext extract(C carrier, Getter<C> getter)
        throws SpanContextParseException {
    checkNotNull(carrier, "carrier");
    checkNotNull(getter, "getter");
    try {//ww  w .ja  v  a2 s  .co m
        String headerStr = getter.get(carrier, HEADER_NAME);
        if (headerStr == null || headerStr.length() < MIN_HEADER_SIZE) {
            throw new SpanContextParseException("Missing or too short header: " + HEADER_NAME);
        }
        checkArgument(headerStr.charAt(TRACE_ID_SIZE) == SPAN_ID_DELIMITER, "Invalid TRACE_ID size");

        TraceId traceId = TraceId.fromLowerBase16(headerStr.subSequence(0, TRACE_ID_SIZE));
        int traceOptionsPos = headerStr.indexOf(TRACE_OPTION_DELIMITER, TRACE_ID_SIZE);
        CharSequence spanIdStr = headerStr.subSequence(SPAN_ID_START_POS,
                traceOptionsPos < 0 ? headerStr.length() : traceOptionsPos);
        SpanId spanId = longToSpanId(UnsignedLongs.parseUnsignedLong(spanIdStr.toString(), 10));
        TraceOptions traceOptions = OPTIONS_NOT_SAMPLED;
        if (traceOptionsPos > 0) {
            String traceOptionsStr = headerStr.substring(traceOptionsPos + TRACE_OPTION_DELIMITER_SIZE);
            if ((UnsignedInts.parseUnsignedInt(traceOptionsStr, 10) & CLOUD_TRACE_IS_SAMPLED) != 0) {
                traceOptions = OPTIONS_SAMPLED;
            }
        }
        return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
    } catch (IllegalArgumentException e) {
        throw new SpanContextParseException("Invalid input", e);
    }
}