|
| 1 | +classdef HasQuantity |
| 2 | +% HasQuantity - Provide methods for parsing quantity specification value |
| 3 | + |
| 4 | + % properties |
| 5 | + % QuantityKey = 'quantity' |
| 6 | + % end |
| 7 | + |
| 8 | + methods (Static, Access = protected) |
| 9 | + function isRequired = isRequired(source) |
| 10 | + if isKey(source, 'quantity') |
| 11 | + quantity = source('quantity'); |
| 12 | + file.interface.HasQuantity.validateQuantity(quantity) |
| 13 | + |
| 14 | + if ischar(quantity) |
| 15 | + switch quantity |
| 16 | + case {'?', 'zero_or_one'} |
| 17 | + isRequired = false; |
| 18 | + case {'*', 'zero_or_many'} |
| 19 | + isRequired = false; |
| 20 | + case {'+', 'one_or_many'} |
| 21 | + isRequired = true; |
| 22 | + end |
| 23 | + elseif isnumeric(quantity) |
| 24 | + isRequired = quantity >= 1; |
| 25 | + end |
| 26 | + else |
| 27 | + isRequired = true; % Default |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + function isScalar = isScalar(source) |
| 32 | + if isKey(source, 'quantity') |
| 33 | + quantity = source('quantity'); |
| 34 | + file.interface.HasQuantity.validateQuantity(quantity) |
| 35 | + |
| 36 | + if ischar(quantity) |
| 37 | + switch quantity |
| 38 | + case {'?', 'zero_or_one'} |
| 39 | + isScalar = true; |
| 40 | + case {'*', 'zero_or_many'} |
| 41 | + isScalar = false; |
| 42 | + case {'+', 'one_or_many'} |
| 43 | + isScalar = false; |
| 44 | + end |
| 45 | + elseif isnumeric(quantity) |
| 46 | + if quantity == 1 |
| 47 | + isScalar = true; |
| 48 | + else |
| 49 | + isScalar = false; |
| 50 | + end |
| 51 | + end |
| 52 | + else |
| 53 | + isScalar = true; % Default |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + methods (Static, Access = private) |
| 59 | + function validateQuantity(quantity) |
| 60 | + % validateQuantity - Validate quantity specification value |
| 61 | + if ischar(quantity) |
| 62 | + validQuantities = [ ... |
| 63 | + "?", "zero_or_one", ... |
| 64 | + "*", "zero_or_many", ... |
| 65 | + "+", "one_or_many" ... |
| 66 | + ]; |
| 67 | + if ~any(strcmp(validQuantities, quantity)) |
| 68 | + validQuantitiesStr = strjoin(" " + validQuantities, newline); |
| 69 | + ME = MException('NWB:Schema:UnsupportedQuantity', ... |
| 70 | + ['Quantity is "%s", but expected quantity to be one of the ' ... |
| 71 | + 'following values:\n%s\n'], quantity, validQuantitiesStr); |
| 72 | + throwAsCaller(ME) |
| 73 | + end |
| 74 | + |
| 75 | + elseif isnumeric(quantity) |
| 76 | + assert( mod(quantity,1) == 0 && quantity > 0, ... |
| 77 | + 'NWB:Schema:UnsupportedQuantity', ... |
| 78 | + 'Expected quantity to positive integer') |
| 79 | + else |
| 80 | + ME = MException('NWB:Schema:UnsupportedQuantity', ... |
| 81 | + 'Expected quantity to be text or numeric.'); |
| 82 | + throwAsCaller(ME) |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments