33
44-module (ff_range ).
55
6+ -include_lib (" damsel/include/dmsl_domain_thrift.hrl" ).
7+
8+ -type cash_range () :: dmsl_domain_thrift :'CashRange' ().
9+ -type cash () :: dmsl_domain_thrift :'Cash' ().
610-type range (T ) :: {'maybe' (bound (T )), 'maybe' (bound (T ))}.
711-type bound (T ) :: {exclusive | inclusive , ord (T )}.
812
1317-export_type ([range / 1 ]).
1418-export_type ([bound / 1 ]).
1519
20+ -export ([is_inside /2 ]).
1621-export ([intersect /2 ]).
1722-export ([contains /2 ]).
1823
1924% %
2025
26+ -spec is_inside (cash (), cash_range ()) -> within | {exceeds , lower | upper }.
27+ is_inside (Cash , # domain_CashRange {lower = Lower , upper = Upper } = CashRange ) ->
28+ case
29+ {
30+ compare_cash (fun erlang :'>' /2 , Cash , Lower ),
31+ compare_cash (fun erlang :'<' /2 , Cash , Upper )
32+ }
33+ of
34+ {true , true } ->
35+ within ;
36+ {false , true } ->
37+ {exceeds , lower };
38+ {true , false } ->
39+ {exceeds , upper };
40+ _ ->
41+ error ({misconfiguration , {'Invalid cash range specified' , CashRange , Cash }})
42+ end .
43+
2144-spec intersect (range (T ), range (T )) -> range (T ) | undefined .
2245intersect (R1 , R2 ) ->
2346 B1 = max_bound (lower (R1 ), lower (R2 )),
@@ -35,6 +58,18 @@ contains(R1, R2) ->
3558
3659% %
3760
61+ -define (CASH (Amount , SymCode ), # domain_Cash {
62+ amount = Amount ,
63+ currency = # domain_CurrencyRef {symbolic_code = SymCode }
64+ }).
65+
66+ compare_cash (_ , V , {inclusive , V }) ->
67+ true ;
68+ compare_cash (F , ? CASH (A , C ), {_ , ? CASH (Am , C )}) ->
69+ F (A , Am );
70+ compare_cash (_ , _ , _ ) ->
71+ error .
72+
3873compare_bounds (B1 , B1 ) ->
3974 eq ;
4075compare_bounds (_B1 , neginf ) ->
0 commit comments