This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

JAVA - Search based on any combination of attributes

User_67OC0
User_67OC0 Member Posts: 1 Green Ribbon

We have a file system FileSystem that contains files in the form of the matrix. Each of these files have the following 4 attributes:

  • size;
  • name;
  • timeCreated;
  • permission;

I need implement a way to search in your file system based on any combination of attributes

  • eg: all files that have Permission W and created before 18-05-2022
  • eg2: all files with name containg "asd"
  • The filesystem matrix has to be traversed in reversed diagonal ( ⬋ ) :
    • eg for matrix :
    • 123456789
  • the traversal should be : [1 2 4 3 5 7 6 8 9]

How would be the best way to do that?

Thanks in advance :)



Code:

package util.filesystem;

import java.time.LocalDateTime;

public enum FileSystem {
    FILE_SYSTEM;


    public static File[][] files = new File[5][5];

    static {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {

                long size = 1000 * (i+j);
                String name = "file" + i + j;
                final LocalDateTime creationTime = LocalDateTime.of(2022, 12, i+j+1, 12, 12);
                Permissions permission = Permissions.values()[(10*i+j) % 3];
                files[i][j] = new File(size, name, creationTime, permission);
            }
        }
    }
}

package util.filesystem;

import java.time.LocalDateTime;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class File {
    private long size;
    private String name;
    private LocalDateTime timeCreated;
    private Permissions permission;
}
package util.filesystem;

public enum Permissions {
    R, W , RW
}

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Teste</groupId>
  <artifactId>Teste</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
      <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>


Tagged:

Answers

  • justsomeone
    justsomeone Member Posts: 51 Red Ribbon

    Hello @User_67OC0


    1) you need a method on the enum to return the 2 dimension array you just created

    2) if you learned about stream and lambda expression then you can get stream of that array and use the filter and use predicate to get the files that only apply to those rule you mentioned

    3) if you did not learn about step 2 yet then no problem

    a) for this one eg: all files that have Permission W and created before 18-05-2022

    using 2 nested loop check each file if it has the right permission or not and also check https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/LocalDate.html#isBefore(java.time.chrono.ChronoLocalDate) to check if the creation date is before that date or not

    you could create method in your File class to take permission as parameter and return true or false based on the file that called it

    maybe create arraylist to hold the files that follow the rule and return that arraylist

    b) eg2: all files with name containg "asd" you can use https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#contains(java.lang.CharSequence)

    for a and b you need to create getter method for your file class or create method that return boolean and it check the rule you want

    • c) The filesystem matrix has to be traversed in reversed diagonal ( ⬋ ) :
      • eg for matrix :
      • 123456789
    • the traversal should be : [1 2 4 3 5 7 6 8 9]

    it would be better to draw matrix as

    1 2 3

    4 5 6

    7 8 9

    what the out put you want cause the output you mentioned is not the reversed diagonal


    hope that help and have a nice day :)