jdk/src/share/classes/java/nio/file/attribute/FileTime.java
author alanb
Sun, 28 Apr 2013 21:06:36 +0100
changeset 17190 7e650321026c
parent 16856 2aaed0433c15
child 23010 6dadb192ad81
permissions -rw-r--r--
8013413: javadoc warnings Reviewed-by: lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14014
diff changeset
     2
 * Copyright (c) 2009, 2012, 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
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
     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
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
     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
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
    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
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    28
import java.time.Instant;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    29
import java.time.LocalDateTime;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    30
import java.time.ZoneOffset;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    31
import java.util.Objects;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    32
import java.util.concurrent.TimeUnit;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    33
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    34
/**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    35
 * Represents the value of a file's time stamp attribute. For example, it may
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    36
 * represent the time that the file was last
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    37
 * {@link BasicFileAttributes#lastModifiedTime() modified},
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    38
 * {@link BasicFileAttributes#lastAccessTime() accessed},
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    39
 * or {@link BasicFileAttributes#creationTime() created}.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    40
 *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    41
 * <p> Instances of this class are immutable.
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
 * @since 1.7
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    44
 * @see java.nio.file.Files#setLastModifiedTime
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    45
 * @see java.nio.file.Files#getLastModifiedTime
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    46
 */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    47
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    48
public final class FileTime
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    49
    implements Comparable<FileTime>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    50
{
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    51
    /**
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    52
     * The unit of granularity to interpret the value. Null if
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    53
     * this {@code FileTime} is converted from an {@code Instant},
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    54
     * the {@code value} and {@code unit} pair will not be used
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    55
     * in this scenario.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    56
     */
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    57
    private final TimeUnit unit;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    58
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    59
    /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    60
     * The value since the epoch; can be negative.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    61
     */
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    62
    private final long value;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    63
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    64
    /**
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    65
     * The value as Instant (created lazily, if not from an instant)
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    66
     */
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    67
    private Instant instant;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    68
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    69
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    70
     * The value return by toString (created lazily)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    71
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    72
    private String valueAsString;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    73
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    74
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    75
     * Initializes a new instance of this class.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    76
     */
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    77
    private FileTime(long value, TimeUnit unit, Instant instant) {
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    78
        this.value = value;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    79
        this.unit = unit;
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    80
        this.instant = instant;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    81
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    82
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    83
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    84
     * 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
    85
     * granularity.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    86
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    87
     * @param   value
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    88
     *          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
    89
     *          negative
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    90
     * @param   unit
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    91
     *          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
    92
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    93
     * @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
    94
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    95
    public static FileTime from(long value, TimeUnit unit) {
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    96
        Objects.requireNonNull(unit, "unit");
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
    97
        return new FileTime(value, unit, null);
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    98
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    99
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   100
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   101
     * 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
   102
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   103
     * @param   value
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   104
     *          the value, in milliseconds, since the epoch
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   105
     *          (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
   106
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   107
     * @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
   108
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   109
    public static FileTime fromMillis(long value) {
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   110
        return new FileTime(value, TimeUnit.MILLISECONDS, null);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   111
    }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   112
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   113
    /**
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   114
     * Returns a {@code FileTime} representing the same point of time value
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   115
     * on the time-line as the provided {@code Instant} object.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   116
     *
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   117
     * @param   instant
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   118
     *          the instant to convert
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   119
     * @return  a {@code FileTime} representing the same point on the time-line
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   120
     *          as the provided instant
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   121
     * @since 1.8
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   122
     */
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   123
    public static FileTime from(Instant instant) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   124
        Objects.requireNonNull(instant, "instant");
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   125
        return new FileTime(0, null, instant);
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   126
    }
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
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   129
     * 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
   130
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   131
     * <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
   132
     * 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
   133
     * if positive.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   134
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   135
     * @param   unit
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   136
     *          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
   137
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   138
     * @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
   139
     *          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
   140
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   141
    public long to(TimeUnit unit) {
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   142
        Objects.requireNonNull(unit, "unit");
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   143
        if (this.unit != null) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   144
            return unit.convert(this.value, this.unit);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   145
        } else {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   146
            long secs = unit.convert(instant.getEpochSecond(), TimeUnit.SECONDS);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   147
            if (secs == Long.MIN_VALUE || secs == Long.MAX_VALUE) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   148
                return secs;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   149
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   150
            long nanos = unit.convert(instant.getNano(), TimeUnit.NANOSECONDS);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   151
            long r = secs + nanos;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   152
            // Math.addExact() variant
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   153
            if (((secs ^ r) & (nanos ^ r)) < 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   154
                return (secs < 0) ? Long.MIN_VALUE : Long.MAX_VALUE;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   155
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   156
            return r;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   157
        }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   158
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   159
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
     * Returns the value in milliseconds.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   162
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   163
     * <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
   164
     * 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
   165
     * if positive.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   166
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   167
     * @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
   168
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   169
    public long toMillis() {
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   170
        if (unit != null) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   171
            return unit.toMillis(value);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   172
        } else {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   173
            long secs = instant.getEpochSecond();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   174
            int  nanos = instant.getNano();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   175
            // Math.multiplyExact() variant
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   176
            long r = secs * 1000;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   177
            long ax = Math.abs(secs);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   178
            if (((ax | 1000) >>> 31 != 0)) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   179
                if ((r / 1000) != secs) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   180
                    return (secs < 0) ? Long.MIN_VALUE : Long.MAX_VALUE;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   181
                }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   182
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   183
            return r + nanos / 1000_000;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   184
        }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   185
    }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   186
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   187
    /**
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   188
     * Time unit constants for conversion.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   189
     */
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   190
    private static final long HOURS_PER_DAY      = 24L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   191
    private static final long MINUTES_PER_HOUR   = 60L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   192
    private static final long SECONDS_PER_MINUTE = 60L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   193
    private static final long SECONDS_PER_HOUR   = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   194
    private static final long SECONDS_PER_DAY    = SECONDS_PER_HOUR * HOURS_PER_DAY;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   195
    private static final long MILLIS_PER_SECOND  = 1000L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   196
    private static final long MICROS_PER_SECOND  = 1000_000L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   197
    private static final long NANOS_PER_SECOND   = 1000_000_000L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   198
    private static final int  NANOS_PER_MILLI    = 1000_000;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   199
    private static final int  NANOS_PER_MICRO    = 1000;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   200
    // The epoch second of Instant.MIN.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   201
    private static final long MIN_SECOND = -31557014167219200L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   202
    // The epoch second of Instant.MAX.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   203
    private static final long MAX_SECOND = 31556889864403199L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   204
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   205
    /*
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   206
     * Scale d by m, checking for overflow.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   207
     */
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   208
    private static long scale(long d, long m, long over) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   209
        if (d >  over) return Long.MAX_VALUE;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   210
        if (d < -over) return Long.MIN_VALUE;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   211
        return d * m;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   212
    }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   213
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   214
    /**
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   215
     * Converts this {@code FileTime} object to an {@code Instant}.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   216
     *
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   217
     * <p> The conversion creates an {@code Instant} that represents the
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   218
     * same point on the time-line as this {@code FileTime}.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   219
     *
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   220
     * <p> {@code FileTime} can store points on the time-line further in the
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   221
     * future and further in the past than {@code Instant}. Conversion
17190
7e650321026c 8013413: javadoc warnings
alanb
parents: 16856
diff changeset
   222
     * from such further time points saturates to {@link Instant#MIN} if
7e650321026c 8013413: javadoc warnings
alanb
parents: 16856
diff changeset
   223
     * earlier than {@code Instant.MIN} or {@link Instant#MAX} if later
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   224
     * than {@code Instant.MAX}.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   225
     *
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   226
     * @return  an instant representing the same point on the time-line as
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   227
     *          this {@code FileTime} object
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   228
     * @since 1.8
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   229
     */
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   230
    public Instant toInstant() {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   231
        if (instant == null) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   232
            long secs = 0L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   233
            int nanos = 0;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   234
            switch (unit) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   235
                case DAYS:
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   236
                    secs = scale(value, SECONDS_PER_DAY,
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   237
                                 Long.MAX_VALUE/SECONDS_PER_DAY);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   238
                    break;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   239
                case HOURS:
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   240
                    secs = scale(value, SECONDS_PER_HOUR,
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   241
                                 Long.MAX_VALUE/SECONDS_PER_HOUR);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   242
                    break;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   243
                case MINUTES:
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   244
                    secs = scale(value, SECONDS_PER_MINUTE,
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   245
                                 Long.MAX_VALUE/SECONDS_PER_MINUTE);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   246
                    break;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   247
                case SECONDS:
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   248
                    secs = value;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   249
                    break;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   250
                case MILLISECONDS:
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   251
                    secs = Math.floorDiv(value, MILLIS_PER_SECOND);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   252
                    nanos = (int)Math.floorMod(value, MILLIS_PER_SECOND)
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   253
                            * NANOS_PER_MILLI;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   254
                    break;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   255
                case MICROSECONDS:
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   256
                    secs = Math.floorDiv(value, MICROS_PER_SECOND);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   257
                    nanos = (int)Math.floorMod(value, MICROS_PER_SECOND)
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   258
                            * NANOS_PER_MICRO;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   259
                    break;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   260
                case NANOSECONDS:
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   261
                    secs = Math.floorDiv(value, NANOS_PER_SECOND);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   262
                    nanos = (int)Math.floorMod(value, NANOS_PER_SECOND);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   263
                    break;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   264
                default : throw new AssertionError("Unit not handled");
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   265
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   266
            if (secs <= MIN_SECOND)
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   267
                instant = Instant.MIN;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   268
            else if (secs >= MAX_SECOND)
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   269
                instant = Instant.MAX;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   270
            else
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   271
                instant = Instant.ofEpochSecond(secs, nanos);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   272
        }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   273
        return instant;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   274
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   275
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   276
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   277
     * 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
   278
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   279
     * <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
   280
     * 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
   281
     * 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
   282
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   283
     * @param   obj
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   284
     *          the object to compare with
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   285
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   286
     * @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
   287
     *          FileTime} that represents the same time
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
    @Override
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   290
    public boolean equals(Object obj) {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   291
        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
   292
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   293
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   294
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   295
     * 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
   296
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   297
     * <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
   298
     * 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
   299
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   300
     * @return  the hash-code value
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   301
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   302
    @Override
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   303
    public int hashCode() {
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   304
        // hashcode of instant representation to satisfy contract with equals
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   305
        return toInstant().hashCode();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   306
    }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   307
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   308
    private long toDays() {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   309
        if (unit != null) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   310
            return unit.toDays(value);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   311
        } else {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   312
            return TimeUnit.SECONDS.toDays(toInstant().getEpochSecond());
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   313
        }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   314
    }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   315
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   316
    private long toExcessNanos(long days) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   317
        if (unit != null) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   318
            return unit.toNanos(value - unit.convert(days, TimeUnit.DAYS));
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   319
        } else {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   320
            return TimeUnit.SECONDS.toNanos(toInstant().getEpochSecond()
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   321
                                            - TimeUnit.DAYS.toSeconds(days));
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   322
        }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   323
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   324
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   325
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   326
     * 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
   327
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   328
     * @param   other
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   329
     *          the other {@code FileTime} to be compared
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   330
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   331
     * @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
   332
     *          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
   333
     *          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
   334
     *          {@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
   335
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   336
    @Override
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   337
    public int compareTo(FileTime other) {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   338
        // same granularity
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   339
        if (unit != null && unit == other.unit) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   340
            return Long.compare(value, other.value);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   341
        } else {
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   342
            // compare using instant representation when unit differs
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   343
            long secs = toInstant().getEpochSecond();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   344
            long secsOther = other.toInstant().getEpochSecond();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   345
            int cmp = Long.compare(secs, secsOther);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   346
            if (cmp != 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   347
                return cmp;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   348
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   349
            cmp = Long.compare(toInstant().getNano(), other.toInstant().getNano());
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   350
            if (cmp != 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   351
                return cmp;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   352
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   353
            if (secs != MAX_SECOND && secs != MIN_SECOND) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   354
                return 0;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   355
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   356
            // if both this and other's Instant reps are MIN/MAX,
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   357
            // use daysSinceEpoch and nanosOfDays, which will not
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   358
            // saturate during calculation.
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   359
            long days = toDays();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   360
            long daysOther = other.toDays();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   361
            if (days == daysOther) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   362
                return Long.compare(toExcessNanos(days), other.toExcessNanos(daysOther));
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   363
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   364
            return Long.compare(days, daysOther);
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   365
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   366
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   367
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   368
    // days in a 400 year cycle = 146097
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   369
    // days in a 10,000 year cycle = 146097 * 25
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   370
    // seconds per day = 86400
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   371
    private static final long DAYS_PER_10000_YEARS = 146097L * 25L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   372
    private static final long SECONDS_PER_10000_YEARS = 146097L * 25L * 86400L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   373
    private static final long SECONDS_0000_TO_1970 = ((146097L * 5L) - (30L * 365L + 7L)) * 86400L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   374
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   375
    // append year/month/day/hour/minute/second/nano with width and 0 padding
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   376
    private StringBuilder append(StringBuilder sb, int w, int d) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   377
        while (w > 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   378
            sb.append((char)(d/w + '0'));
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   379
            d = d % w;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   380
            w /= 10;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   381
        }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   382
        return sb;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   383
    }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   384
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   385
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   386
     * 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
   387
     * is returned in the <a
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   388
     * href="http://www.w3.org/TR/NOTE-datetime">ISO&nbsp;8601</a> format:
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   389
     * <pre>
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   390
     *     YYYY-MM-DDThh:mm:ss[.s+]Z
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   391
     * </pre>
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   392
     * 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
   393
     * 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
   394
     * 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
   395
     * FileTime.fromMillis(1234567890000L).toString()} yields {@code
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   396
     * "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
   397
     * 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
   398
     *
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
   399
     * <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
   400
     * 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
   401
     * the year is less than "{@code 0001}" or greater than "{@code 9999}" then
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
   402
     * this method deviates from ISO 8601 in the same manner as the
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
   403
     * <a href="http://www.w3.org/TR/xmlschema-2/#deviantformats">XML Schema
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
   404
     * language</a>. That is, the year may be expanded to more than four digits
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
   405
     * and may be negative-signed. If more than four digits then leading zeros
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8158
diff changeset
   406
     * 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
   407
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   408
     * @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
   409
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   410
    @Override
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   411
    public String toString() {
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   412
        if (valueAsString == null) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   413
            long secs = 0L;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   414
            int  nanos = 0;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   415
            if (instant == null && unit.compareTo(TimeUnit.SECONDS) >= 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   416
                secs = unit.toSeconds(value);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   417
            } else {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   418
                secs = toInstant().getEpochSecond();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   419
                nanos = toInstant().getNano();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   420
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   421
            LocalDateTime ldt;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   422
            int year = 0;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   423
            if (secs >= -SECONDS_0000_TO_1970) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   424
                // current era
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   425
                long zeroSecs = secs - SECONDS_PER_10000_YEARS + SECONDS_0000_TO_1970;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   426
                long hi = Math.floorDiv(zeroSecs, SECONDS_PER_10000_YEARS) + 1;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   427
                long lo = Math.floorMod(zeroSecs, SECONDS_PER_10000_YEARS);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   428
                ldt = LocalDateTime.ofEpochSecond(lo - SECONDS_0000_TO_1970, nanos, ZoneOffset.UTC);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   429
                year = ldt.getYear() +  (int)hi * 10000;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   430
            } else {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   431
                // before current era
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   432
                long zeroSecs = secs + SECONDS_0000_TO_1970;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   433
                long hi = zeroSecs / SECONDS_PER_10000_YEARS;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   434
                long lo = zeroSecs % SECONDS_PER_10000_YEARS;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   435
                ldt = LocalDateTime.ofEpochSecond(lo - SECONDS_0000_TO_1970, nanos, ZoneOffset.UTC);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   436
                year = ldt.getYear() + (int)hi * 10000;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   437
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   438
            if (year <= 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   439
                year = year - 1;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   440
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   441
            int fraction = ldt.getNano();
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   442
            StringBuilder sb = new StringBuilder(64);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   443
            sb.append(year < 0 ? "-" : "");
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   444
            year = Math.abs(year);
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   445
            if (year < 10000) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   446
                append(sb, 1000, Math.abs(year));
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   447
            } else {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   448
                sb.append(String.valueOf(year));
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   449
            }
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   450
            sb.append('-');
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   451
            append(sb, 10, ldt.getMonthValue());
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   452
            sb.append('-');
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   453
            append(sb, 10, ldt.getDayOfMonth());
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   454
            sb.append('T');
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   455
            append(sb, 10, ldt.getHour());
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   456
            sb.append(':');
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   457
            append(sb, 10, ldt.getMinute());
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   458
            sb.append(':');
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   459
            append(sb, 10, ldt.getSecond());
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   460
            if (fraction != 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   461
                sb.append('.');
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   462
                // adding leading zeros and stripping any trailing zeros
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   463
                int w = 100_000_000;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   464
                while (fraction % 10 == 0) {
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   465
                    fraction /= 10;
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   466
                    w /= 10;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   467
                }
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   468
                append(sb, w, fraction);
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   469
            }
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   470
            sb.append('Z');
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   471
            valueAsString = sb.toString();
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   472
        }
16856
2aaed0433c15 8011647: Add java.time.Instant methods to java.nio.file.attribute.FileTime
sherman
parents: 14342
diff changeset
   473
        return valueAsString;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   474
    }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   475
}