/*
* Copyright (c) 2013, the authors.
*
* This file is part of 'DXFS'.
*
* DXFS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DXFS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DXFS. If not, see <http://www.gnu.org/licenses/>.
*/
package nextflow.fs.dx;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Attributes for file and folder in DX storage
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
public class DxFileAttributes implements BasicFileAttributes {
private static final String[] EMPTY = new String[0];
private volatile Map<String,Object> attr;
final private boolean fDirectory;
/**
* Create a {@code DxFileAttributes} by using the values in the provider map
*
* @param attr
* @return
*/
public static DxFileAttributes file( Map<String,Object> attr ) {
if( attr == null ) throw new IllegalArgumentException();
return new DxFileAttributes(attr, false);
}
/**
* Directory supports only *name* and *folder* attributes
*
* @return
*/
public static DxFileAttributes directory(String path) {
String name;
String folder = null;
// normalize the path
if( "/".equals(path) ) {
name = "/";
}
else {
if( path.endsWith("/") ) {
path = path.substring(0, path.length()-1);
}
int p = path.lastIndexOf('/');
if( p == -1 ) {
name = path;
}
else {
name = path.substring(p+1);
folder = path.substring(0,p);
}
}
Map<String,Object> attr = new HashMap<>();
attr.put("name", name);
attr.put("folder", folder);
attr.put("path", path);
return new DxFileAttributes(attr, true);
}
/**
* Create a file attributes object with the specified set of attributes key-value pairs
*
* @param attributes The map holding all attributes for this object
* @param isDirectory Whenever the it contains attributes for a *file* or a *directory*
*/
DxFileAttributes( Map<String,Object> attributes, boolean isDirectory ) {
this.attr = attributes;
this.fDirectory = isDirectory;
}
/**
* @return A map containing all attributes
*/
public Map<String,Object> asMap() {
return new HashMap<>(attr);
}
/**
* @return An attribute value by its name
*/
public Object get(String name) {
return attr.get(name);
}
public String getName() {
return (String) attr.get("name");
}
public String[] getLinks() {
return fDirectory ? EMPTY : (String[]) attr.get("links");
}
public String[] getTags() {
return fDirectory ? EMPTY : (String[]) attr.get("tags");
}
public String getMedia() {
return fDirectory ? null : (String) attr.get("media");
}
@Override
public FileTime creationTime() {
if( fDirectory ) return null;
Long lastModifiedMillis = (Long) attr.get("created");
if( lastModifiedMillis != null ) {
return FileTime.from( lastModifiedMillis, TimeUnit.MILLISECONDS );
}
return null;
}
@Override
public FileTime lastModifiedTime() {
if( fDirectory ) return null;
Long lastModifiedMillis = (Long) attr.get("modified");
if( lastModifiedMillis != null ) {
return FileTime.from( lastModifiedMillis, TimeUnit.MILLISECONDS );
}
return null;
}
@Override
public FileTime lastAccessTime() {
return null;
}
public String getClassType(){
if( fDirectory ) throw new UnsupportedOperationException();
return (String) attr.get("class");
}
public String getProjectId() {
if( fDirectory ) throw new UnsupportedOperationException();
return (String) attr.get("project");
}
public String getState() {
if( fDirectory ) throw new UnsupportedOperationException();
return (String) attr.get("state");
}
public String getFolder() {
return (String) attr.get("folder");
}
public boolean isSponsored() {
if( fDirectory ) throw new UnsupportedOperationException();
Boolean sponsored = (Boolean) attr.get("sponsored");
if( sponsored != null ) {
return sponsored;
}
return false;
}
public boolean isHidden() {
if( fDirectory ) { return false; }
Boolean hidden = (Boolean) attr.get("hidden");
if( hidden != null ) {
return hidden;
}
return false;
}
@Override
public String fileKey() {
// directory does not have an ID attribute, so use the path itself as unique identifier
return (String) ( fDirectory ? attr.get("path") : attr.get("id") );
}
public String[] getTypes() {
return fDirectory ? EMPTY : (String[]) attr.get("types");
}
@Override
public long size() {
if( fDirectory ) return 0;
Object obj = attr.get("size");
if( obj instanceof Number ) {
return ((Number) obj).longValue();
}
else if( obj == null ) {
return 0;
}
else {
throw new IllegalStateException("Not a valid file size value: " + obj.toString());
}
}
@Override
public boolean isRegularFile() {
return !fDirectory;
}
@Override
public boolean isDirectory() {
return fDirectory;
}
@Override
public boolean isSymbolicLink() {
return false;
}
@Override
public boolean isOther() {
return false;
}
}