View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.niofs;
20  
21  import java.nio.file.attribute.BasicFileAttributes;
22  import java.nio.file.attribute.FileTime;
23  import java.util.Objects;
24  import java.util.regex.Pattern;
25  
26  /**
27   * @author Thomas Scheffler (yagee)
28   *
29   */
30  public class MCRFileAttributes<T> implements BasicFileAttributes {
31      enum FileType {
32          file, directory, link, other;
33  
34          public static FileType fromAttribute(BasicFileAttributes attrs) {
35              if (attrs.isRegularFile()) {
36                  return file;
37              }
38              if (attrs.isDirectory()) {
39                  return directory;
40              }
41              if (attrs.isSymbolicLink()) {
42                  return link;
43              }
44              return other;
45          }
46      }
47  
48      private static final FileTime EPOCHE_TIME = FileTime.fromMillis(0);
49  
50      private static final Pattern MD5_HEX_PATTERN = Pattern.compile("[a-fA-F0-9]{32}");
51  
52      FileTime lastModified, lastAccessTime, creationTime;
53  
54      long size;
55  
56      T filekey;
57  
58      String md5sum;
59  
60      FileType type;
61  
62      private MCRFileAttributes(final FileType type, final long size, final T filekey, final String md5sum,
63          final FileTime creationTime, final FileTime lastModified, final FileTime lastAccessTime) {
64          super();
65          this.type = type;
66          if (size < 0) {
67              throw new IllegalArgumentException("'size' cannot be negative");
68          }
69          this.size = size;
70          this.filekey = filekey;
71          setMd5sum(type, md5sum);
72          this.creationTime = creationTime == null ? lastModified == null ? EPOCHE_TIME : lastModified : creationTime;
73          this.lastModified = lastModified == null ? creationTime == null ? EPOCHE_TIME : creationTime : lastModified;
74          this.lastAccessTime = lastAccessTime == null ? EPOCHE_TIME : lastAccessTime;
75      }
76  
77      /**
78       *
79       */
80      public static <T> MCRFileAttributes<T> directory(final T filekey, final long size, final FileTime lastModified) {
81          return new MCRFileAttributes<>(FileType.directory, size, filekey, null, null, lastModified, null);
82      }
83  
84      public static <T> MCRFileAttributes<T> file(final T filekey, final long size, final String md5sum,
85          final FileTime lastModified) {
86          return file(filekey, size, md5sum, null, lastModified, null);
87      }
88  
89      public static <T> MCRFileAttributes<T> file(final T filekey, final long size, final String md5sum,
90          final FileTime creationTime, final FileTime lastModified, final FileTime lastAccessTime) {
91          return new MCRFileAttributes<>(FileType.file, size, filekey, md5sum, creationTime, lastModified,
92              lastAccessTime);
93      }
94  
95      public static <T> MCRFileAttributes<T> fromAttributes(BasicFileAttributes attrs, String md5) {
96          return new MCRFileAttributes<>(FileType.fromAttribute(attrs), attrs.size(), (T) attrs.fileKey(), md5,
97              attrs.creationTime(), attrs.lastModifiedTime(), attrs.lastAccessTime());
98      }
99  
100     /* (non-Javadoc)
101      * @see java.nio.file.attribute.BasicFileAttributes#creationTime()
102      */
103     @Override
104     public FileTime creationTime() {
105         return creationTime;
106     }
107 
108     /* (non-Javadoc)
109      * @see java.nio.file.attribute.BasicFileAttributes#fileKey()
110      */
111     @Override
112     public T fileKey() {
113         return filekey;
114     }
115 
116     /* (non-Javadoc)
117      * @see java.nio.file.attribute.BasicFileAttributes#isDirectory()
118      */
119     @Override
120     public boolean isDirectory() {
121         return type == FileType.directory;
122     }
123 
124     /* (non-Javadoc)
125      * @see java.nio.file.attribute.BasicFileAttributes#isOther()
126      */
127     @Override
128     public boolean isOther() {
129         return type == FileType.other;
130     }
131 
132     /* (non-Javadoc)
133      * @see java.nio.file.attribute.BasicFileAttributes#isRegularFile()
134      */
135     @Override
136     public boolean isRegularFile() {
137         return type == FileType.file;
138     }
139 
140     /* (non-Javadoc)
141      * @see java.nio.file.attribute.BasicFileAttributes#isSymbolicLink()
142      */
143     @Override
144     public boolean isSymbolicLink() {
145         return type == FileType.link;
146     }
147 
148     /* (non-Javadoc)
149      * @see java.nio.file.attribute.BasicFileAttributes#lastAccessTime()
150      */
151     @Override
152     public FileTime lastAccessTime() {
153         return lastAccessTime;
154     }
155 
156     /* (non-Javadoc)
157      * @see java.nio.file.attribute.BasicFileAttributes#lastModifiedTime()
158      */
159     @Override
160     public FileTime lastModifiedTime() {
161         return lastModified;
162     }
163 
164     public String md5sum() {
165         return md5sum;
166     }
167 
168     /* (non-Javadoc)
169      * @see java.nio.file.attribute.BasicFileAttributes#size()
170      */
171     @Override
172     public long size() {
173         return size;
174     }
175 
176     private void setMd5sum(final FileType type, final String md5sum) {
177         if (type != FileType.file && md5sum == null) {
178             return;
179         }
180         Objects.requireNonNull(md5sum, "'md5sum' is required for files");
181         if (md5sum.length() != 32 || !MD5_HEX_PATTERN.matcher(md5sum).matches()) {
182             throw new IllegalArgumentException("Not a valid md5sum: " + md5sum);
183         }
184         this.md5sum = md5sum;
185     }
186 
187 }