Example usage for com.google.common.primitives Shorts checkedCast

List of usage examples for com.google.common.primitives Shorts checkedCast

Introduction

In this page you can find the example usage for com.google.common.primitives Shorts checkedCast.

Prototype

public static short checkedCast(long value) 

Source Link

Document

Returns the short value that is equal to value , if possible.

Usage

From source file:com.ning.billing.meter.timeline.samples.ScalarSample.java

public static ScalarSample fromObject(final Object sampleValue) {
    if (sampleValue == null) {
        return new ScalarSample<Void>(SampleOpcode.NULL, null);
    } else if (sampleValue instanceof Byte) {
        return new ScalarSample<Byte>(SampleOpcode.BYTE, (Byte) sampleValue);
    } else if (sampleValue instanceof Short) {
        return new ScalarSample<Short>(SampleOpcode.SHORT, (Short) sampleValue);
    } else if (sampleValue instanceof Integer) {
        try {//  w w  w  . j a  v  a  2s  .com
            // Can it fit in a short?
            final short optimizedShort = Shorts.checkedCast(Long.valueOf(sampleValue.toString()));
            return new ScalarSample<Short>(SampleOpcode.SHORT, optimizedShort);
        } catch (IllegalArgumentException e) {
            return new ScalarSample<Integer>(SampleOpcode.INT, (Integer) sampleValue);
        }
    } else if (sampleValue instanceof Long) {
        try {
            // Can it fit in a short?
            final short optimizedShort = Shorts.checkedCast(Long.valueOf(sampleValue.toString()));
            return new ScalarSample<Short>(SampleOpcode.SHORT, optimizedShort);
        } catch (IllegalArgumentException e) {
            try {
                // Can it fit in an int?
                final int optimizedLong = Ints.checkedCast(Long.valueOf(sampleValue.toString()));
                return new ScalarSample<Integer>(SampleOpcode.INT, optimizedLong);
            } catch (IllegalArgumentException ohWell) {
                return new ScalarSample<Long>(SampleOpcode.LONG, (Long) sampleValue);
            }
        }
    } else if (sampleValue instanceof Float) {
        return new ScalarSample<Float>(SampleOpcode.FLOAT, (Float) sampleValue);
    } else if (sampleValue instanceof Double) {
        return new ScalarSample<Double>(SampleOpcode.DOUBLE, (Double) sampleValue);
    } else {
        return new ScalarSample<String>(SampleOpcode.STRING, sampleValue.toString());
    }
}

From source file:io.prestosql.type.SmallintOperators.java

@ScalarOperator(ADD)
@SqlType(StandardTypes.SMALLINT)/*from   w  w w  . ja v  a2  s.  c om*/
public static long add(@SqlType(StandardTypes.SMALLINT) long left,
        @SqlType(StandardTypes.SMALLINT) long right) {
    try {
        return Shorts.checkedCast(left + right);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE,
                format("smallint addition overflow: %s + %s", left, right), e);
    }
}

From source file:edu.mit.streamjit.util.bytecode.Field.java

public Field(java.lang.reflect.Field f, Klass parent, Module module) {
    super(module.types().getFieldType(f), f.getName());
    //parent is set by our parent adding us to its list prior to making it
    //unmodifiable.  (We can't add ourselves and have the list wrapped
    //unmodifiable later because it's stored in a final field.)
    this.modifiers = Sets.immutableEnumSet(Modifier.fromFieldBits(Shorts.checkedCast(f.getModifiers())));
}

From source file:com.facebook.buck.android.resources.ResChunk.java

ResChunk(int chunkType, int headerSize, int chunkSize) {
    this.type = Shorts.checkedCast(chunkType);
    this.headerSize = Shorts.checkedCast(headerSize);
    this.chunkSize = chunkSize;
    Preconditions.checkState((chunkSize % 4) == 0);
}

From source file:io.prestosql.type.SmallintOperators.java

@ScalarOperator(SUBTRACT)
@SqlType(StandardTypes.SMALLINT)//from w w w .  j  av a2  s  . com
public static long subtract(@SqlType(StandardTypes.SMALLINT) long left,
        @SqlType(StandardTypes.SMALLINT) long right) {
    try {
        return Shorts.checkedCast(left - right);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE,
                format("smallint subtraction overflow: %s - %s", left, right), e);
    }
}

From source file:io.prestosql.plugin.jdbc.StandardColumnMappings.java

public static LongWriteFunction smallintWriteFunction() {
    return (statement, index, value) -> statement.setShort(index, Shorts.checkedCast(value));
}

From source file:io.prestosql.type.SmallintOperators.java

@ScalarOperator(MULTIPLY)
@SqlType(StandardTypes.SMALLINT)//from  ww  w  .ja v a2  s  .c o m
public static long multiply(@SqlType(StandardTypes.SMALLINT) long left,
        @SqlType(StandardTypes.SMALLINT) long right) {
    try {
        return Shorts.checkedCast(left * right);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE,
                format("smallint multiplication overflow: %s * %s", left, right), e);
    }
}

From source file:org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleExtendedCommunityRegistry.java

@Override
public void serializeExtendedCommunity(final ExtendedCommunities extendedCommunity,
        final ByteBuf byteAggregator) {
    final ExtendedCommunitySerializer serializer = this.handlers
            .getSerializer(extendedCommunity.getExtendedCommunity().getImplementedInterface());
    if (serializer == null) {
        return;//from  www.  java  2s.co  m
    }
    ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getType(extendedCommunity.isTransitive())),
            byteAggregator);
    ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getSubType()), byteAggregator);
    serializer.serializeExtendedCommunity(extendedCommunity.getExtendedCommunity(), byteAggregator);
}

From source file:com.github.hexosse.grounditem.utils.JsonItemStack.java

public final ItemStack toItemStack() {
    final ItemStack itemStack = new ItemStack(type == null ? Material.GRASS : Material.valueOf(type),
            Ints.checkedCast(amount), Shorts.checkedCast(damage));
    final ItemMeta meta = itemStack.getItemMeta();

    if (name != null)
        meta.setDisplayName(name);//from w  w w  . j ava  2s  .  co m

    if (lore != null)
        meta.setLore(lore);

    if (enchantments.size() != 0) {
        for (final Map.Entry<String, Long> entry : enchantments.entrySet()) {
            meta.addEnchant(Enchantment.getByName(entry.getKey()), Ints.checkedCast(entry.getValue()), true);
        }
    }

    itemStack.setItemMeta(meta);

    return itemStack;
}

From source file:edu.mit.streamjit.util.bytecode.Modifier.java

private Modifier(int bit) {
    this.bit = Shorts.checkedCast(bit);
}