| 1 | |
package org.webslinger.commons.vfs.util; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
|
| 5 | |
import org.apache.commons.vfs.FileObject; |
| 6 | |
|
| 7 | |
import org.webslinger.util.TTLObject; |
| 8 | |
|
| 9 | |
public abstract class FileObjectAttributeTTLObject<T, A> extends TTLObject<T> { |
| 10 | |
protected final FileObject file; |
| 11 | |
protected final String attributeName; |
| 12 | |
private A attributeValue; |
| 13 | |
private Class<T> resultType; |
| 14 | |
private Class<A> attributeType; |
| 15 | |
|
| 16 | 356 | public FileObjectAttributeTTLObject(FileObject file, String attributeName, Class<T> resultType, Class<A> attributeType) { |
| 17 | 356 | this.file = file; |
| 18 | 356 | this.attributeName = attributeName; |
| 19 | 356 | this.resultType = resultType; |
| 20 | 356 | this.attributeType = attributeType; |
| 21 | 356 | } |
| 22 | |
|
| 23 | |
protected T load(T old) throws IOException { |
| 24 | 356 | if (!file.exists()) return null; |
| 25 | 308 | A newAttributeValue = attributeType.cast(file.getContent().getAttribute(attributeName)); |
| 26 | 308 | if (newAttributeValue == null) return null; |
| 27 | 1 | if (!newAttributeValue.equals(attributeValue)) { |
| 28 | 1 | attributeValue = newAttributeValue; |
| 29 | 1 | return parseAttribute(newAttributeValue); |
| 30 | |
} |
| 31 | 0 | return old; |
| 32 | |
} |
| 33 | |
|
| 34 | |
protected T parseAttribute(A attributeValue) throws IOException { |
| 35 | 0 | return resultType.cast(attributeValue); |
| 36 | |
} |
| 37 | |
|
| 38 | |
public String toString() { |
| 39 | 0 | return file.toString(); |
| 40 | |
} |
| 41 | |
|
| 42 | |
} |
| 43 | |
|