001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2005-2018 The ConQAT Project                               |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016|                                                                          |
017+-------------------------------------------------------------------------*/
018package org.conqat.lib.commons.uniformpath;
019
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.List;
023
024import org.conqat.lib.commons.collections.CollectionUtils;
025import org.conqat.lib.commons.collections.PairList;
026
027import com.google.common.base.Preconditions;
028
029/**
030 * Utility methods to aid the migration from string-typed uniform paths to
031 * type-safe {@link UniformPath}s. Can be removed when/if string-typed uniform
032 * paths are no longer relevant.
033 */
034public class UniformPathCompatibilityUtil {
035
036        /** Converts the given string to a {@link UniformPath}. */
037        public static UniformPath convert(String uniformPath) {
038                return UniformPath.of(getAbsoluteSegments(uniformPath));
039        }
040
041        /** Converts the given string and type to a {@link UniformPath}. */
042        public static UniformPath convert(UniformPath.EType type, String uniformPath) {
043                return UniformPath.of(type, getAbsoluteSegments(uniformPath));
044        }
045
046        /** Converts the given string to a {@link RelativeUniformPath}. */
047        public static RelativeUniformPath convertRelative(String uniformPath) {
048                Preconditions.checkNotNull(uniformPath, "Uniform path must not be null");
049
050                return RelativeUniformPath.of(uniformPath.trim().split("/"));
051        }
052
053        /** Splits the path into segments and resolves relative parts. */
054        /* package */static List<String> getAbsoluteSegments(String uniformPath) {
055                Preconditions.checkNotNull(uniformPath, "Uniform path must not be null");
056
057                return RelativeUniformPath.resolveRelativeSegments(Arrays.asList(uniformPath.trim().split("/")));
058        }
059
060        /** Converts the given strings to {@link UniformPath}s. */
061        public static List<UniformPath> convertCollection(Collection<String> uniformPaths) {
062                return CollectionUtils.map(uniformPaths, UniformPathCompatibilityUtil::convert);
063        }
064
065        /**
066         * Returns the {@link String} representations of the given {@link UniformPath}s.
067         */
068        public static List<String> asUniformPathStrings(Collection<UniformPath> uniformPaths) {
069                return CollectionUtils.map(uniformPaths, UniformPath::toString);
070        }
071
072        /**
073         * Converts a pair list containing {@link UniformPath}s back to a string-typed
074         * uniform path pair list.
075         */
076        public static <T> PairList<String, T> convertPairList(PairList<UniformPath, T> pairList) {
077                return pairList.mapFirst(UniformPath::toString);
078        }
079}