Java Code Examples for com.google.common.primitives.Ints#indexOf()

The following examples show how to use com.google.common.primitives.Ints#indexOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bytecode.java    From Mixin with MIT License 6 votes vote down vote up
/**
 * If the supplied instruction is a constant, returns the constant value
 * from the instruction
 * 
 * @param insn constant instruction to process
 * @return the constant value or <tt>null</tt> if the value cannot be parsed
 *      (<tt>null</tt> constant is returned as <tt>Type.VOID_TYPE</tt>)
 */
public static Object getConstant(AbstractInsnNode insn) {
    if (insn == null) {
        return null;
    } else if (insn instanceof LdcInsnNode) {
        return ((LdcInsnNode)insn).cst;
    } else if (insn instanceof IntInsnNode) {
        int value = ((IntInsnNode)insn).operand;
        if (insn.getOpcode() == Opcodes.BIPUSH || insn.getOpcode() == Opcodes.SIPUSH) {
            return Integer.valueOf(value);
        }
        throw new IllegalArgumentException("IntInsnNode with invalid opcode " + insn.getOpcode() + " in getConstant");
    } else if (insn instanceof TypeInsnNode) {
        if (insn.getOpcode() < Opcodes.CHECKCAST) {
            return null; // Don't treat NEW and ANEWARRAY as constants 
        }
        return Type.getObjectType(((TypeInsnNode)insn).desc);
    }
    
    int index = Ints.indexOf(Bytecode.CONSTANTS_ALL, insn.getOpcode());
    return index < 0 ? null : Bytecode.CONSTANTS_VALUES[index];
}
 
Example 2
Source File: Conversions.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws NullPointerException
 *             if the wrapped array was <code>null</code>.
 */
@Override
public int indexOf(Object o) {
	// Will make the method fail if array is null.
	if (size() < 1) {
		return -1;
	}
	if (o instanceof Integer) {
		return Ints.indexOf(array, ((Integer) o).intValue());
	}
	return -1;
}
 
Example 3
Source File: Bytecode.java    From Mixin with MIT License 5 votes vote down vote up
/**
 * Returns the {@link Type} of a particular constant instruction's payload 
 * 
 * @param insn constant instruction
 * @return type of constant or <tt>null</tt> if it cannot be parsed (<tt>
 *      null</tt> constant is returned as <tt>Type.VOID_TYPE</tt>)
 */
public static Type getConstantType(AbstractInsnNode insn) {
    if (insn == null) {
        return null;
    } else if (insn instanceof LdcInsnNode) {
        Object cst = ((LdcInsnNode)insn).cst;
        if (cst instanceof Integer) {
            return Type.getType("I");
        } else if (cst instanceof Float) {
            return Type.getType("F");
        } else if (cst instanceof Long) {
            return Type.getType("J");
        } else if (cst instanceof Double) {
            return Type.getType("D");
        } else if (cst instanceof String) {
            return Type.getType(Constants.STRING_DESC);
        } else if (cst instanceof Type) {
            return Type.getType(Constants.CLASS_DESC);
        }
        throw new IllegalArgumentException("LdcInsnNode with invalid payload type " + cst.getClass() + " in getConstant");
    } else if (insn instanceof TypeInsnNode) {
        if (insn.getOpcode() < Opcodes.CHECKCAST) {
            return null; // Don't treat NEW and ANEWARRAY as constants 
        }
        return Type.getType(Constants.CLASS_DESC);
    }
    
    int index = Ints.indexOf(Bytecode.CONSTANTS_ALL, insn.getOpcode());
    return index < 0 ? null : Type.getType(Bytecode.CONSTANTS_TYPES[index]);
}
 
Example 4
Source File: ElementIndexInArray.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void element_index_of_ints_in_array_java_with_guava () {
	
	int [] twoQuarters = {1, 2, 3, 4, 5, 6};
	
	int lastMonthInFirstQuarter = Ints.indexOf(twoQuarters, 3);
	assertEquals(2, lastMonthInFirstQuarter);
}
 
Example 5
Source File: GuavaTutorial.java    From maven-framework-project with MIT License 3 votes vote down vote up
@Test
public void example20(){
	
	int[] array1 = { 1, 2, 3, 4, 5 };
	int a = 4;
	
	boolean contains = Ints.contains(array1, a);//判断数组中是否存在a元素
	
	System.out.println(contains);
	
	int indexOf = Ints.indexOf(array1, a); //获取该元素在数组中的下标
	
	System.out.println(indexOf);
	
	int max = Ints.max(array1);//求数组中的最大值
	
	System.out.println(max);
	
	int min = Ints.min(array1);//求数组中的最小值
	
	System.out.println(min);
	
	int[] array2 = {6, 7, 8, 9, 10};
	
	int[] concat = Ints.concat(array1, array2 );//合并数组
	
	System.out.println(Arrays.toString(concat));
	
}
 
Example 6
Source File: IntArray.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Find the index of the first occurrence of the specified value.
 * 
 * @param value  the value to find
 * @return the index of the value, -1 if not found
 */
public int indexOf(int value) {
  return Ints.indexOf(array, value);
}