|
| 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.commons.lang3.StringUtils; |
| 23 | + |
| 24 | +import lombok.experimental.UtilityClass; |
| 25 | + |
| 26 | +@UtilityClass |
| 27 | +public class KeyUtils { |
| 28 | + |
| 29 | + /** |
| 30 | + * Whether the path is the parent path of the child |
| 31 | + * <p> Only the parentPath is the parent path of the childPath, return true |
| 32 | + * <p> If the parentPath is equal to the childPath, return false |
| 33 | + */ |
| 34 | + public static boolean isParent(final String parentPath, final String childPath) { |
| 35 | + if (StringUtils.isEmpty(parentPath)) { |
| 36 | + throw new IllegalArgumentException("Invalid parent path string " + parentPath); |
| 37 | + } |
| 38 | + if (StringUtils.isEmpty(childPath)) { |
| 39 | + throw new IllegalArgumentException("Invalid child path string " + childPath); |
| 40 | + } |
| 41 | + final String standardParentPath = removeLastSlash(parentPath); |
| 42 | + final String standardChildPath = removeLastSlash(childPath); |
| 43 | + return standardParentPath.equals(getParentPath(standardChildPath)) |
| 44 | + && !standardParentPath.equals(standardChildPath); |
| 45 | + } |
| 46 | + |
| 47 | + public static boolean isSamePath(final String path1, final String path2) { |
| 48 | + return removeLastSlash(path1).equals(path2); |
| 49 | + } |
| 50 | + |
| 51 | + public static String getParentPath(final String path) { |
| 52 | + if (StringUtils.isEmpty(path)) { |
| 53 | + throw new IllegalArgumentException("Invalid path string " + path); |
| 54 | + } |
| 55 | + final String standardPath = removeLastSlash(path); |
| 56 | + if ("/".equals(standardPath)) { |
| 57 | + return path; |
| 58 | + } |
| 59 | + final int i = StringUtils.lastIndexOf(standardPath, "/"); |
| 60 | + if (i == 0) { |
| 61 | + return "/"; |
| 62 | + } |
| 63 | + return path.substring(0, i); |
| 64 | + } |
| 65 | + |
| 66 | + private static String removeLastSlash(final String path) { |
| 67 | + checkNotNull(path, "path is null"); |
| 68 | + if (!path.startsWith("/")) { |
| 69 | + throw new IllegalArgumentException("Invalid path string " + path); |
| 70 | + } |
| 71 | + int length = path.length() - 1; |
| 72 | + while (length >= 0 && path.charAt(length) == '/') { |
| 73 | + length--; |
| 74 | + } |
| 75 | + if (length == -1) { |
| 76 | + return "/"; |
| 77 | + } |
| 78 | + return path.substring(0, length + 1); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments