Hooli Hooli - 1 month ago 17
Java Question

cqengine cant index by equals

I'm trying to add an index where my override

equals()
determines if two objects are the same or not.

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));
}


The problem is on this line
cars.addIndex(NavigableIndex.onAttribute(Car.CAR));
where the error message is
no suitable method found for onAttribute(Attribute<Car,Car>)
. Am I doing something wrong here or is there another call I should be using instead?

Answer

Remove cars.addIndex(NavigableIndex.onAttribute(Car.CAR));, because it is not really an usefull index... and I think this was not a motivation of the developer. You should create Attributes for CAR_ID and CAR_NAME and create an Query for comparison. In this case I misuse (to achieve what you expect) IndexedCollection as a simple Set. But... here is a possible solution, if I have understood you correctly:

Override equals in 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 + '}';
    }
}

Main:

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()));