Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在hazelcast中创建自己的自定义谓词来比较复合id_Java_Hazelcast_Hazelcast Imap - Fatal编程技术网

Java 如何在hazelcast中创建自己的自定义谓词来比较复合id

Java 如何在hazelcast中创建自己的自定义谓词来比较复合id,java,hazelcast,hazelcast-imap,Java,Hazelcast,Hazelcast Imap,我想创建自己的custome谓词来比较复合id的内部对象。之所以需要这样做,是因为我必须在对象内部的对象(复合id)上编写特定的日期比较逻辑。我不想比较单个属性。我想使用复合id,因为它来自调用程序,我无法使用Predicate.in和Predicate.equals获得结果 我的对象结构如下所示 Birth{id=BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}, name='b3', dob=Wed Jan 01 10:53:20 IS

我想创建自己的custome谓词来比较复合id的内部对象。之所以需要这样做,是因为我必须在对象内部的对象(复合id)上编写特定的日期比较逻辑。我不想比较单个属性。我想使用复合id,因为它来自调用程序,我无法使用Predicate.in和Predicate.equals获得结果

我的对象结构如下所示

Birth{id=BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}, name='b3', dob=Wed Jan 01 10:53:20 IST 1902, description='MNP'}
key   : BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}

value : Birth{id=BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}, name='b3', dob=Wed Jan 01 10:53:20 IST 1902, description='MNP'}
public class DatePredicate extends AbstractPredicate<Comparable, BirthId> {

    Comparable[] values;
    private volatile Set<Comparable> convertedInValues;

    public DatePredicate() {
    }

    public DatePredicate(String attribute, Comparable... values) {
        if (values == null) {
            throw new NullPointerException("Array can't be null");
        } else {
            this.values = values;
        }
    }

    protected boolean applyForSingleAttributeValue(Map.Entry entry, Comparable attributeValue) {

        //My own date comparison logic
        return true;
    }

    public int getId() {
        return 99;
    }
}
在IMap中,它的存储方式如下所示

Birth{id=BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}, name='b3', dob=Wed Jan 01 10:53:20 IST 1902, description='MNP'}
key   : BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}

value : Birth{id=BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}, name='b3', dob=Wed Jan 01 10:53:20 IST 1902, description='MNP'}
public class DatePredicate extends AbstractPredicate<Comparable, BirthId> {

    Comparable[] values;
    private volatile Set<Comparable> convertedInValues;

    public DatePredicate() {
    }

    public DatePredicate(String attribute, Comparable... values) {
        if (values == null) {
            throw new NullPointerException("Array can't be null");
        } else {
            this.values = values;
        }
    }

    protected boolean applyForSingleAttributeValue(Map.Entry entry, Comparable attributeValue) {

        //My own date comparison logic
        return true;
    }

    public int getId() {
        return 99;
    }
}
下面是我的Java类(出生和出生ID)结构

    public class Birth implements Serializable, Portable {

        private static final long serialVersionUID = 1L;

        private BirthId id;
        private String name;
        private Date dob;
        private String description;

        public BirthId getId() {
            return id;
        }

        public void setId(BirthId id) {
            this.id = id;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getName() {
            return name;
        }

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

        public Date getDob() {
            return dob;
        }

        public void setDob(Date dob) {
            this.dob = dob;
        }

        public int hashCode() {
            return (new HashCodeBuilder()).append(this.id).append(this.name).append(this.dob).toHashCode();
        }

        public boolean equals(Object other) {
            if (other == this) {
                return true;
            } else if (!(other instanceof Birth)) {
                return false;
            } else {
                Birth rhs = (Birth) other;
                return (new EqualsBuilder()).append(this.id, rhs.id).append(this.name, rhs.name).append(this.dob, rhs.dob).isEquals();
            }
        }

        @Override public String toString() {
            return "Birth{" + "id=" + id + ", name='" + name + '\'' + ", dob=" + dob + ", description='" + description + '\'' + '}';
        }

        public int getFactoryId() {
            return 1;
        }

        public int getClassId() {
            return 1;
        }

        public void writePortable(PortableWriter portableWriter) throws IOException {
            portableWriter.writePortable("idComposite", getId());
            portableWriter.writeUTF("id", getId().toString());
            portableWriter.writeUTF("name", getName());
            portableWriter.writeUTF("description", getDescription());
            Date date = getDob();
            portableWriter.writeLong("dob", ((date == null) ? -1 : date.getTime()));
        }

        public void readPortable(PortableReader portableReader) throws IOException {
            setId((BirthId) portableReader.readPortable("idComposite"));
            setName(portableReader.readUTF("name"));
            setDescription(portableReader.readUTF("description"));
            long date = portableReader.readLong("dob");
            setDob(((date == -1) ? null : new Date(date)));
        }

    }

public class BirthId implements Comparable<BirthId>, Serializable, Portable {

    private static final long serialVersionUID = 1L;

    private String name;
    private Date dob;

    public String getName() {
        return name;
    }

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

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public int hashCode() {
        return (new HashCodeBuilder()).append(this.name).append(this.dob).toHashCode();
    }

    public boolean equals(Object other) {
        if (other == this) {
            return true;
        } else if (!(other instanceof BirthId)) {
            return false;
        } else {
            BirthId rhs = (BirthId) other;
            return (new EqualsBuilder()).append(this.name, rhs.name).append(this.dob, rhs.dob).isEquals();
        }
    }

    public int compareTo(BirthId rhs) {
        return this == rhs ? 0 : (null == rhs ? -1 : (new CompareToBuilder()).append(this.name, rhs.name).append(this.dob, rhs.dob).toComparison());
    }

    @Override public String toString() {
        return "BirthId{" + "name='" + name + '\'' + ", dob=" + dob + '}';
    }

    public int getFactoryId() {
        return 1;
    }

    public int getClassId() {
        return 2;
    }

    public void writePortable(PortableWriter portableWriter) throws IOException {
        portableWriter.writeUTF("name", getName());
        Date date = getDob();
        portableWriter.writeLong("dob", ((date == null) ? -1 : date.getTime()));
    }

    public void readPortable(PortableReader portableReader) throws IOException {
        setName(portableReader.readUTF("name"));
        long date = portableReader.readLong("dob");
        setDob(((date == -1) ? null : new Date(date)));
    }

    public static ClassDefinition getClassDefinition(int portableVersion) {
        ClassDefinitionBuilder result = new ClassDefinitionBuilder(1, 2, portableVersion);
        result.addUTFField("name");
        result.addLongField("dob");
        return result.build();
    }

}
我正在犯错误

Exception in thread "main" com.hazelcast.nio.serialization.HazelcastSerializationException: com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory@5f007be3 is not be able to create an instance for id: 99 on factoryId: -32
我不知道创建自己的自定义谓词的最佳方法,hazelcast文档也没有指定


有谁能告诉我怎么做吗?

@oomph fortuity,您的
日期谓词
扩展了
抽象谓词
,它实现了
可识别数据序列化
,并由内置Hazelcast谓词使用。内置谓词可序列化工厂尝试反序列化类&失败,因为它只知道如何序列化/反序列化内置谓词

相反,只需实现
com.hazelcast.query.Predicate
接口:

class DatePredicate implements Predicate<BirthId, Birth> {

BirthId birthIdToCompare;

public DatePredicate() {
}

public DatePredicate(BirthId birthId) {
    this.birthIdToCompare = birthId;
}

@Override
public boolean apply(Map.Entry<BirthId, Birth> mapEntry) {
    BirthId key = mapEntry.getKey();
    ///your custom logic
    return true;
}
}

让我知道这是否有效。

@oomph偶然性,您的
日期谓词
扩展了
抽象谓词
,它实现了
可识别的数据序列化
,并由内置Hazelcast谓词使用。内置谓词可序列化工厂尝试反序列化类&失败,因为它只知道如何序列化/反序列化内置谓词

相反,只需实现
com.hazelcast.query.Predicate
接口:

class DatePredicate implements Predicate<BirthId, Birth> {

BirthId birthIdToCompare;

public DatePredicate() {
}

public DatePredicate(BirthId birthId) {
    this.birthIdToCompare = birthId;
}

@Override
public boolean apply(Map.Entry<BirthId, Birth> mapEntry) {
    BirthId key = mapEntry.getKey();
    ///your custom logic
    return true;
}
}
让我知道这是否有效