Reputation: 135
how can i represent a record type in java ??
exemple
TYPE Pattern = RECORD
Semantique:varchar;
type:varchar;
chemin:varchar;
END;
Upvotes: 4
Views: 22426
Reputation: 748
The Java representation (as of JDK 17) would be:
record Pattern(String semantique, String type; String chemin) { };
A Java record is immutable, i. e. its members cannot be altered plus they are private. That is, their references cannot be altered, their states can unless they are instances of immutable classes like String, LocalDate etc.
By this—without adding any more code—you will get:
a) A canonical constructor
public Pattern(String semantique, String type, String chemin) {
this.semantique = semantique;
this.type = type;
this.chemin = chemin;
}
b) Reading accessors
public String semantique();
public String type();
public String chemin();
c) A boolean equals(Object other)
This is based on equality of the members—at least it behaves like that for me.
In classical Java the above one-liner would look like this:
public class Pattern {
private final String semantique;
private final String type;
private final String chemin;
public Pattern(String semantique; String type; String chemin) {
this.semantique = semantique;
this.type = type;
this.chemin = chemin;
}
public String semantique() {
return semantique;
}
public String type() {
return type;
}
public String chemin() {
return chemin;
}
public equals(Object other) {
if (other == null)
return false;
if (false == Pattern.class.isInstance(other)
return false;
final var typesafeOther = (Pattern) other;
return Objects.equals(this.semantique, typeSafeOther.semantique)
&& Objects.equals(this.type, typeSafeOther.type)
&& Objects.equals(this.chemin, typeSafeOther.chemin);
}
}
I am not sure, wether the .hashCode()
is based on quality of the members, too, but I would expect it to be.
You can add:
validation of input
Pattern {
Objects.requireNonNull(semantique);
Objects.requireNonNull(type);
Objects.requireNonNull(chemin);
}
custom constructors
This one needs to delegate to another constructor, ultimately calling the canonical constructor at the end of the chain.
public Pattern() {
this("n/A", "n/A", "n/A");
}
custom methods
This might be silly from your domain's perspective, but it demonstrates the technical possibility.
public boolean hasSemantique() {
return semantique != null && false == semantique.trim().length() > 0;
}
Upvotes: 0
Reputation: 221135
Starting with JDK 14 (preview feature), you can write:
record Pattern (String semantique, String type, String chemin);
Upvotes: 5
Reputation: 5286
public class Record {
String semantique;
String type;
String chemin;
}
You may want getters and setters if this is going to be a bean. Those would look like:
public void setType(String type){ this.type=type; }
public String getType() { return type; }
Upvotes: 2
Reputation: 994121
You can roughly translate that to Java like this:
public class Pattern {
public String Semantique;
public String type;
public String chemin;
}
However, this is likely to be substantially different in details than whatever language you're translating from (Pascal?). For example, you can't read and write instances of Java classes directly to disk files.
Upvotes: 0