All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.activej.fs.ActiveFsAdapters Maven / Gradle / Ivy

Go to download

Provides tools for building efficient, scalable local, remote or clustered file servers. It utilizes ActiveJ CSP for fast and reliable file transfer.

There is a newer version: 6.0-beta2
Show newest version
package io.activej.fs;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static io.activej.fs.util.RemoteFsUtils.escapeGlob;

public class ActiveFsAdapters {

	public static ActiveFs transform(ActiveFs originalFs, Function> into, Function> from, Function> globInto) {
		return new TransformActiveFs(originalFs, into, from, globInto);
	}

	public static ActiveFs transform(ActiveFs originalFs, Function> into, Function> from) {
		return transform(originalFs, into, from, $ -> Optional.empty());
	}

	// similar to 'chroot'
	public static ActiveFs addPrefix(ActiveFs originalFs, String prefix) {
		if (prefix.length() == 0) {
			return originalFs;
		}
		String escapedPrefix = escapeGlob(prefix);
		return transform(originalFs,
				name -> Optional.of(prefix + name),
				name -> Optional.ofNullable(name.startsWith(prefix) ? name.substring(prefix.length()) : null),
				name -> Optional.of(escapedPrefix + name)
		);
	}

	// similar to 'cd'
	public static ActiveFs subdirectory(ActiveFs originalFs, String dir) {
		if (dir.length() == 0) {
			return originalFs;
		}
		return addPrefix(originalFs, dir.endsWith("/") ? dir : dir + '/');
	}

	public static ActiveFs removePrefix(ActiveFs originalFs, String prefix) {
		if (prefix.length() == 0) {
			return originalFs;
		}
		String escapedPrefix = escapeGlob(prefix);
		return transform(originalFs,
				name -> Optional.ofNullable(name.startsWith(prefix) ? name.substring(prefix.length()) : null),
				name -> Optional.of(prefix + name),
				name -> Optional.of(name.startsWith(escapedPrefix) ? name.substring(escapedPrefix.length()) : "**")
		);
	}

	public static ActiveFs filter(ActiveFs originalFs, Predicate predicate) {
		return new FilterActiveFs(originalFs, predicate);
	}

	public static ActiveFs mount(ActiveFs root, Map mounts) {
		return new MountingActiveFs(root,
				mounts.entrySet().stream()
						.collect(Collectors.toMap(
								Map.Entry::getKey,
								e -> removePrefix(e.getValue(), e.getKey()))));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy