Equals implements Operator<Boolean> { @Nullable @Override public Boolean apply( OperatorContext context, BaseEventTypeNode eventNode, FilterOperand[] operands) throws UaException { validate(context, operands); FilterOperand op0 = operands[0]; FilterOperand op1 = operands[1]; Object value0 = context.resolve(op0, eventNode); Object value1 = context.resolve(op1, eventNode); if (value0 == null || value1 == null) { return null; } BuiltinDataType dt0 = getType(value0); BuiltinDataType dt1 = getType(value1); if (dt0 == null || dt1 == null) { throw new UaException(StatusCodes.Bad_UnexpectedError); } int p0 = ImplicitConversions.getPrecedence(dt0); int p1 = ImplicitConversions.getPrecedence(dt1); if (p0 == p1) { assert dt0 == dt1; return equals(value0, value1); } else if (p0 >= p1) { Object converted1 = convert(value1, dt0); return equals(value0, converted1); } else { Object converted0 = convert(value0, dt1); return equals(converted0, value1); } } Equals(); @Override void validate(FilterContext context, FilterOperand[] operands); @Nullable @Override Boolean apply(
        OperatorContext context,
        BaseEventTypeNode eventNode,
        FilterOperand[] operands);  }
@Test public void testArray() throws Exception { OperatorContext context = mock(OperatorContext.class); BaseEventTypeNode eventNode = mock(BaseEventTypeNode.class); FilterOperand op0 = new LiteralOperand(new Variant(new int[]{1, 2, 3})); FilterOperand op1 = new LiteralOperand(new Variant(new int[]{1, 2, 3})); when(context.resolve(op0, eventNode)) .thenReturn(new int[]{1, 2, 3}); when(context.resolve(op1, eventNode)) .thenReturn(new int[]{1, 2, 3}); Boolean result = Operators.EQUALS.apply( context, eventNode, new FilterOperand[]{op0, op1} ); assertNotNull(result); assertTrue(result); }
@Test public void testScalar() throws Exception { OperatorContext context = mock(OperatorContext.class); BaseEventTypeNode eventNode = mock(BaseEventTypeNode.class); FilterOperand op0 = new LiteralOperand(new Variant(42)); FilterOperand op1 = new LiteralOperand(new Variant(42)); when(context.resolve(op0, eventNode)) .thenReturn(42); when(context.resolve(op1, eventNode)) .thenReturn(42); Boolean result = Operators.EQUALS.apply( context, eventNode, new FilterOperand[]{op0, op1} ); assertNotNull(result); assertTrue(result); }
@Test public void testArrayWithImplicitConversion() throws Exception { OperatorContext context = mock(OperatorContext.class); BaseEventTypeNode eventNode = mock(BaseEventTypeNode.class); FilterOperand op0 = new LiteralOperand(new Variant(new int[]{1, 2, 3})); FilterOperand op1 = new LiteralOperand(new Variant(new long[]{1L, 2L, 3L})); when(context.resolve(op0, eventNode)) .thenReturn(new int[]{1, 2, 3}); when(context.resolve(op1, eventNode)) .thenReturn(new Long[]{1L, 2L, 3L}); Boolean result = Operators.EQUALS.apply( context, eventNode, new FilterOperand[]{op0, op1} ); assertNotNull(result); assertTrue(result); }
@Test public void testScalarWithImplicitConversion() throws Exception { OperatorContext context = mock(OperatorContext.class); BaseEventTypeNode eventNode = mock(BaseEventTypeNode.class); FilterOperand op0 = new LiteralOperand(new Variant(42)); FilterOperand op1 = new LiteralOperand(new Variant(42L)); when(context.resolve(op0, eventNode)) .thenReturn(42); when(context.resolve(op1, eventNode)) .thenReturn(42L); Boolean result = Operators.EQUALS.apply( context, eventNode, new FilterOperand[]{op0, op1} ); assertNotNull(result); assertTrue(result); }
@Test public void testArrayNotEqual() throws Exception { OperatorContext context = mock(OperatorContext.class); BaseEventTypeNode eventNode = mock(BaseEventTypeNode.class); FilterOperand op0 = new LiteralOperand(new Variant(new int[]{1, 2, 3})); FilterOperand op1 = new LiteralOperand(new Variant(new int[]{3, 2, 1})); when(context.resolve(op0, eventNode)) .thenReturn(new int[]{1, 2, 3}); when(context.resolve(op1, eventNode)) .thenReturn(new int[]{3, 2, 1}); Boolean result = Operators.EQUALS.apply( context, eventNode, new FilterOperand[]{op0, op1} ); assertNotNull(result); assertFalse(result); }
@Test public void testScalarNotEqual() throws Exception { OperatorContext context = mock(OperatorContext.class); BaseEventTypeNode eventNode = mock(BaseEventTypeNode.class); FilterOperand op0 = new LiteralOperand(new Variant(1)); FilterOperand op1 = new LiteralOperand(new Variant(2)); when(context.resolve(op0, eventNode)) .thenReturn(1); when(context.resolve(op1, eventNode)) .thenReturn(2); Boolean result = Operators.EQUALS.apply( context, eventNode, new FilterOperand[]{op0, op1} ); assertNotNull(result); assertFalse(result); }