简体   繁体   中英

The best way to store data in java with more dimensions?

I have some input like :

  package 1 : org.orchestr.something  version= 5.1.3

      uses = org.do.resource   ; version= 1.2.1
      uses = org.test.summer   ; version= 1.5.2


  package 2 : fr.test.something  version= 5.1.3

      uses = com.java.rest     ; version= 1.0.1
      uses = org.osgi.summer   ; version= 1.4.2

  ....

So in this data I have set of packages defined by their version and bunch others packages that they use within a specific version, and i want to know with is the best way/ practical way to store this kind of data.

It sounds like you intend to represent some graph like structure.

A first naive implementation could/would be using specific classes, like Package and Entry where a Package probably has a list/set of of Entry objects (and an Entry has a name + version string).

Of course, as you are in the end intend to create some sort of graph, you probably should look into solutions such as graphql , too.

Something like this might work, assuming there's one-level of nested dependency in your case:

A base Artifact class:

  class Artifact {
    protected String name;
    protected String version;

    public Artifact(String name, String version) {
      this.name = name;
      this.version = version;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getVersion() {
      return version;
    }

    public void setVersion(String version) {
      this.version = version;
    }
  }

A Package class that extends Artifact and adds another parameter uses :

class Package extends Artifact {

    public Package(String name, String version, List<Artifact> uses) {
      super(name, version);
      this.uses = uses;
    }

    private List<Artifact> uses;

    public List<Artifact> getUses() {
      return uses;
    }

    public void setUses(List<Artifact> uses) {
      this.uses = uses;
    }
  }

You can then store it as a List or Map as needed.

Example of using in a Map :

    Map<String, Package> map = new HashMap<>();
    map.put("package1", new Package("org.orchestr.something", "5.1.3", Arrays.asList(
        new Artifact("org.do.resource", "1.2.1"),
        new Artifact("org.test.summer", "1.5.2")))
    );

    map.put("package2", new Package("fr.test.something", "5.1.3", Arrays.asList(
        new Artifact("com.java.rest", "1.0.1"),
        new Artifact("org.osgi.summer", "1.4.2")))
    );

The package and uses data look equal. So i would use a map:

class Uses { private path, version; }
Map<Uses, Set<Uses>> data;

The packages are the keys, the used dependencies are elements of the value.

You could model it as a map of artifacts :

import java.util.Objects;

public class Artifact {
    private String name;
    private String version;

    public Artifact(String name, String version) {
        this.name = name;
        this.version = version;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Artifact that = (Artifact) o;
        return version.equals(that.version) &&
                name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(version, name);
    }
}

And here would be a usage of this :

    public static void main(String[] args) {
        Map<Artifact, Set<Artifact>>  map = new HashMap<>();


        Set<Artifact> dependencies = new HashSet<>();
        dependencies.add(new Artifact("org.do.resource ", "1.2.1"));
        dependencies.add(new Artifact("org.test.summer ", "1.5.2"));

        map.put(new Artifact("org.orchestr.something", "5.1.3"), dependencies);
    }

I had to override equals and hashcode methods for Artifact class because objects of this class will be used as keys in HashMap . If you want to also support package version just add such field to Artifact class and add apropriate constructor, getters, setters and modify equals and hashcode methods accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM