<commit_before>/**
 * Copyright 2013 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bennavetta.appsite2.filesystem.util;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.appengine.repackaged.com.google.common.base.Splitter;
import com.google.appengine.repackaged.com.google.common.collect.Iterables;

/**
 * Utility methods for manipulating paths.
 * @author ben
 *
 */
public class PathUtils
{
	/**
	 * The path separator character used by file system paths.
	 * Value: {@value}
	 */
	public static final String SEPARATOR = "/";
	
	/**
	 * Error message if a path passed to one of these methods is {@code null}.
	 */
	private static final String NULL_PATH_MSG = "Path must not be null";
	
	/**
	 * An internal constructor to prevent instantiation.
	 */
	private PathUtils() {}
	
	/**
	 * Ensure that a directory path ends with a trailing slash.
	 * @param path the path string (cannot be {@code null})
	 * @return the path, ending with a slash
	 */
	public static final String directoryPath(final String path)
	{
		checkNotNull(path, NULL_PATH_MSG);
		return path.endsWith(SEPARATOR) ? path : path.concat(SEPARATOR);
	}
	
	/**
	 * Get the last component in a path string. For example, the last path component of {@code /foo/bar/baz.txt} is {@code baz.txt}. 
	 * @param path the path string (cannot be {@code null})
	 * @return the last component in the path
	 */
	public static final String lastPathComponent(final String path)
	{
		return Iterables.getLast(Splitter.on(SEPARATOR).split(checkNotNull(path, NULL_PATH_MSG)));
	}
	
	/**
	 * Remove the last component of a path. For example, this method would return {@code /foo/bar} given {@code /foo/bar/baz.txt}.
	 * @param path the path string (cannot be {@code null})
	 * @return the path minus its last component
	 */
	public static final String withoutLastComponent(final String path)
	{
		checkNotNull(path, NULL_PATH_MSG);
		if(path.equals(SEPARATOR))
		{
			return path;
		}
		int end = 0;
		if(path.endsWith(SEPARATOR))<commit_msg>forgot to add PathUtils to commit<commit_after>/**
 * Copyright 2013 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bennavetta.appsite2.filesystem.util;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.regex.Pattern;

import com.google.appengine.repackaged.com.google.common.base.Joiner;
import com.google.appengine.repackaged.com.google.common.base.Splitter;
import com.google.appengine.repackaged.com.google.common.collect.Iterables;

/**
 * Utility methods for manipulating paths.
 * @author ben
 *
 */
public class PathUtils
{
	/**
	 * The path separator character used by file system paths.
	 * Value: {@value}
	 */
	public static final String SEPARATOR = "/";
	
	/**
	 * Error message if a path passed to one of these methods is {@code null}.
	 */
	private static final String NULL_PATH_MSG = "Path must not be null";
	
	/**
	 * An internal constructor to prevent instantiation.
	 */
	private PathUtils() {}
	
	/**
	 * Ensure that a directory path ends with a trailing slash.
	 * @param path the path string (cannot be {@code null})
	 * @return the path, ending with a slash
	 */
	public static final String directoryPath(final String path)
	{
		checkNotNull(path, NULL_PATH_MSG);
		return path.endsWith(SEPARATOR) ? path : path.concat(SEPARATOR);
	}
	
	/**
	 * Get the last component in a path string. For example, the last path component of {@code /foo/bar/baz.txt} is {@code baz.txt}. 
	 * @param path the path string (cannot be {@code null})
	 * @return the last component in the path
	 */
	public static final String lastPathComponent(String path)
	{
		if(SEPARATOR.equals(path))
		{
			return path;
		}
		path = checkNotNull(path, NULL_PATH_MSG).replaceAll(Pattern.quote(SEPARATOR) + "+", SEPARATOR);
		if(path.endsWith(SEPARATOR))
		{
			path = path.substring(0, path.length()-1);
		}
		return Iterables.getLast(Splitter.on(SEPARATOR).split(path));
	}
	
	/**
	 * Remove the last component of a path. For example, this method would return {@code /foo/bar} given {@code /foo/bar/baz.txt}.
	 * @param path the path string (cannot be {@code null})
	 * @return the path minus its last component
	 */
	public static final String withoutLastComponent(final String path)
	{
		checkNotNull(path, NULL_PATH_MSG);
		if(path.equals(SEPARATOR))
		{
			return path;
		}
		int end = 0;
		if(path.endsWith(SEPARATOR))
{'fraction_non_alphanumeric': 0.10648826979472141, 'fraction_numerical': 0.004032258064516129, 'mean_word_length': 3.820671378091873, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5015228', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1572, 'n_words': 593}