DiscreteAttribute extends AbstractIndexableAttribute implements RangeAttribute { @Override public int getValueIndex(Object value) { if (value instanceof Integer || value instanceof Long || value instanceof Byte || value instanceof Short) { return indexMap.getIfAbsent(((Number)value).longValue(), -1); } else if (value instanceof String) { try { Long longValue = Long.valueOf((String)value); return indexMap.getIfAbsent(longValue, -1); } catch (NumberFormatException ex) { return -1; } } else { return -1; } } DiscreteAttribute(String key, AttributeLevel level); @Override int registerXAttribute(XAttribute att); @Override ImmutableList<Object> getValues(); @Override int getValueIndex(Object value); @Override Object getValue(int index); @Override int getValueSize(); @Override Object getMin(); @Override Object getMax();  }
@Test public void testGetValueIndex() { DiscreteAttribute att = this.newWithTwoValues("concept:name", AttributeLevel.EVENT); Assert.assertEquals(0, att.getValueIndex(100)); Assert.assertEquals(1, att.getValueIndex(200)); DiscreteAttribute att2 = this.newWithInvalidKeyAttribute("concept:name", AttributeLevel.EVENT); Object result2 = att2.getValueIndex(100); Assert.assertEquals(-1, result2); DiscreteAttribute att3 = this.newEmptyAttribute("concept:name", AttributeLevel.EVENT); Object result3 = att3.getValueIndex(100); Assert.assertEquals(-1, result3); }