cqengine cant指数等于

时间:2016-10-30 21:09:32

标签: java cqengine

我试图添加一个索引,我的覆盖equals()确定两个对象是否相同。

Car.java

public static class Car {

        final String id;
        private String name;

        public Car(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }

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

        public static final Attribute<Car, Car> CAR = new SimpleAttribute<Car, Car>() {
            @Override
            public Car getValue(Car car, QueryOptions queryOptions) {
                return car;
            }
        };

        @Override
        public String toString() {
            return "Car{" + "id=" + id + ", name=" + name + '}';
        }

}

Fetcher.java

public static final ResultSet<Car> get(final IndexedCollection<Car> indexedCollection, final Car car) {
    return indexedCollection.retrieve(QueryFactory.equal(Car.CAR, car));
}

Main.java

public static void main(String args[]) {
            IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
            cars.addIndex(NavigableIndex.onAttribute(Car.CAR));
}

问题出在cars.addIndex(NavigableIndex.onAttribute(Car.CAR));这一行,其中错误消息为no suitable method found for onAttribute(Attribute<Car,Car>)。我在这里做错了什么,或者我应该使用另一个电话吗?

1 个答案:

答案 0 :(得分:1)

删除cars.addIndex(NavigableIndex.onAttribute(Car.CAR));,因为它实际上并不是一个有用的索引......我认为这不是开发人员的动机。您应该为CAR_IDCAR_NAME创建属性并创建查询以进行比较。在这种情况下,我滥用(实现您的期望)IndexedCollection作为一个简单的Set。但是......如果我理解正确的话,这是一个可能的解决方案:

在Car中覆盖等号:

class Car {
    private final int id;
    private String name;
    public Car(int i, String name) {
        this.id = i;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }    
    @Override
    public boolean equals(Object obj) {
        if(this == obj) return true;
        if(obj == null) return false;
        if (!(obj instanceof Car)) return false;        
        Car car = (Car) obj;        
        if(car.getId() == this.getId())
            if(car.getName().equals(this.getName()))
                return true;          
        return false;
    }
    public static final Attribute<Car, Car> CAR = new SimpleAttribute<Car, Car>() {
        @Override
        public Car getValue(Car car, QueryOptions queryOptions) {
            return car;        
        }
    };
    @Override
    public String toString() {
        return "Car{" + "id=" + id + ", name=" + name + '}';
    }
}

主要:

IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
cars.add(new Car(1, "test"));
cars.add(new Car(2, "test2"));
cars.add(new Car(3, "test3"));      
Car s = new Car(2, "test2");
ResultSet<Car> cs= cars.retrieve(QueryFactory.equal(Car.CAR, s));
cs.forEach(c -> System.out.println(c.toString()));