|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Commons Demo |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 - 2026 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.addons.demo; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import lombok.NonNull; |
| 24 | +import lombok.experimental.UtilityClass; |
| 25 | + |
| 26 | +@UtilityClass |
| 27 | +class DemoSourceConditionHelper { |
| 28 | + |
| 29 | + public boolean eval(String condition, Map<String, String> env) { |
| 30 | + if (condition == null) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + String[] expr = condition.split(" "); |
| 35 | + |
| 36 | + if (expr.length == 3) { |
| 37 | + String lhs = env.get(expr[0]); |
| 38 | + |
| 39 | + if (lhs == null) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + String operator = expr[1]; |
| 44 | + String rhs = expr[2]; |
| 45 | + |
| 46 | + switch (operator) { |
| 47 | + case "lt": |
| 48 | + return compare(lhs, rhs) < 0; |
| 49 | + case "le": |
| 50 | + return compare(lhs, rhs) <= 0; |
| 51 | + case "eq": |
| 52 | + return compare(lhs, rhs) == 0; |
| 53 | + case "ge": |
| 54 | + return compare(lhs, rhs) >= 0; |
| 55 | + case "gt": |
| 56 | + return compare(lhs, rhs) > 0; |
| 57 | + case "ne": |
| 58 | + return compare(lhs, rhs) != 0; |
| 59 | + default: |
| 60 | + throw new IllegalArgumentException("Unknown operator: " + operator); |
| 61 | + } |
| 62 | + } else { |
| 63 | + throw new IllegalArgumentException("Invalid condition: '" + condition |
| 64 | + + "'. Must be exactly 3 components: [VARIABLE] [OPERATOR] [VERSION]"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private int compare(String a, String b) { |
| 69 | + String[] aa = split(a); |
| 70 | + String[] bb = split(b); |
| 71 | + |
| 72 | + int minLength = Math.min(aa.length, bb.length); |
| 73 | + |
| 74 | + for (int i = 0; i < minLength; i++) { |
| 75 | + int ai = Integer.parseInt(aa[i]); |
| 76 | + int bi = Integer.parseInt(bb[i]); |
| 77 | + int c = Integer.compare(ai, bi); |
| 78 | + if (c != 0) { |
| 79 | + return c; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + private String[] split(@NonNull String version) { |
| 87 | + if (!version.matches("^\\d+(\\.\\d+){0,2}$")) { |
| 88 | + throw new IllegalArgumentException("Invalid version: '" + version |
| 89 | + + "'. Must be 'major', 'major.minor', or major.minor.patch'"); |
| 90 | + } |
| 91 | + return version.split("\\."); |
| 92 | + } |
| 93 | +} |
0 commit comments