@@ -174,13 +174,22 @@ func MinRSSI(dbfs float64) AircraftFilter {
|
||||
return func(a *Aircraft) bool { return a.RSSI >= dbfs }
|
||||
}
|
||||
|
||||
// And, Or, and Not compose filters as boolean operators. Filter/All already AND
|
||||
// their arguments; And is the value form, letting you nest, e.g.
|
||||
// Or(And(a, b), And(c, d)).
|
||||
|
||||
// Not inverts a filter.
|
||||
func Not(f AircraftFilter) AircraftFilter {
|
||||
return func(a *Aircraft) bool { return !f(a) }
|
||||
}
|
||||
|
||||
// Any keeps aircraft matching at least one of the supplied filters (logical OR).
|
||||
func Any(filters ...AircraftFilter) AircraftFilter {
|
||||
// And keeps aircraft matching every supplied filter (logical AND).
|
||||
func And(filters ...AircraftFilter) AircraftFilter {
|
||||
return func(a *Aircraft) bool { return keep(a, filters) }
|
||||
}
|
||||
|
||||
// Or keeps aircraft matching at least one of the supplied filters (logical OR).
|
||||
func Or(filters ...AircraftFilter) AircraftFilter {
|
||||
return func(a *Aircraft) bool {
|
||||
for _, f := range filters {
|
||||
if f(a) {
|
||||
|
||||
@@ -113,21 +113,34 @@ func TestWithinNMOf(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnyOR(t *testing.T) {
|
||||
// Two non-overlapping 5 nm circles; Any should keep aircraft in either.
|
||||
func TestOr(t *testing.T) {
|
||||
// Two non-overlapping 5 nm circles; Or should keep aircraft in either.
|
||||
r := &AircraftReport{Aircraft: []Aircraft{
|
||||
{Hex: "nearA", Lat: 40.01, Lon: -75.0},
|
||||
{Hex: "nearB", Lat: 41.0, Lon: -76.01},
|
||||
{Hex: "between", Lat: 40.5, Lon: -75.5},
|
||||
{Hex: "nopos"},
|
||||
}}
|
||||
near := r.Filter(Any(WithinNMOf(40, -75, 5), WithinNMOf(41, -76, 5)))
|
||||
near := r.Filter(Or(WithinNMOf(40, -75, 5), WithinNMOf(41, -76, 5)))
|
||||
got := map[string]bool{}
|
||||
for _, a := range near {
|
||||
got[a.Hex] = true
|
||||
}
|
||||
if !got["nearA"] || !got["nearB"] || got["between"] || got["nopos"] || len(got) != 2 {
|
||||
t.Errorf("Any(WithinNMOf...) OR mismatch, matched: %v", got)
|
||||
t.Errorf("Or(WithinNMOf...) mismatch, matched: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnd(t *testing.T) {
|
||||
// And matches only aircraft passing every filter; here position AND in-circle.
|
||||
r := &AircraftReport{Aircraft: []Aircraft{
|
||||
{Hex: "in", Lat: 40.01, Lon: -75.0},
|
||||
{Hex: "out", Lat: 50.0, Lon: -75.0},
|
||||
{Hex: "nopos"},
|
||||
}}
|
||||
near := r.Filter(And(WithPosition(), WithinNMOf(40, -75, 5)))
|
||||
if len(near) != 1 || near[0].Hex != "in" {
|
||||
t.Errorf("And mismatch, matched: %+v", near)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user