Lab3 #2
9
Lab3/src/main/java/org/lumijiez/base/ArbitraryFile.java
Normal file
9
Lab3/src/main/java/org/lumijiez/base/ArbitraryFile.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package org.lumijiez.base;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ArbitraryFile extends Document{
|
||||
public ArbitraryFile(Path path) {
|
||||
super(path);
|
||||
}
|
||||
}
|
||||
9
Lab3/src/main/java/org/lumijiez/base/CodeFile.java
Normal file
9
Lab3/src/main/java/org/lumijiez/base/CodeFile.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package org.lumijiez.base;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class CodeFile extends Document{
|
||||
public CodeFile(Path path) {
|
||||
super(path);
|
||||
}
|
||||
}
|
||||
@@ -3,31 +3,57 @@ package org.lumijiez.base;
|
||||
import org.lumijiez.enums.FileType;
|
||||
import org.lumijiez.interfaces.IDocument;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Date;
|
||||
|
||||
public class Document implements IDocument {
|
||||
private String fullFilename = "";
|
||||
public class Document extends File implements IDocument {
|
||||
private String extension;
|
||||
private FileType fileType;
|
||||
private Date createdAt;
|
||||
private Date modifiedAt;
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return null;
|
||||
public Document(Path path) {
|
||||
super(path.toString());
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
int lastDotIndex = getName().lastIndexOf('.');
|
||||
extension = (lastDotIndex > 0) ? getName().substring(lastDotIndex + 1) : "";
|
||||
fileType = FileType.getFileType(extension);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExtension() {
|
||||
return null;
|
||||
return extension;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getCreatedTime() {
|
||||
try {
|
||||
BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class);
|
||||
return new Date(fileAttributes.creationTime().toMillis());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getModificationTime() {
|
||||
try {
|
||||
BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class);
|
||||
return new Date(fileAttributes.lastAccessTime().toMillis());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileType getFileType() {
|
||||
return fileType;
|
||||
}
|
||||
}
|
||||
|
||||
9
Lab3/src/main/java/org/lumijiez/base/ImageFile.java
Normal file
9
Lab3/src/main/java/org/lumijiez/base/ImageFile.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package org.lumijiez.base;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ImageFile extends Document{
|
||||
public ImageFile(Path path) {
|
||||
super(path);
|
||||
}
|
||||
}
|
||||
9
Lab3/src/main/java/org/lumijiez/base/TextFile.java
Normal file
9
Lab3/src/main/java/org/lumijiez/base/TextFile.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package org.lumijiez.base;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class TextFile extends Document {
|
||||
public TextFile(Path path) {
|
||||
super(path);
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,22 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public enum FileType {
|
||||
IMAGE, PLAINTEXT, FILE, CODE;
|
||||
IMAGE("Image"), PLAINTEXT("Plaintext"), FILE("File"), CODE("Code"), NONE("None");
|
||||
public List<String> typeExtensions;
|
||||
public final String typeName;
|
||||
|
||||
FileType(String typeName) {
|
||||
this.typeName = typeName;
|
||||
}
|
||||
|
||||
public static FileType getFileType(String extension) {
|
||||
for (FileType fileType : values()) {
|
||||
if (fileType.typeExtensions.contains(extension.toLowerCase())) {
|
||||
return fileType;
|
||||
}
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
|
||||
static {
|
||||
IMAGE.typeExtensions = new ArrayList<>(List.of("jpg", "png"));
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package org.lumijiez.interfaces;
|
||||
|
||||
import org.lumijiez.enums.FileType;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public interface IDocument {
|
||||
public String getFilename();
|
||||
public String getExtension();
|
||||
public Date getCreatedTime();
|
||||
public Date getModificationTime();
|
||||
public FileType getFileType();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user