2

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?

Hooli
  • 1,135
  • 3
  • 19
  • 46
  • `IndexedCollection cars` need a `Car` but `Attribute CAR` is not a Car. It is something like an SimpleEntry. Try call `getValue();` – asdf Oct 30 '16 at 21:27
  • @asdf: Where do I call `getValue();` exactly? – Hooli Oct 30 '16 at 21:31
  • See this example: [Car](https://github.com/npgall/cqengine/blob/master/code/src/test/java/com/googlecode/cqengine/examples/introduction/Car.java) and [Introduction](https://github.com/npgall/cqengine/blob/master/code/src/test/java/com/googlecode/cqengine/examples/introduction/Introduction.java) – asdf Oct 30 '16 at 21:31
  • @asdf: I don't actually see `equals()` being used anywhere there? – Hooli Oct 30 '16 at 21:32
  • You should fix first the other issue... – asdf Oct 30 '16 at 21:36
  • @asdf: Could you possibly post the fix as an answer? – Hooli Oct 30 '16 at 21:41

1 Answers1

1

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()));
asdf
  • 246
  • 3
  • 12