|
| 1 | +classdef RelationalOperators |
| 2 | +% Enumeration of relational operators. |
| 3 | +% |
| 4 | +% Enumeration imagem.util.enums.RelationalOperators |
| 5 | +% |
| 6 | +% Example |
| 7 | +% % pick an enumeration |
| 8 | +% op = imagem.util.enums.RelationalOperators.GreaterThan; |
| 9 | +% % apply it onto numerical values |
| 10 | +% res = apply(op, 1:10, 4) |
| 11 | +% res = |
| 12 | +% 1x10 logical array |
| 13 | +% 0 0 0 0 1 1 1 1 1 1 |
| 14 | +% |
| 15 | +% See also |
| 16 | +% |
| 17 | + |
| 18 | +% ------ |
| 19 | +% Author: David Legland |
| 20 | + |
| 21 | +% Created: 2020-12-23, using Matlab 9.8.0.1323502 (R2020a) |
| 22 | +% Copyright 2020 INRAE - BIA-BIBS. |
| 23 | + |
| 24 | + |
| 25 | +%% Enumerates the different cases |
| 26 | +enumeration |
| 27 | + Equal('Equal (==)', @eq, '=='); |
| 28 | + GreaterThanOrEqual('Greater Than or Equal (>=)', @ge, '>='); |
| 29 | + GreaterThan('Greater Than (>)', @gt, '>'); |
| 30 | + LesserThanOrEqual('Lesser Than or Equal (<=)', @le, '<='); |
| 31 | + LesserThan('Lesser Than (<)', @lt, '<'); |
| 32 | + NotEqual('Not Equal (~=)', @ne, '~='); |
| 33 | +end % end properties |
| 34 | + |
| 35 | + |
| 36 | +%% Static methods |
| 37 | +methods (Static) |
| 38 | + function res = allNames() |
| 39 | + % Returns a cell list with all enumeration names. |
| 40 | + mc = ?imagem.util.enums.RelationalOperators; |
| 41 | + itemList = mc.EnumerationMemberList; |
| 42 | + nItems = length(itemList); |
| 43 | + res = cell(1, nItems); |
| 44 | + |
| 45 | + for i = 1:nItems |
| 46 | + % retrieve current enumeration item |
| 47 | + mitem = itemList(i); |
| 48 | + res{i} = mitem.Name; |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + function res = fromName(name) |
| 53 | + % Identifies a imagem.util.enums.RelationalOperators from its name. |
| 54 | + if nargin == 0 || ~ischar(name) |
| 55 | + error('requires a character array as input argument'); |
| 56 | + end |
| 57 | + |
| 58 | + mc = ?imagem.util.enums.RelationalOperators; |
| 59 | + itemList = mc.EnumerationMemberList; |
| 60 | + for i = 1:length(itemList) |
| 61 | + % retrieve current enumeration item |
| 62 | + mitem = itemList(i); |
| 63 | + item = imagem.util.enums.RelationalOperators.(mitem.Name); |
| 64 | + if strcmpi(name, char(item)) |
| 65 | + res = item; |
| 66 | + return; |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + error('Unrecognized imagem.util.enums.RelationalOperators name: %s', name); |
| 71 | + end |
| 72 | + |
| 73 | + function res = allLabels() |
| 74 | + % Returns a cell list with all enumeration names. |
| 75 | + mc = ?imagem.util.enums.RelationalOperators; |
| 76 | + itemList = mc.EnumerationMemberList; |
| 77 | + nItems = length(itemList); |
| 78 | + res = cell(1, nItems); |
| 79 | + |
| 80 | + for i = 1:nItems |
| 81 | + % retrieve current enumeration item |
| 82 | + mitem = itemList(i); |
| 83 | + item = imagem.util.enums.RelationalOperators.(mitem.Name); |
| 84 | + res{i} = item.Label; |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + function res = fromLabel(label) |
| 89 | + % Identifies a imagem.util.enums.RelationalOperators from its label. |
| 90 | + if nargin == 0 || ~ischar(label) |
| 91 | + error('requires a character array as input argument'); |
| 92 | + end |
| 93 | + |
| 94 | + mc = ?imagem.util.enums.RelationalOperators; |
| 95 | + itemList = mc.EnumerationMemberList; |
| 96 | + for i = 1:length(itemList) |
| 97 | + % retrieve current enumeration item |
| 98 | + mitem = itemList(i); |
| 99 | + item = imagem.util.enums.RelationalOperators.(mitem.Name); |
| 100 | + if strcmpi(label, item.Label) |
| 101 | + res = item; |
| 102 | + return; |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + error('Unrecognized imagem.util.enums.RelationalOperators label: %s', label); |
| 107 | + end |
| 108 | +end % end methods |
| 109 | + |
| 110 | + |
| 111 | +%% Constructor |
| 112 | +methods |
| 113 | + function obj = RelationalOperators(label, op, symbol) |
| 114 | + % Constructor for imagem.util.enums.RelationalOperators class. |
| 115 | + obj.Label = label; |
| 116 | + obj.Op = op; |
| 117 | + obj.Symbol = symbol; |
| 118 | + end |
| 119 | + |
| 120 | +end % end constructors |
| 121 | + |
| 122 | +%% Methods |
| 123 | +methods |
| 124 | + function res = apply(obj, arg1, arg2) |
| 125 | + % Apply the selected operator to a pair of numerical arguments. |
| 126 | + % |
| 127 | + % op = imagem.util.enums.RelationalOperators.GreaterThan; |
| 128 | + % res = apply(op, 1:10, 4) |
| 129 | + % res = |
| 130 | + % 1x10 logical array |
| 131 | + % 0 0 0 0 1 1 1 1 1 1 |
| 132 | + res = obj.Op(arg1, arg2); |
| 133 | + end |
| 134 | +end |
| 135 | + |
| 136 | +%% Properties |
| 137 | +properties |
| 138 | + % The label used to identify the enumeration in graphical widgets. |
| 139 | + Label; |
| 140 | + % The operation to apply on numerical values. |
| 141 | + Op; |
| 142 | + % The symbol of the operation in Matlab syntax. |
| 143 | + Symbol; |
| 144 | +end % end properties |
| 145 | + |
| 146 | +end % end classdef |
| 147 | + |
0 commit comments