Java 谓词<;T>;从文件

Java 谓词<;T>;从文件,java,json,parsing,predicate,Java,Json,Parsing,Predicate,有没有办法从文件(JSON、XML或java中可解析的东西)中获取这些参数?我的代码中有很多“问题”,而在代码中编码显然不是最好的方法 new Question("Is it a men?", character -> character.isMale(), Question.TYPE.GENDER)); 公开课问题{ 私有最终字符串文本; 私有最终谓词过滤器; 私人最终问题。类型; /** *@param text testo della domanda *@param过滤器过滤器d

有没有办法从文件(JSON、XML或java中可解析的东西)中获取这些参数?我的代码中有很多“问题”,而在代码中编码显然不是最好的方法

new Question("Is it a men?", character -> character.isMale(), Question.TYPE.GENDER));

公开课问题{
私有最终字符串文本;
私有最终谓词过滤器;
私人最终问题。类型;
/**
*@param text testo della domanda
*@param过滤器过滤器della domanda
*@多曼达公寓式高级公寓
*/
公共问题(最终字符串文本、最终谓词筛选器、最终类型){
this.text=文本;
this.filter=过滤器;
this.type=type;
}
/**
*@return Testo della domanda。
*/
公共字符串getText(){
返回文本;
}
/**
*每一个水獭都必须进行必要的过滤。
*/
公共谓词getFilter(){
回流过滤器;
}
/**
*@return la classe di appartenenza della domanda。
*/
公共类型getType(){
返回此.type;
}
/**
*定义多曼德公寓的古典风格,即多曼德公寓的实用性。
*/
公共枚举类型{
/***/
头发颜色、眼睛颜色、性别、眼镜、头发类型、帽子、胡须;
}
}
-----字符类

package model;

import java.util.Optional;

import javax.swing.ImageIcon;

import utilities.Colors;

public class Character {
    private final String name;
    private final boolean isMale;
    private final Colors eyeColor;
    private final Optional<Colors> hairColor;
    private final Colors skinColor;
    private final boolean hasBeard;
    private final boolean hasMustache;
    private final boolean hasHat;
    private final boolean hasEarings;
    private final boolean hasGlasses;
    private final Hair hairType;
    private final String picPath;

    /**
     * Tipi di capelli.
     */
    public enum Hair {
        /***/
        STRAIGHT, CURLY, BALD;
    }

    /**
     * @param name nome
     * @param isMale genere
     * @param eyeColor colore degli occhi
     * @param hairColor colore dei capelli
     * @param skinColor colore della pelle
     * @param hasBeard .
     * @param hasMustache .
     * @param hasHat .
     * @param hasEarings .
     * @param hasGlasses .
     * @param hairType .
     * @param picPath .
     */
    public Character(final String name, final boolean isMale, final Colors eyeColor, final Optional<Colors> hairColor, final Colors skinColor,
            final boolean hasBeard, final boolean hasMustache, final boolean hasHat, final boolean hasEarings, final boolean hasGlasses,
            final Hair hairType, final String picPath) {
        super();
        this.name = name;
        this.isMale = isMale;
        this.eyeColor = eyeColor;
        this.hairColor = hairColor;
        this.skinColor = skinColor;
        this.hasBeard = hasBeard;
        this.hasMustache = hasMustache;
        this.hasHat = hasHat;
        this.hasEarings = hasEarings;
        this.hasGlasses = hasGlasses;
        this.hairType = hairType;
        this.picPath = picPath;
    }

    /**
     * @return il nome del personaggio
     */
    public String getName() {
        return name;
    }

    /**
     * @return ritorna true se è maschio
     */
    public boolean isMale() {
        return this.isMale;
    }

    /**
     * @return il colore dei capelli del personaggio (Empty se non li ha).
     */
    public Optional<Colors> getHairColor() {
        return this.hairColor;
    }

    /**
     * @return il colore della pelle del personaggio.
     */
    public Colors getSkinColor() {
        return this.skinColor;
    }

    /**
     * @return true se ha la barba
     */
    public boolean hasBeard() {
        return this.hasBeard;
    }

    /**
     * @return true se ha i baffi
     */
    public boolean hasMustache() {
        return this.hasMustache;
    }

    /**
     * @return true se ha il cappello
     */
    public boolean hasHat() {
        return this.hasHat;
    }

    /**
     * @return true se ha gli orecchini/piercing
     */
    public boolean hasEarings() {
        return this.hasEarings;
    }

    /**
     * @return true se ha gli occhiali
     */
    public boolean hasGlasses() {
        return this.hasGlasses;
    }

    /**
     * @return il tipo di capelli definiti in enum Character.Hair
     */
    public Hair getHairType() {
        return this.hairType;
    }

    /**
     * @return il colore degli occhi del personaggio.
     */
    public Colors getEyeColor() {
        return this.eyeColor;
    }

    /**
     * @return l'immagine che rappresenta il personaggio.
     */
    public ImageIcon getPicPath() {
        return new ImageIcon(this.picPath);
    }
}
包模型;
导入java.util.Optional;
导入javax.swing.ImageIcon;
导入实用程序。颜色;
公共阶级性质{
私有最终字符串名;
私有布尔伊斯梅尔;
私人最终颜色眼睛颜色;
私人最终可选发型颜色;
私人最终颜色皮肤颜色;
私人哈斯比尔德;
私人小胡子;
私有最终布尔哈希;
私有最终布尔hasEarings;
私人眼镜;
私人最终发型;
私有最终字符串路径;
/**
*蒂皮·迪·卡佩利。
*/
公众头发{
/***/
笔直、卷曲、秃顶;
}
/**
*@param name nome
*@param isMale genre
*@param eyeColor colore degli occhi
*@param hairColor colore dei capelli
*@param skinColor colore della pelle
*@param hasBeard。
*@param有胡子。
*@param hasHat。
*@param hasEarings。
*@param戴眼镜。
*@param发型。
*@param picPath。
*/
公共字符(最终字符串名称、最终布尔值isMale、最终颜色eyeColor、最终可选发型颜色、最终颜色skinColor、,
最终布尔hasBeard,最终布尔hasBeard,最终布尔hasat,最终布尔hasEarings,最终布尔hasglass,
最终头发类型,最终字符串(路径){
超级();
this.name=名称;
this.isMale=isMale;
this.eyeColor=eyeColor;
this.hairColor=hairColor;
this.skinColor=皮肤颜色;
this.hasBeard=hasBeard;
this.hasmotache=hasmotache;
this.hasHat=hasHat;
this.hasEarings=hasEarings;
this.hasglass=hasglass;
this.hairType=hairType;
this.picPath=picPath;
}
/**
*@return il nome del personaggio
*/
公共字符串getName(){
返回名称;
}
/**
*@return ritorna true seèmaschio
*/
公共布尔值isMale(){
退回这个.isMale;
}
/**
*@return il colore dei capelli del personaggio(空的se non li ha)。
*/
公共可选getHairColor(){
把这个还给我;
}
/**
*@return il colore della pelle del personaggio。
*/
公共颜色getSkinColor(){
退回这个。skinColor;
}
/**
*@return true se ha la barba
*/
公共布尔hasBeard(){
把这个还给我;
}
/**
*@return true se ha i baffi
*/
公共布尔hasMustache(){
还这个。留胡子;
}
/**
*@return true se ha il cappello
*/
公共布尔hasHat(){
返回这个.hasHat;
}
/**
*@return true se ha gli orecchini/刺穿
*/
公共布尔hasEarings(){
把这个还给我;
}
/**
*@return true se ha gli occhiali
*/
公众眼镜(){
把这个还给我;
}
/**
*@return il tipo di capelli definiti在枚举字符中。Hair
*/
公共头发getHairType(){
返回此.hairType;
}
/**
*@return il colore degli occhi del personaggio。
*/
公共颜色getEyeColor(){
返回这个.eyeColor;
}
/**
*@return l'immagine che rappresenta il personaggio。
*/
公共图像图标getPicPath(){
返回新的图像图标(this.picPath);
}
}

请显示
问题的定义
;最简单的方法是将
Question
的集合序列化和反序列化为xml文件。但是,我猜谓词不能直接序列化。如果按此方式强制转换,它应该是可序列化的。如果为
字符
类添加代码,这个问题将更容易回答。我怀疑类型enum可以替换为属性名,而文件可以简单地命名一个bean属性和一个或多个限定值。我使用的是GSON解析器,我认为真正的问题是谓词,而不是我的enum类型,无论如何,任何建议都将不胜感激
package model;

import java.util.Optional;

import javax.swing.ImageIcon;

import utilities.Colors;

public class Character {
    private final String name;
    private final boolean isMale;
    private final Colors eyeColor;
    private final Optional<Colors> hairColor;
    private final Colors skinColor;
    private final boolean hasBeard;
    private final boolean hasMustache;
    private final boolean hasHat;
    private final boolean hasEarings;
    private final boolean hasGlasses;
    private final Hair hairType;
    private final String picPath;

    /**
     * Tipi di capelli.
     */
    public enum Hair {
        /***/
        STRAIGHT, CURLY, BALD;
    }

    /**
     * @param name nome
     * @param isMale genere
     * @param eyeColor colore degli occhi
     * @param hairColor colore dei capelli
     * @param skinColor colore della pelle
     * @param hasBeard .
     * @param hasMustache .
     * @param hasHat .
     * @param hasEarings .
     * @param hasGlasses .
     * @param hairType .
     * @param picPath .
     */
    public Character(final String name, final boolean isMale, final Colors eyeColor, final Optional<Colors> hairColor, final Colors skinColor,
            final boolean hasBeard, final boolean hasMustache, final boolean hasHat, final boolean hasEarings, final boolean hasGlasses,
            final Hair hairType, final String picPath) {
        super();
        this.name = name;
        this.isMale = isMale;
        this.eyeColor = eyeColor;
        this.hairColor = hairColor;
        this.skinColor = skinColor;
        this.hasBeard = hasBeard;
        this.hasMustache = hasMustache;
        this.hasHat = hasHat;
        this.hasEarings = hasEarings;
        this.hasGlasses = hasGlasses;
        this.hairType = hairType;
        this.picPath = picPath;
    }

    /**
     * @return il nome del personaggio
     */
    public String getName() {
        return name;
    }

    /**
     * @return ritorna true se è maschio
     */
    public boolean isMale() {
        return this.isMale;
    }

    /**
     * @return il colore dei capelli del personaggio (Empty se non li ha).
     */
    public Optional<Colors> getHairColor() {
        return this.hairColor;
    }

    /**
     * @return il colore della pelle del personaggio.
     */
    public Colors getSkinColor() {
        return this.skinColor;
    }

    /**
     * @return true se ha la barba
     */
    public boolean hasBeard() {
        return this.hasBeard;
    }

    /**
     * @return true se ha i baffi
     */
    public boolean hasMustache() {
        return this.hasMustache;
    }

    /**
     * @return true se ha il cappello
     */
    public boolean hasHat() {
        return this.hasHat;
    }

    /**
     * @return true se ha gli orecchini/piercing
     */
    public boolean hasEarings() {
        return this.hasEarings;
    }

    /**
     * @return true se ha gli occhiali
     */
    public boolean hasGlasses() {
        return this.hasGlasses;
    }

    /**
     * @return il tipo di capelli definiti in enum Character.Hair
     */
    public Hair getHairType() {
        return this.hairType;
    }

    /**
     * @return il colore degli occhi del personaggio.
     */
    public Colors getEyeColor() {
        return this.eyeColor;
    }

    /**
     * @return l'immagine che rappresenta il personaggio.
     */
    public ImageIcon getPicPath() {
        return new ImageIcon(this.picPath);
    }
}