-
Notifications
You must be signed in to change notification settings - Fork 999
Expand file tree
/
Copy pathFileTime.java
More file actions
105 lines (87 loc) · 2.89 KB
/
FileTime.java
File metadata and controls
105 lines (87 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.nio.file.attribute;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
/**
* Convert FileTime units.
*
* This is a complete j2objc rewrite, designed to avoid Android's
* FileTime java.time dependencies. If an app only needs jre_channels (which many do),
* adding java.nio.file.FileTime to jre_channels would increase its size by ~23M.
*
* @author Mary Qin
*/
public class FileTime implements Comparable<FileTime>{
private final long millis;
FileTime(long millis) {
this.millis = millis;
}
public static FileTime from(long value, TimeUnit unit) {
return new FileTime(TimeUnit.MILLISECONDS.convert(value, unit));
}
public long to(TimeUnit unit) {
return unit.convert(millis, TimeUnit.MILLISECONDS);
}
public long toMillis() {
return millis;
}
public static FileTime fromMillis(long value) {
return new FileTime(value);
}
public native Instant toInstant() /*-[
id p = [JavaNioFileAttributeFileTime getProvider];
if (p) {
return [p toInstantWithJavaNioFileAttributeFileTime:self];
}
@throw create_JavaLangUnsupportedOperationException_initWithNSString_(@"java.time is not available");
]-*/;
public static FileTime from(Instant instant) {
return getProvider().fromInstant(instant);
}
private static Provider provider;
private static Provider getProvider() {
if (provider == null) {
try {
Class.forName("java.nio.file.attribute.FileTimeProvider").newInstance();
} catch (Exception e) {
throw new UnsupportedOperationException("java.time is not available", e);
}
}
return provider;
}
public interface Provider {
Object toInstant(FileTime fileTime);
FileTime fromInstant(Object instant);
}
public static void setProvider(Provider p) {
provider = p;
}
@Override
public int compareTo(FileTime o) {
if (o.millis < millis) {
return -1;
} else if (o.millis == millis) {
return 0;
}
return 1;
}
@Override
public boolean equals(Object o) {
return (o instanceof FileTime) ? compareTo((FileTime) o) == 0 : false;
}
}