|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dolphinscheduler.plugin.registry.jdbc; |
| 19 | + |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
| 22 | +import org.apache.dolphinscheduler.registry.api.RegistryConstants; |
| 23 | + |
| 24 | +import org.apache.commons.lang3.StringUtils; |
| 25 | + |
| 26 | +import lombok.experimental.UtilityClass; |
| 27 | + |
| 28 | +@UtilityClass |
| 29 | +public class KeyUtils { |
| 30 | + |
| 31 | + /** |
| 32 | + * Whether the path is the parent path of the child |
| 33 | + * <p> Only the parentPath is the parent path of the childPath, return true |
| 34 | + * <p> If the parentPath is equal to the childPath, return false |
| 35 | + */ |
| 36 | + public static boolean isParent(final String parentPath, final String childPath) { |
| 37 | + if (StringUtils.isEmpty(parentPath)) { |
| 38 | + throw new IllegalArgumentException("Invalid parent path " + parentPath); |
| 39 | + } |
| 40 | + if (StringUtils.isEmpty(childPath)) { |
| 41 | + throw new IllegalArgumentException("Invalid child path " + childPath); |
| 42 | + } |
| 43 | + final String[] parentSplit = parentPath.split(RegistryConstants.PATH_SEPARATOR); |
| 44 | + final String[] childSplit = childPath.split(RegistryConstants.PATH_SEPARATOR); |
| 45 | + if (parentSplit.length >= childSplit.length) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + for (int i = 0; i < parentSplit.length; i++) { |
| 49 | + if (!parentSplit[i].equals(childSplit[i])) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + } |
| 53 | + return true; |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + public static boolean isSamePath(final String path1, final String path2) { |
| 58 | + return removeLastSlash(path1).equals(path2); |
| 59 | + } |
| 60 | + |
| 61 | + private static String removeLastSlash(final String path) { |
| 62 | + checkNotNull(path, "path is null"); |
| 63 | + if (!path.startsWith(RegistryConstants.PATH_SEPARATOR)) { |
| 64 | + throw new IllegalArgumentException("Invalid path " + path); |
| 65 | + } |
| 66 | + int length = path.length() - 1; |
| 67 | + while (length >= 0 && path.charAt(length) == RegistryConstants.PATH_SEPARATOR_CHAR) { |
| 68 | + length--; |
| 69 | + } |
| 70 | + if (length == -1) { |
| 71 | + return RegistryConstants.PATH_SEPARATOR; |
| 72 | + } |
| 73 | + return path.substring(0, length + 1); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments