JBBPOnlyFieldEvaluator implements JBBPIntegerValueEvaluator { @Override public int eval(final JBBPBitInputStream inStream, final int currentCompiledBlockOffset, final JBBPCompiledBlock block, final JBBPNamedNumericFieldMap fieldMap) { final int result; if (this.externalFieldName == null) { final JBBPNamedFieldInfo namedField = block.getNamedFields()[this.namedFieldIndex]; final JBBPNumericField numericField = fieldMap.get(namedField); if (numericField == null) { throw new java.lang.ArithmeticException("Can't find field '" + namedField.getFieldName() + "' among numeric fields"); } else { result = numericField.getAsInt(); } } else { result = this.externalFieldName.equals("$") ? (int) inStream.getCounter() : fieldMap.getExternalFieldValue(this.externalFieldName, block, this); } return result; } JBBPOnlyFieldEvaluator(final String externalFieldName, final int namedFieldIndex); @Override int eval(final JBBPBitInputStream inStream, final int currentCompiledBlockOffset, final JBBPCompiledBlock block, final JBBPNamedNumericFieldMap fieldMap); @Override String toString(); @Override void visitItems(final JBBPCompiledBlock block, final int currentCompiledBlockOffset, final ExpressionEvaluatorVisitor visitor);  }
@Test public void testNumericValue() { final int value = 1234; final JBBPNamedNumericFieldMap map = new JBBPNamedNumericFieldMap(); final JBBPNamedFieldInfo nameInfo = new JBBPNamedFieldInfo("value", "value", value); map.putField(new JBBPFieldInt(nameInfo, value)); final List<JBBPNamedFieldInfo> list = new ArrayList<>(); list.add(nameInfo); final byte[] compiled = new byte[] {0}; final JBBPCompiledBlock compiledBlock = JBBPCompiledBlock.prepare().setCompiledData(compiled).setSource("none").setNamedFieldData(list).build(); JBBPOnlyFieldEvaluator expr = new JBBPOnlyFieldEvaluator(null, 0); assertEquals(value, expr.eval(null, 0, compiledBlock, map)); }
@Test public void testExternalValue() { final int value = 1234; final JBBPNamedNumericFieldMap map = new JBBPNamedNumericFieldMap((fieldName, numericFieldMap, compiledBlock) -> { if (fieldName.equals("value")) { return value; } assertNotNull(numericFieldMap); assertNotNull(compiledBlock); fail("Unexpected request for value [" + fieldName + ']'); return -1; }); final List<JBBPNamedFieldInfo> list = new ArrayList<>(); final byte[] compiled = new byte[] {0}; final JBBPCompiledBlock compiledBlock = JBBPCompiledBlock.prepare().setCompiledData(compiled).setSource("none").setNamedFieldData(list).build(); JBBPOnlyFieldEvaluator expr = new JBBPOnlyFieldEvaluator("value", -1); assertEquals(value, expr.eval(null, 0, compiledBlock, map)); }
@Test public void testExternalValueNamedAsFirstCharDollar() { final int value = 1234; final JBBPNamedNumericFieldMap map = new JBBPNamedNumericFieldMap((fieldName, numericFieldMap, compiledBlock) -> { if (fieldName.equals("$value")) { return value; } assertNotNull(numericFieldMap); assertNotNull(compiledBlock); fail("Unexpected request for value [" + fieldName + ']'); return -1; }); final List<JBBPNamedFieldInfo> list = new ArrayList<>(); final byte[] compiled = new byte[] {0}; final JBBPCompiledBlock compiledBlock = JBBPCompiledBlock.prepare().setCompiledData(compiled).setSource("none").setNamedFieldData(list).build(); JBBPOnlyFieldEvaluator expr = new JBBPOnlyFieldEvaluator("$value", -1); assertEquals(value, expr.eval(null, 0, compiledBlock, map)); }
@Test public void testCounterOfStreamAsParameter() throws Exception { final List<JBBPNamedFieldInfo> list = new ArrayList<>(); final byte[] compiled = new byte[] {0}; final JBBPCompiledBlock compiledBlock = JBBPCompiledBlock.prepare().setCompiledData(compiled).setSource("none").setNamedFieldData(list).build(); JBBPOnlyFieldEvaluator expr = new JBBPOnlyFieldEvaluator("$", -1); final JBBPBitInputStream inStream = new JBBPBitInputStream(new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5})); inStream.read(); inStream.read(); inStream.read(); assertEquals(3, expr.eval(inStream, 0, compiledBlock, null)); }