2016-10-30 39 views
1

我試圖添加一個索引,其中我的覆蓋equals()確定兩個對象是否相同。cqengine cant index by 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)); 
} 

的proble m在這條線上cars.addIndex(NavigableIndex.onAttribute(Car.CAR));其中錯誤消息是no suitable method found for onAttribute(Attribute<Car,Car>)。我在這裏做錯了什麼,或者是否有另一個我應該使用的電話?

+0

'IndexedCollection cars'需要一個'汽車

覆蓋在車等於'但'屬性 CAR'不是汽車。它就像一個SimpleEntry。嘗試調用'getValue();' – asdf

+0

@asdf:我在哪裏調用'getValue();'完全? – Hooli

+0

看到這個例子:[Car](https://github.com/npgall/cqengine/blob/master/code/src/test/java/com/googlecode/cqengine/examples/introduction/Car.java)和[Introduction ](https://github.com/npgall/cqengine/blob/master/code/src/test/java/com/googlecode/cqengine/examples/introduction/Introduction.java) – asdf

回答

1

刪除cars.addIndex(NavigableIndex.onAttribute(Car.CAR));,因爲它不是一個真正有用的索引...我認爲這不是開發人員的動機。您應該爲CAR_IDCAR_NAME創建屬性並創建一個用於比較的查詢。在這種情況下,我濫用(達到你的期望)IndexedCollection作爲一個簡單的Set。但是...這裏是一個可能的解決方案,如果我明白你正確地:

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