GeoTools : BetweenFilter

This shows how to create a Between Filter in code. It asks for all features with values between 5 and 15. This could also be done with a logic filter and PropertyIsGreaterThanOrEqualTo and PropertyIsLessThanOrEqualTo filters.

        FilterFactory ff = FilterFactory.createFilterFactory();
        BetweenFilter a= ff.createBetweenFilter();

        AttributeType a1 = attFactory.newAttributeType("value", Integer.class);
        AttributeType a2 = attFactory.newAttributeType("geometry",
                Geometry.class);
        FeatureType schema = FeatureTypeFactory.newFeatureType(new AttributeType[] {
                    a1, a2
                }, "testSchema");

        a.addLeftValue(new LiteralExpressionImpl(new Double(5)));
        a.addRightValue(new LiteralExpressionImpl(new Double(15)));
        a.addMiddleValue(new AttributeExpressionImpl(schema, "value"));

        Feature f1 = schema.create(new Object[] {
                    new Integer(12), new GeometryCollection(null, null, -1)
                });
        Feature f2 = schema.create(new Object[] {
                    new Integer(3), new GeometryCollection(null, null, -1)
                });
        Feature f3 = schema.create(new Object[] {
                    new Integer(15), new GeometryCollection(null, null, -1)
                });
        Feature f4 = schema.create(new Object[] {
                    new Integer(5), new GeometryCollection(null, null, -1)
                });
        Feature f5 = schema.create(new Object[] {
                    new Integer(30), new GeometryCollection(null, null, -1)
                });

        assertEquals(true, a.contains(f1)); // in between
        assertEquals(false, a.contains(f2)); // too small
        assertEquals(true, a.contains(f3)); // max value
        assertEquals(true, a.contains(f4)); // min value
        assertEquals(false, a.contains(f5)); // too large