Search in sources :

Example 1 with DOUBLE

use of javax.lang.model.type.TypeKind.DOUBLE in project auto-matter by danielnorberg.

the class AutoMatterProcessor method valueHashCode.

private MethodSpec valueHashCode(final Descriptor d) throws AutoMatterProcessorException {
    MethodSpec.Builder hashcode = MethodSpec.methodBuilder("hashCode").addAnnotation(Override.class).addModifiers(PUBLIC).returns(TypeName.INT).addStatement("int result = 1");
    boolean needsTemp = d.fields().stream().map(ExecutableElement::getReturnType).map(TypeMirror::getKind).anyMatch(DOUBLE::equals);
    if (needsTemp) {
        hashcode.addStatement("long temp");
    }
    for (ExecutableElement field : d.fields()) {
        final String name = "this." + fieldName(field);
        final TypeMirror type = field.getReturnType();
        switch(type.getKind()) {
            case LONG:
                hashcode.addStatement("result = 31 * result + (int) ($N ^ ($N >>> 32))", name, name);
                break;
            case INT:
                hashcode.addStatement("result = 31 * result + $N", name);
                break;
            case BOOLEAN:
                hashcode.addStatement("result = 31 * result + ($N ? 1231 : 1237)", name);
                break;
            case BYTE:
            case SHORT:
            case CHAR:
                hashcode.addStatement("result = 31 * result + (int) $N", name);
                break;
            case FLOAT:
                hashcode.addStatement("result = 31 * result + ($N != +0.0f ? $T.floatToIntBits($N) : 0)", name, ClassName.get(Float.class), name);
                break;
            case DOUBLE:
                hashcode.addStatement("temp = $T.doubleToLongBits($N)", ClassName.get(Double.class), name);
                hashcode.addStatement("result = 31 * result + (int) (temp ^ (temp >>> 32))");
                break;
            case ARRAY:
                hashcode.addStatement("result = 31 * result + ($N != null ? $T.hashCode($N) : 0)", name, ClassName.get(Arrays.class), name);
                break;
            case TYPEVAR:
            case DECLARED:
            case ERROR:
                hashcode.addStatement("result = 31 * result + ($N != null ? $N.hashCode() : 0)", name, name);
                break;
            default:
                throw fail("Unsupported type: " + type, field);
        }
    }
    return hashcode.addStatement("return result").build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) DOUBLE(javax.lang.model.type.TypeKind.DOUBLE) Arrays(java.util.Arrays) Builder(com.squareup.javapoet.MethodSpec.Builder)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)1 Builder (com.squareup.javapoet.MethodSpec.Builder)1 Arrays (java.util.Arrays)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 DOUBLE (javax.lang.model.type.TypeKind.DOUBLE)1 TypeMirror (javax.lang.model.type.TypeMirror)1