
/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen;

import java.util.Arrays;

class IndexQueryInfoKey {

    private final Class<?> targetType;
    private final Class<?>[] valueTypes;
    private final boolean composite;
    private final String name;

    IndexQueryInfoKey(String name, boolean composite, Class<?> targetType, Class<?>... valueTypes) {
        assert name != null;
        assert targetType != null;
        assert valueTypes != null;
        assert valueTypes.length > 0;
        this.name = name;
        this.composite = composite;
        this.targetType = targetType;
        this.valueTypes = valueTypes;
    }

    public IndexQueryInfo getIndexQueryInfo(Permazen jdb) {

        // Handle composite index
        if (this.composite)
            return new IndexQueryInfo(jdb, this.targetType, this.name, this.valueTypes);

        // Handle map value index
        if (this.valueTypes.length == 2)
            return new IndexQueryInfo(jdb, this.targetType, this.name, this.valueTypes[0], this.valueTypes[1]);

        // Handle all others
        assert this.valueTypes.length == 1;
        return new IndexQueryInfo(jdb, this.targetType, this.name, this.valueTypes[0]);
    }

// Object

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj == null || obj.getClass() != this.getClass())
            return false;
        final IndexQueryInfoKey that = (IndexQueryInfoKey)obj;
        return this.name.equals(that.name)
          && this.composite == that.composite
          && this.targetType.equals(that.targetType)
          && Arrays.equals(this.valueTypes, that.valueTypes);
    }

    @Override
    public int hashCode() {
        return this.name.hashCode()
          ^ (this.composite ? ~0 : 0)
          ^ this.targetType.hashCode()
          ^ Arrays.hashCode(this.valueTypes);
    }
}


@@ -155,34 +155,26 @@
 e final 
-Class%3C?%3E targetTyp
+String nam
 e;%0A    p
@@ -190,29 +190,25 @@
 nal 
-Class%3C?%3E%5B%5D valueTypes
+boolean composite
 ;%0A  
@@ -223,32 +223,34 @@
 e final 
-boolean composit
+Class%3C?%3E targetTyp
 e;%0A    p
@@ -262,27 +262,37 @@
 e final 
-String name
+Class%3C?%3E%5B%5D valueTypes
 ;%0A%0A    I
@@ -1249,16 +1249,313 @@
 Object%0A%0A
+    @Override%0A    public String toString() %7B%0A        return this.getClass().getSimpleName()%0A          + %22%5Bname=%22 + this.name%0A          + %22,composite=%22 + this.composite%0A          + %22,targetType=%22 + this.targetType%0A          + %22,valueTypes=%22 + Arrays.asList(this.valueTypes)%0A          + %22%5D%22;%0A    %7D%0A%0A
     @Ove

Add IndexQueryInfoKey.toString() to facilitate debugging.