|
| 1 | +package com.javaquery.util.io; |
| 2 | + |
| 3 | +import com.javaquery.util.Assert; |
| 4 | +import com.javaquery.util.collection.Collections; |
| 5 | +import com.javaquery.util.string.Strings; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author vicky.thakor |
| 13 | + * @since 1.0.3 |
| 14 | + */ |
| 15 | +public final class JFile extends File { |
| 16 | + |
| 17 | + private final Map<String, String> attributes; |
| 18 | + |
| 19 | + public JFile(String pathname) { |
| 20 | + super(pathname); |
| 21 | + attributes = new HashMap<>(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @return file extension |
| 26 | + */ |
| 27 | + public String getExtension(){ |
| 28 | + String name = getName(); |
| 29 | + if(Strings.nonNullNonEmpty(name)){ |
| 30 | + int lastIndexOfDot = name.lastIndexOf("."); |
| 31 | + if(lastIndexOfDot > 0){ |
| 32 | + return name.substring(lastIndexOfDot + 1); |
| 33 | + } |
| 34 | + } |
| 35 | + return ""; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param name a name to change for current file |
| 40 | + * @return renamed file object if and only if the renaming succeeded; |
| 41 | + * <code>null</code> otherwise |
| 42 | + * |
| 43 | + * @throws SecurityException |
| 44 | + * If a security manager exists and its <code>{@link |
| 45 | + * java.lang.SecurityManager#checkWrite(java.lang.String)}</code> |
| 46 | + * method denies write access to either the old or new pathnames |
| 47 | + * |
| 48 | + * @throws NullPointerException |
| 49 | + * If parameter <code>dest</code> is <code>null</code> |
| 50 | + */ |
| 51 | + public final JFile rename(String name){ |
| 52 | + Assert.nonNull(name, NullPointerException::new); |
| 53 | + if(exists()){ |
| 54 | + boolean result = renameTo(new File(getParent() + File.separatorChar + name)); |
| 55 | + if(result){ |
| 56 | + return new JFile(getParent() + File.separatorChar + name); |
| 57 | + } |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return String content of this file. |
| 64 | + */ |
| 65 | + public final String read(){ |
| 66 | + return Files.readFromFile(this); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param data data to write to this file |
| 71 | + */ |
| 72 | + public final void write(String data) { |
| 73 | + Files.writeToFile(this, data); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param data to append to this file |
| 78 | + * @param appendNewLine <code>true</code> to append new line at the end of data |
| 79 | + * otherwise <code>false</code> |
| 80 | + */ |
| 81 | + public final void append(String data, boolean appendNewLine) { |
| 82 | + Files.appendToFile(this, data, appendNewLine); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return attributes of file |
| 87 | + */ |
| 88 | + public final Map<String, String> getAttributes() { |
| 89 | + return attributes; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param key a key of attribute |
| 94 | + * @param value a value of attribute |
| 95 | + */ |
| 96 | + public final void addAttribute(String key, String value){ |
| 97 | + attributes.put(key, value); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param attributes add attributes map to file |
| 102 | + */ |
| 103 | + public final void addAllAttribute(Map<String, String> attributes){ |
| 104 | + if(Collections.nonNullNonEmpty(attributes)){ |
| 105 | + this.attributes.putAll(attributes); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param key a key to find attribute |
| 111 | + * @param defaultValue default value in case attribute not found |
| 112 | + * @return attribute value if found otherwise defaultValue |
| 113 | + */ |
| 114 | + public final String optAttribute(String key, String defaultValue){ |
| 115 | + return attributes.getOrDefault(key, defaultValue); |
| 116 | + } |
| 117 | +} |
0 commit comments