author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8808 | fc799c458da8 |
child 14014 | da3648e13e67 |
permissions | -rw-r--r-- |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8808
diff
changeset
|
2 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
4 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
10 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
15 |
* accompanied this code). |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
16 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
20 |
* |
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
24 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
25 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
26 |
package java.nio.file.attribute; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
27 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
28 |
import java.util.Calendar; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
29 |
import java.util.GregorianCalendar; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
30 |
import java.util.Date; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
31 |
import java.util.Formatter; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
32 |
import java.util.Locale; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
33 |
import java.util.TimeZone; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
34 |
import java.util.concurrent.TimeUnit; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
35 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
36 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
37 |
* Represents the value of a file's time stamp attribute. For example, it may |
8158 | 38 |
* represent the time that the file was last |
39 |
* {@link BasicFileAttributes#lastModifiedTime() modified}, |
|
40 |
* {@link BasicFileAttributes#lastAccessTime() accessed}, |
|
41 |
* or {@link BasicFileAttributes#creationTime() created}. |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
42 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
43 |
* <p> Instances of this class are immutable. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
44 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
45 |
* @since 1.7 |
8158 | 46 |
* @see java.nio.file.Files#setLastModifiedTime |
47 |
* @see java.nio.file.Files#getLastModifiedTime |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
48 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
49 |
|
8158 | 50 |
public final class FileTime |
51 |
implements Comparable<FileTime> |
|
52 |
{ |
|
53 |
/** |
|
54 |
* The value since the epoch; can be negative. |
|
55 |
*/ |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
56 |
private final long value; |
8158 | 57 |
|
58 |
/** |
|
59 |
* The unit of granularity to interpret the value. |
|
60 |
*/ |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
61 |
private final TimeUnit unit; |
8158 | 62 |
|
63 |
/** |
|
64 |
* The value return by toString (created lazily) |
|
65 |
*/ |
|
66 |
private String valueAsString; |
|
67 |
||
68 |
/** |
|
69 |
* The value in days and excess nanos (created lazily) |
|
70 |
*/ |
|
71 |
private DaysAndNanos daysAndNanos; |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
72 |
|
8158 | 73 |
/** |
74 |
* Returns a DaysAndNanos object representing the value. |
|
75 |
*/ |
|
76 |
private DaysAndNanos asDaysAndNanos() { |
|
77 |
if (daysAndNanos == null) |
|
78 |
daysAndNanos = new DaysAndNanos(value, unit); |
|
79 |
return daysAndNanos; |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Initializes a new instance of this class. |
|
84 |
*/ |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
85 |
private FileTime(long value, TimeUnit unit) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
86 |
if (unit == null) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
87 |
throw new NullPointerException(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
88 |
this.value = value; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
89 |
this.unit = unit; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
90 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
91 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
92 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
93 |
* Returns a {@code FileTime} representing a value at the given unit of |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
94 |
* granularity. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
95 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
96 |
* @param value |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
97 |
* the value since the epoch (1970-01-01T00:00:00Z); can be |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
98 |
* negative |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
99 |
* @param unit |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
100 |
* the unit of granularity to interpret the value |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
101 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
102 |
* @return a {@code FileTime} representing the given value |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
103 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
104 |
public static FileTime from(long value, TimeUnit unit) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
105 |
return new FileTime(value, unit); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
106 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
107 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
108 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
109 |
* Returns a {@code FileTime} representing the given value in milliseconds. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
110 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
111 |
* @param value |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
112 |
* the value, in milliseconds, since the epoch |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
113 |
* (1970-01-01T00:00:00Z); can be negative |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
114 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
115 |
* @return a {@code FileTime} representing the given value |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
116 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
117 |
public static FileTime fromMillis(long value) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
118 |
return new FileTime(value, TimeUnit.MILLISECONDS); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
119 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
120 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
121 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
122 |
* Returns the value at the given unit of granularity. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
123 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
124 |
* <p> Conversion from a coarser granularity that would numerically overflow |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
125 |
* saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
126 |
* if positive. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
127 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
128 |
* @param unit |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
129 |
* the unit of granularity for the return value |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
130 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
131 |
* @return value in the given unit of granularity, since the epoch |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
132 |
* since the epoch (1970-01-01T00:00:00Z); can be negative |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
133 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
134 |
public long to(TimeUnit unit) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
135 |
return unit.convert(this.value, this.unit); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
136 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
137 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
138 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
139 |
* Returns the value in milliseconds. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
140 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
141 |
* <p> Conversion from a coarser granularity that would numerically overflow |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
142 |
* saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
143 |
* if positive. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
144 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
145 |
* @return the value in milliseconds, since the epoch (1970-01-01T00:00:00Z) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
146 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
147 |
public long toMillis() { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
148 |
return unit.toMillis(value); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
149 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
150 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
151 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
152 |
* Tests this {@code FileTime} for equality with the given object. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
153 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
154 |
* <p> The result is {@code true} if and only if the argument is not {@code |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
155 |
* null} and is a {@code FileTime} that represents the same time. This |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
156 |
* method satisfies the general contract of the {@code Object.equals} method. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
157 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
158 |
* @param obj |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
159 |
* the object to compare with |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
160 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
161 |
* @return {@code true} if, and only if, the given object is a {@code |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
162 |
* FileTime} that represents the same time |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
163 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
164 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
165 |
public boolean equals(Object obj) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
166 |
return (obj instanceof FileTime) ? compareTo((FileTime)obj) == 0 : false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
167 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
168 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
169 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
170 |
* Computes a hash code for this file time. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
171 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
172 |
* <p> The hash code is based upon the value represented, and satisfies the |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
173 |
* general contract of the {@link Object#hashCode} method. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
174 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
175 |
* @return the hash-code value |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
176 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
177 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
178 |
public int hashCode() { |
8158 | 179 |
// hashcode of days/nanos representation to satisfy contract with equals |
180 |
return asDaysAndNanos().hashCode(); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
181 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
182 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
183 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
184 |
* Compares the value of two {@code FileTime} objects for order. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
185 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
186 |
* @param other |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
187 |
* the other {@code FileTime} to be compared |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
188 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
189 |
* @return {@code 0} if this {@code FileTime} is equal to {@code other}, a |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
190 |
* value less than 0 if this {@code FileTime} represents a time |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
191 |
* that is before {@code other}, and a value greater than 0 if this |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
192 |
* {@code FileTime} represents a time that is after {@code other} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
193 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
194 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
195 |
public int compareTo(FileTime other) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
196 |
// same granularity |
8158 | 197 |
if (unit == other.unit) { |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
198 |
return (value < other.value) ? -1 : (value == other.value ? 0 : 1); |
8158 | 199 |
} else { |
200 |
// compare using days/nanos representation when unit differs |
|
201 |
return asDaysAndNanos().compareTo(other.asDaysAndNanos()); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
202 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
203 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
204 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
205 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
206 |
* Returns the string representation of this {@code FileTime}. The string |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
207 |
* is returned in the <a |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
208 |
* href="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> format: |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
209 |
* <pre> |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
210 |
* YYYY-MM-DDThh:mm:ss[.s+]Z |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
211 |
* </pre> |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
212 |
* where "{@code [.s+]}" represents a dot followed by one of more digits |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
213 |
* for the decimal fraction of a second. It is only present when the decimal |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
214 |
* fraction of a second is not zero. For example, {@code |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
215 |
* FileTime.fromMillis(1234567890000L).toString()} yields {@code |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
216 |
* "2009-02-13T23:31:30Z"}, and {@code FileTime.fromMillis(1234567890123L).toString()} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
217 |
* yields {@code "2009-02-13T23:31:30.123Z"}. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
218 |
* |
8808 | 219 |
* <p> A {@code FileTime} is primarily intended to represent the value of a |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
220 |
* file's time stamp. Where used to represent <i>extreme values</i>, where |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
221 |
* the year is less than "{@code 0001}" or greater than "{@code 9999}" then |
8808 | 222 |
* this method deviates from ISO 8601 in the same manner as the |
223 |
* <a href="http://www.w3.org/TR/xmlschema-2/#deviantformats">XML Schema |
|
224 |
* language</a>. That is, the year may be expanded to more than four digits |
|
225 |
* and may be negative-signed. If more than four digits then leading zeros |
|
226 |
* are not present. The year before "{@code 0001}" is "{@code -0001}". |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
227 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
228 |
* @return the string representation of this file time |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
229 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
230 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
231 |
public String toString() { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
232 |
String v = valueAsString; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
233 |
if (v == null) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
234 |
// overflow saturates to Long.MIN_VALUE or Long.MAX_VALUE so this |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
235 |
// limits the range: |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
236 |
// [-292275056-05-16T16:47:04.192Z,292278994-08-17T07:12:55.807Z] |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
237 |
long ms = toMillis(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
238 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
239 |
// nothing to do when seconds/minutes/hours/days |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
240 |
String fractionAsString = ""; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
241 |
if (unit.compareTo(TimeUnit.SECONDS) < 0) { |
8158 | 242 |
long fraction = asDaysAndNanos().fractionOfSecondInNanos(); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
243 |
if (fraction != 0L) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
244 |
// fraction must be positive |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
245 |
if (fraction < 0L) { |
8158 | 246 |
final long MAX_FRACTION_PLUS_1 = 1000L * 1000L * 1000L; |
247 |
fraction += MAX_FRACTION_PLUS_1; |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
248 |
if (ms != Long.MIN_VALUE) ms--; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
249 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
250 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
251 |
// convert to String, adding leading zeros as required and |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
252 |
// stripping any trailing zeros |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
253 |
String s = Long.toString(fraction); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
254 |
int len = s.length(); |
8158 | 255 |
int width = 9 - len; |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
256 |
StringBuilder sb = new StringBuilder("."); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
257 |
while (width-- > 0) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
258 |
sb.append('0'); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
259 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
260 |
if (s.charAt(len-1) == '0') { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
261 |
// drop trailing zeros |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
262 |
len--; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
263 |
while (s.charAt(len-1) == '0') |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
264 |
len--; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
265 |
sb.append(s.substring(0, len)); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
266 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
267 |
sb.append(s); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
268 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
269 |
fractionAsString = sb.toString(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
270 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
271 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
272 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
273 |
// create calendar to use with formatter. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
274 |
GregorianCalendar cal = |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
275 |
new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.ROOT); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
276 |
if (value < 0L) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
277 |
cal.setGregorianChange(new Date(Long.MIN_VALUE)); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
278 |
cal.setTimeInMillis(ms); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
279 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
280 |
// years are negative before common era |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
281 |
String sign = (cal.get(Calendar.ERA) == GregorianCalendar.BC) ? "-" : ""; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
282 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
283 |
// [-]YYYY-MM-DDThh:mm:ss[.s]Z |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
284 |
v = new Formatter(Locale.ROOT) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
285 |
.format("%s%tFT%tR:%tS%sZ", sign, cal, cal, cal, fractionAsString) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
286 |
.toString(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
287 |
valueAsString = v; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
288 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
289 |
return v; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
290 |
} |
8158 | 291 |
|
292 |
/** |
|
293 |
* Represents a FileTime's value as two longs: the number of days since |
|
294 |
* the epoch, and the excess (in nanoseconds). This is used for comparing |
|
295 |
* values with different units of granularity. |
|
296 |
*/ |
|
297 |
private static class DaysAndNanos implements Comparable<DaysAndNanos> { |
|
298 |
// constants for conversion |
|
299 |
private static final long C0 = 1L; |
|
300 |
private static final long C1 = C0 * 24L; |
|
301 |
private static final long C2 = C1 * 60L; |
|
302 |
private static final long C3 = C2 * 60L; |
|
303 |
private static final long C4 = C3 * 1000L; |
|
304 |
private static final long C5 = C4 * 1000L; |
|
305 |
private static final long C6 = C5 * 1000L; |
|
306 |
||
307 |
/** |
|
308 |
* The value (in days) since the epoch; can be negative. |
|
309 |
*/ |
|
310 |
private final long days; |
|
311 |
||
312 |
/** |
|
313 |
* The excess (in nanoseconds); can be negative if days <= 0. |
|
314 |
*/ |
|
315 |
private final long excessNanos; |
|
316 |
||
317 |
/** |
|
318 |
* Initializes a new instance of this class. |
|
319 |
*/ |
|
320 |
DaysAndNanos(long value, TimeUnit unit) { |
|
321 |
long scale; |
|
322 |
switch (unit) { |
|
323 |
case DAYS : scale = C0; break; |
|
324 |
case HOURS : scale = C1; break; |
|
325 |
case MINUTES : scale = C2; break; |
|
326 |
case SECONDS : scale = C3; break; |
|
327 |
case MILLISECONDS : scale = C4; break; |
|
328 |
case MICROSECONDS : scale = C5; break; |
|
329 |
case NANOSECONDS : scale = C6; break; |
|
330 |
default : throw new AssertionError("Unit not handled"); |
|
331 |
} |
|
332 |
this.days = unit.toDays(value); |
|
333 |
this.excessNanos = unit.toNanos(value - (this.days * scale)); |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Returns the fraction of a second, in nanoseconds. |
|
338 |
*/ |
|
339 |
long fractionOfSecondInNanos() { |
|
340 |
return excessNanos % (1000L * 1000L * 1000L); |
|
341 |
} |
|
342 |
||
343 |
@Override |
|
344 |
public boolean equals(Object obj) { |
|
345 |
return (obj instanceof DaysAndNanos) ? |
|
346 |
compareTo((DaysAndNanos)obj) == 0 : false; |
|
347 |
} |
|
348 |
||
349 |
@Override |
|
350 |
public int hashCode() { |
|
351 |
return (int)(days ^ (days >>> 32) ^ |
|
352 |
excessNanos ^ (excessNanos >>> 32)); |
|
353 |
} |
|
354 |
||
355 |
@Override |
|
356 |
public int compareTo(DaysAndNanos other) { |
|
357 |
if (this.days != other.days) |
|
358 |
return (this.days < other.days) ? -1 : 1; |
|
359 |
return (this.excessNanos < other.excessNanos) ? -1 : |
|
360 |
(this.excessNanos == other.excessNanos) ? 0 : 1; |
|
361 |
} |
|
362 |
} |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
363 |
} |