| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one or more | |
| * contributor license agreements. See the NOTICE file distributed with | |
| * this work for additional information regarding copyright ownership. | |
| * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| * (the "License"); you may not use this file except in compliance with | |
| * the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package java.nio.file.attribute; | |
| import java.time.Instant; | |
| import java.util.concurrent.TimeUnit; | |
| /** | |
| * Convert FileTime units. | |
| * | |
| * This is a complete j2objc rewrite, designed to avoid Android's | |
| * FileTime java.time dependencies. If an app only needs jre_channels (which many do), | |
| * adding java.nio.file.FileTime to jre_channels would increase its size by ~23M. | |
| * | |
| * @author Mary Qin | |
| */ | |
| public class FileTime implements Comparable<FileTime>{ | |
| private final long millis; | |
| FileTime(long millis) { | |
| this.millis = millis; | |
| } | |
| public static FileTime from(long value, TimeUnit unit) { | |
| return new FileTime(TimeUnit.MILLISECONDS.convert(value, unit)); | |
| } | |
| public long to(TimeUnit unit) { | |
| return unit.convert(millis, TimeUnit.MILLISECONDS); | |
| } | |
| public long toMillis() { | |
| return millis; | |
| } | |
| public static FileTime fromMillis(long value) { | |
| return new FileTime(value); | |
| } | |
| public native Instant toInstant() /*-[ | |
| id p = [JavaNioFileAttributeFileTime getProvider]; | |
| if (p) { | |
| return [p toInstantWithJavaNioFileAttributeFileTime:self]; | |
| } | |
| @throw create_JavaLangUnsupportedOperationException_initWithNSString_(@"java.time is not available"); | |
| ]-*/; | |
| public static FileTime from(Instant instant) { | |
| return getProvider().fromInstant(instant); | |
| } | |
| private static Provider provider; | |
| private static Provider getProvider() { | |
| if (provider == null) { | |
| try { | |
| Class.forName("java.nio.file.attribute.FileTimeProvider").newInstance(); | |
| } catch (Exception e) { | |
| throw new UnsupportedOperationException("java.time is not available", e); | |
| } | |
| } | |
| return provider; | |
| } | |
| public interface Provider { | |
| Object toInstant(FileTime fileTime); | |
| FileTime fromInstant(Object instant); | |
| } | |
| public static void setProvider(Provider p) { | |
| provider = p; | |
| } | |
| @Override | |
| public int compareTo(FileTime o) { | |
| if (o.millis < millis) { | |
| return -1; | |
| } else if (o.millis == millis) { | |
| return 0; | |
| } | |
| return 1; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| return (o instanceof FileTime) ? compareTo((FileTime) o) == 0 : false; | |
| } | |
| } | |