Guest User

Untitled

a guest
Apr 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. package com.emasphere.operation.query.model.definition.matrix.dimension;
  2.  
  3. import com.emasphere.operation.injection.model.CodeLabel;
  4. import com.emasphere.operation.query.model.SimpleRegexUtils;
  5. import com.fasterxml.jackson.annotation.JsonCreator;
  6. import com.fasterxml.jackson.annotation.JsonIgnore;
  7. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  8. import com.fasterxml.jackson.annotation.JsonProperty;
  9. import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
  10. import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
  11.  
  12. import java.io.Serializable;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Objects;
  16. import java.util.function.BiFunction;
  17. import java.util.function.Consumer;
  18. import java.util.regex.Pattern;
  19. import java.util.regex.PatternSyntaxException;
  20.  
  21. import static java.util.Arrays.*;
  22. import static java.util.Collections.*;
  23.  
  24. /**
  25. * The {@link MatrixDimensionDefinition dimension} is based on hard-coded range of text values.
  26. *
  27. * @author Sebastien Gerard
  28. */
  29. @JsonDeserialize(builder = MatrixTextRangeValuesDimensionDefinition.Builder.class)
  30. public class MatrixTextRangeValuesDimensionDefinition implements MatrixDimensionDefinition {
  31.  
  32. /**
  33. * Returns a new empty {@link Builder builder}.
  34. */
  35. public static MatrixTextRangeValuesDimensionDefinition.Builder matrixTextRangeValuesDimensionDefinition() {
  36. return new Builder();
  37. }
  38.  
  39. /**
  40. * Returns a new {@link Group text group}.
  41. */
  42. public static Group textGroup(String name, List<String> entries) {
  43. return textGroup(Type.STRICT_MATCH, name, entries);
  44. }
  45.  
  46. /**
  47. * Returns a new {@link Group text group}.
  48. */
  49. public static Group textGroup(String name, String... entries) {
  50. return textGroup(name, asList(entries));
  51. }
  52.  
  53. /**
  54. * Returns a new {@link Group text group}.
  55. */
  56. public static Group textGroup(Type type, String name, List<String> entries) {
  57. return new Group(entries, type, name);
  58. }
  59.  
  60. /**
  61. * Returns a new {@link Group text group}.
  62. */
  63. public static Group textGroup(Type type, String name, String... entries) {
  64. return textGroup(type, name, asList(entries));
  65. }
  66.  
  67. private final CodeLabel valueReference;
  68. private final List<Group> textGroups;
  69.  
  70. private MatrixTextRangeValuesDimensionDefinition(Builder builder) {
  71. valueReference = builder.valueReference;
  72. textGroups = unmodifiableList(builder.textGroups);
  73. }
  74.  
  75. @Override
  76. public CodeLabel getValueReference() {
  77. return valueReference;
  78. }
  79.  
  80. @Override
  81. public DimensionType getType() {
  82. return DimensionType.TEXT_RANGE_VALUES;
  83. }
  84.  
  85. @Override
  86. public <T> T accept(MatrixDimensionDefinition.Visitor<T> visitor) {
  87. if (visitor instanceof Visitor) {
  88. return ((Visitor<T>) visitor).visitMatrixTextRangeValuesDimension(this);
  89. } else {
  90. return visitor.visitMatrixDimension(this);
  91. }
  92. }
  93.  
  94. /**
  95. * Returns {@link Group groups} of this dimension. Dimension values are grouped by range and associated to a name.
  96. */
  97. public List<Group> getTextGroups() {
  98. return textGroups;
  99. }
  100.  
  101. /**
  102. * Group of text values.
  103. */
  104. public static final class Group implements Serializable {
  105.  
  106. private final List<String> values;
  107. private final Type type;
  108. private final String name;
  109.  
  110. @JsonCreator
  111. public Group(@JsonProperty("entries") List<String> values,
  112. @JsonProperty("type") Type type,
  113. @JsonProperty("name") String name) {
  114. this.values = values;
  115. this.type = type;
  116. this.name = name;
  117. }
  118.  
  119. /**
  120. * Returns all the group values.
  121. */
  122. public List<String> getValues() {
  123. return values;
  124. }
  125.  
  126. /**
  127. * Returns the {@link Type group type}.
  128. */
  129. public Type getType() {
  130. return type;
  131. }
  132.  
  133. /**
  134. * Returns the name of this range displayed to the end-user.
  135. */
  136. public String getName() {
  137. return name;
  138. }
  139.  
  140. /**
  141. * Returns whether the specified value is contained within this group.
  142. */
  143. public boolean isInGroup(String value) {
  144. return getValues()
  145. .stream()
  146. .anyMatch(groupEntry -> getType().matches(groupEntry, value));
  147. }
  148.  
  149. @Override
  150. public String toString() {
  151. return "Group{" +
  152. "name='" + name + '\'' +
  153. '}';
  154. }
  155.  
  156. @Override
  157. public boolean equals(Object o) {
  158. if (this == o) {
  159. return true;
  160. }
  161.  
  162. if (o == null || getClass() != o.getClass()) {
  163. return false;
  164. }
  165.  
  166. final Group group = (Group) o;
  167.  
  168. return Objects.equals(name, group.name);
  169. }
  170.  
  171. @Override
  172. public int hashCode() {
  173. return Objects.hash(name);
  174. }
  175. }
  176.  
  177. /**
  178. * Type of group entry.
  179. */
  180. public enum Type {
  181.  
  182. /**
  183. * Group entries must exactly match flow entry values.
  184. */
  185. STRICT_MATCH(Object::equals, x -> {
  186. }),
  187.  
  188. /**
  189. * Group entries are expressions that must match flow entry values.
  190. */
  191. SIMPLE_EXPRESSION(
  192. (groupEntry, value) -> (value != null) && SimpleRegexUtils.buildPattern(groupEntry).matcher(value).matches(),
  193. groupEntry -> {
  194. try {
  195. SimpleRegexUtils.buildPattern(groupEntry);
  196. } catch (PatternSyntaxException e) {
  197. throw new IllegalArgumentException("Error while validating the group entry.", e);
  198. }
  199. }
  200. ),
  201.  
  202. @SuppressWarnings("ResultOfMethodCallIgnored")
  203. REGULAR_EXPRESSION(
  204. (groupEntry, value) -> (value != null) && Pattern.matches(groupEntry, value),
  205. groupEntry -> {
  206. try {
  207. Pattern.compile(groupEntry);
  208. } catch (PatternSyntaxException e) {
  209. throw new IllegalArgumentException("Error while validating the group entry.", e);
  210. }
  211. }
  212. );
  213.  
  214. /**
  215. * @deprecated to be removed once the migration has been performed
  216. */
  217. @JsonCreator
  218. public static Type forValue(String value) {
  219. if ("EXPRESSION".equals(value)) {
  220. return SIMPLE_EXPRESSION;
  221. }
  222.  
  223. return Type.valueOf(value);
  224. }
  225.  
  226. private final BiFunction<String, String, Boolean> matcher;
  227. private final Consumer<String> validator;
  228.  
  229. Type(BiFunction<String, String, Boolean> matcher,
  230. Consumer<String> validator) {
  231. this.matcher = matcher;
  232. this.validator = validator;
  233. }
  234.  
  235. /**
  236. * Returns whether the specified group entry matches the current value.
  237. */
  238. public boolean matches(String groupEntry, String value) {
  239. return matcher.apply(groupEntry, value); // TODO operational build the pattern only once and not when matching
  240. }
  241.  
  242. /**
  243. * Validates that the specified group entry is valid.
  244. */
  245. public void validate(String groupEntry) throws IllegalArgumentException {
  246. validator.accept(groupEntry);
  247. }
  248. }
  249.  
  250. /**
  251. * Visitor pattern for {@link MatrixTextRangeValuesDimensionDefinition}.
  252. *
  253. * @param <T> type of object returned when visiting
  254. */
  255. public interface Visitor<T> extends MatrixDimensionDefinition.Visitor<T> {
  256.  
  257. /**
  258. * Visits the specified definition.
  259. */
  260. T visitMatrixTextRangeValuesDimension(MatrixTextRangeValuesDimensionDefinition definition);
  261.  
  262. }
  263.  
  264. /**
  265. * Builder of {@link MatrixTextRangeValuesDimensionDefinition}.
  266. */
  267. @JsonPOJOBuilder(withPrefix = "")
  268. @JsonIgnoreProperties(ignoreUnknown = true)
  269. public static final class Builder {
  270.  
  271. private CodeLabel valueReference;
  272. private final List<Group> textGroups = new ArrayList<>();
  273.  
  274. private Builder() {
  275. }
  276.  
  277. public Builder valueReference(CodeLabel valueReference) {
  278. this.valueReference = valueReference;
  279. return this;
  280. }
  281.  
  282. @JsonProperty("textGroups")
  283. public Builder textGroups(List<Group> textGroups) {
  284. this.textGroups.addAll(textGroups);
  285. return this;
  286. }
  287.  
  288. @JsonIgnore
  289. public Builder textGroups(Group... textGroups) {
  290. return textGroups(asList(textGroups));
  291. }
  292.  
  293. public MatrixTextRangeValuesDimensionDefinition build() {
  294. return new MatrixTextRangeValuesDimensionDefinition(this);
  295. }
  296. }
  297. }
Add Comment
Please, Sign In to add comment