All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.core.io.Node Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed 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 br.com.objectos.core.io;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Comparator;
import java.util.Objects;

/**
 * @author [email protected] (Marcio Endo)
 */
public abstract class Node implements Comparable {

  private final Path delegate;

  Node(Path delegate) {
    this.delegate = delegate;
  }

  static Node of(Path path) {
    return Files.isDirectory(path)
        ? Directory.at(path.toFile())
        : File.of(path);
  }

  public static OrderBy orderBy() {
    return OrderBy.INSTANCE;
  }

  @Override
  public int compareTo(Node o) {
    int nameCount = Integer.compare(delegate.getNameCount(), o.delegate.getNameCount());

    if (nameCount != 0) {
      return nameCount;
    }

    return delegate.compareTo(o.delegate);
  }

  public void delete() throws IOException {
    if (exists()) {
      Files.delete(delegate);
    }
  }

  public void deleteUnchecked() {
    try {
      delete();
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  @Override
  public final boolean equals(final Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof Node)) {
      return false;
    }
    final Node that = (Node) obj;
    return Objects.equals(delegate, that.delegate);
  }

  public final boolean exists() {
    return Files.exists(delegate);
  }

  @Override
  public final int hashCode() {
    return Objects.hashCode(delegate);
  }

  public boolean isDirectory() {
    return false;
  }

  public boolean isFile() {
    return false;
  }

  public java.io.File toFile() {
    return delegate.toFile();
  }

  @Override
  public final String toString() {
    return delegate.toString();
  }

  Path delegate() {
    return delegate;
  }

  private BasicFileAttributes basicFileAttributes() {
    try {
      return Files.readAttributes(delegate, BasicFileAttributes.class);
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  private FileTime lastModifiedTime() {
    return basicFileAttributes().lastModifiedTime();
  }

  public static class OrderBy {

    private static final OrderBy INSTANCE = new OrderBy();

    private static final Comparator LAST_MODIFIED = (a, b) -> a.lastModifiedTime()
        .compareTo(b.lastModifiedTime());

    private OrderBy() {
    }

    public Comparator lastModified() {
      return LAST_MODIFIED;
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy