Skip to content
Advertisement

Why this lambda function does not remember type, in Java? [closed]

I am trying to use java streams as much as possible.
One handicap is, converting local variables to effective finals.

here is an example of non effective final variable:

var yetkiliAdı = TS_SQLSelectUtils.select(cp.sqlAnc, "hamsiparis").columns(
        "STR254_YETKILI"
).whereFirstColumnAsId(so.selectedId).getStr();
if (TGS_StringUtils.isNullOrEmpty(yetkiliAdı)) {
    yetkiliAdı = TS_SQLSelectUtils.select(cp.sqlAnc, "firma").columns(
            "STR254_YETADI"
    ).whereFirstColumnAsId(tedFirmId).getStr();
}
yetkiliAdı = TGS_StringUtils.toNullIfEmpty(yetkiliAdı);

I am trying to convert this code to effective final by below:

var yetkiliAdı = TGS_Coronator.of("")
.anoint(
        val -> TS_SQLSelectUtils.select(cp.sqlAnc, "hamsiparis")
                .columns("STR254_YETKILI")
                .whereFirstColumnAsId(so.selectedId)
                .getStr()
).anointIf(val -> TGS_StringUtils.isNullOrEmpty(val),
        val -> TS_SQLSelectUtils.select(cp.sqlAnc, "firma")
                .columns("STR254_YETADI")
                .whereFirstColumnAsId(tedFirmId)
                .getStr()
).anoint(
        val -> TGS_StringUtils.toNullIfEmpty(val)
).coronate();

However I get the error “incompatible types: Object cannot be converted to CharSequence” error locations picture

I tried to put the code in github with my limited knowledge
TGS_Coronator.java:

package com.tugalsan.api.coronator.client;

import com.tugalsan.api.compiler.client.*;
import com.tugalsan.api.pack.client.*;
import com.tugalsan.api.validator.client.*;
import java.util.*;

public class TGS_Coronator<T> {

    //CONSTRUCTOR
    private T bufferedValue;

    public TGS_Coronator(T initVal) {
        bufferedValue = initVal;
    }

    public static <T> TGS_Coronator of(T initialValue) {
        return new TGS_Coronator(initialValue);
    }

    //LOADERS
    private List<TGS_Pack3<TGS_CompilerType1<T, T>, TGS_ValidatorType1<T>, /*is it stopper*/ Boolean>> pack = new ArrayList();

    public TGS_Coronator<T> anoint(TGS_CompilerType1<T, T> val) {
        pack.add(new TGS_Pack3(val, null, null));
        return this;
    }

    public TGS_Coronator<T> coronateIf(TGS_ValidatorType1<T> validate, TGS_CompilerType1<T, T> val) {
        if (validate.validate(bufferedValue)) {
            pack.add(new TGS_Pack3(null, validate, true));
        }
        return this;
    }

    public TGS_Coronator<T> anointIf(TGS_ValidatorType1<T> validate, TGS_CompilerType1<T, T> val) {
        if (validate.validate(bufferedValue)) {
            pack.add(new TGS_Pack3(val, validate, false));
        }
        return this;
    }

    public TGS_Coronator<T> anointAndCoronateIf(TGS_ValidatorType1<T> validate, TGS_CompilerType1<T, T> val) {
        if (validate.validate(bufferedValue)) {
            pack.add(new TGS_Pack3(val, validate, true));
        }
        return this;
    }

    //FETCHER
    public T coronate() {
        for (var comp : pack) {
            var setter = comp.value0;
            var validator = comp.value1;
            var validatorIsStopper = comp.value2;
            if (validator == null) {
                bufferedValue = setter.compile(bufferedValue);
                continue;
            }
            if (!validator.validate(bufferedValue)) {
                continue;
            }
            if (setter != null) {
                bufferedValue = setter.compile(bufferedValue);
            }
            if (validatorIsStopper) {
                return bufferedValue;
            }
        }
        return bufferedValue;
    }

}

Any directions?

Advertisement

Answer

I found the error after 1 day.

I forgot to define return type on of function:
wrong code:

public static <T> TGS_Coronator of(T initialValue) {
    return new TGS_Coronator(initialValue);
}

right code:

public static <T> TGS_Coronator<T> of(T initialValue) {
    return new TGS_Coronator(initialValue);
}

full code at github

Advertisement