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

com.github.protobufel.common.files.resources.Resources Maven / Gradle / Ivy

The newest version!
/*
 * BSD 3-Clause License
 *
 * Copyright (c) 2017, David Tesler
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 *  Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 *  Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

package com.github.protobufel.common.files.resources;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.github.protobufel.common.verifications.Verifications.assertNonNull;

@NonNullByDefault
public class Resources {
  private Resources() {}

  private interface ISharedFileSet {

    public String getDirectory();

    public List getIncludes();

    public List getExcludes();

    public boolean isAllowDirs();

    public boolean isAllowFiles();
  }

  public interface IFileSet extends ISharedFileSet {}

  public static final class FileSet implements IFileSet {
    //TODO make it Serializable
    private final String directory;
    private final List includes;
    private final List excludes;
    private final boolean allowDirs;
    private final boolean allowFiles;

    public FileSet(final IFileSet other) {
      this(other.getDirectory(), other.getIncludes(), other.getExcludes(), false, true);
    }

    public FileSet(
        final String directory,
        final List includes,
        final List excludes,
        final boolean allowDirs,
        final boolean allowFiles) {
      this.directory = directory;
      this.includes =
          assertNonNull(
              includes.isEmpty()
                  ? Collections.emptyList()
                  : Collections.unmodifiableList(new ArrayList(includes)));
      this.excludes =
          assertNonNull(
              excludes.isEmpty()
                  ? Collections.emptyList()
                  : Collections.unmodifiableList(new ArrayList(excludes)));
      this.allowDirs = allowDirs;
      this.allowFiles = allowFiles;
    }

    public static Builder builder() {
      return new Builder();
    }

    public static Builder builder(final IFileSet other) {
      return new Builder(other);
    }

    @Override
    public String getDirectory() {
      return directory;
    }

    @Override
    public List getIncludes() {
      return includes;
    }

    @Override
    public List getExcludes() {
      return excludes;
    }

    @Override
    public boolean isAllowDirs() {
      return allowDirs;
    }

    @Override
    public boolean isAllowFiles() {
      return allowFiles;
    }

    @Override
    public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + (allowDirs ? 1231 : 1237);
      result = prime * result + (allowFiles ? 1231 : 1237);
      result = prime * result + ((directory == null) ? 0 : directory.hashCode());
      result = prime * result + ((excludes == null) ? 0 : excludes.hashCode());
      result = prime * result + ((includes == null) ? 0 : includes.hashCode());
      return result;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
      if (this == obj) {
        return true;
      }
      if (obj == null) {
        return false;
      }
      if (!(obj instanceof FileSet)) {
        return false;
      }
      FileSet other = (FileSet) obj;
      if (allowDirs != other.allowDirs) {
        return false;
      }
      if (allowFiles != other.allowFiles) {
        return false;
      }
      if (!directory.equals(other.directory)) {
        return false;
      }
      if (!excludes.equals(other.excludes)) {
        return false;
      }
      if (!includes.equals(other.includes)) {
        return false;
      }
      return true;
    }

    @Override
    public String toString() {
      return "FileSet [directory="
          + directory
          + ", includes="
          + includes
          + ", excludes="
          + excludes
          + ", allowDirs="
          + allowDirs
          + ", allowFiles="
          + allowFiles
          + "]";
    }

    public static final class Builder implements ISharedFileSet {
      private String directory;
      private List includes;
      private List excludes;
      private boolean allowDirs;
      private boolean allowFiles;

      public Builder() {
        directory = "";
        includes = new ArrayList();
        excludes = new ArrayList();
        allowDirs = false;
        allowFiles = true;
      }

      public Builder(final ISharedFileSet other) {
        this(
            other.getDirectory(),
            other.getIncludes(),
            other.getExcludes(),
            other.isAllowDirs(),
            other.isAllowFiles());
      }

      private Builder(
          final String directory,
          final List includes,
          final List excludes,
          final boolean allowDirs,
          final boolean allowFiles) {
        this.directory = directory;
        this.includes = new ArrayList(includes);
        this.excludes = new ArrayList(excludes);
        this.allowDirs = allowDirs;
        this.allowFiles = allowFiles;
      }

      public String getDirectory() {
        return directory;
      }

      public boolean isAllowDirs() {
        return allowDirs;
      }

      public Builder allowDirs(boolean allowDirs) {
        this.allowDirs = allowDirs;
        return this;
      }

      public boolean isAllowFiles() {
        return allowFiles;
      }

      public Builder allowFiles(boolean allowFiles) {
        this.allowFiles = allowFiles;
        return this;
      }

      public Builder directory(final String directory) {
        this.directory = directory;
        return this;
      }

      @SuppressWarnings("null")
      public List getIncludes() {
        return Collections.unmodifiableList(includes);
      }

      @SuppressWarnings("null")
      public List getExcludes() {
        return Collections.unmodifiableList(excludes);
      }

      public Builder addIncludes(String... includes) {
        Collections.addAll(this.includes, includes);
        return this;
      }

      public Builder addExcludes(String... excludes) {
        Collections.addAll(this.excludes, excludes);
        return this;
      }

      public Builder clearIncludes() {
        includes.clear();
        return this;
      }

      public Builder clearExcludes() {
        excludes.clear();
        return this;
      }

      public Builder clearDir() {
        directory = "";
        return this;
      }

      public Builder reset() {
        clearDir();
        clearIncludes();
        clearExcludes();
        return this;
      }

      public IFileSet build() {
        return new FileSet(directory, includes, excludes, allowDirs, allowFiles);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy