Is it (really that) bad to use case-classes for mutable state?

Consider the following code:

case class Vector3(var x: Float, var y: Float, var z: Float)
{
  def add(v: Vector3): Unit =
  {
    this.x += v.x
    this.y += v.y
    this.z += v.z
  }
}

As you can see, the case class holds mutable state. It is highly discouraged to do this and normally I’d agree and absolutely stick to this “rule”, but here goes the whole story.

I’m using Scala to write a little 3d-game-engine from scratch. So first I thought about using a (much more) functional style, but then the garbage-collector would kick in too often.

read more

Can I/ How do I use a static method in a non static enviroment

public class Transcript {
    private List<Grade> grades = new ArrayList<Grade>();
    private int id;

    public void assignVariables(){
        for(int i = 0; i < grades.size(); i++){
            if(grades.get(i).getLetter() == null){
                grades.get(i).setLetter(GPA2Letter(grades.get(i).gradePoint()));

            }
        }
    }




    public class Registrar {

    static double gpa;

    public Registrar(){

    }

    public static double letter2Point(String gradeIn){
        if(gradeIn == "A" ){
            gpa = 4.0;
        }
        else if(gradeIn == "B"){
            gpa = 3.0;
        }
        else if(gradeIn == "C"){
            gpa = 2.0;
        }
        else if(gradeIn == "D"){
            gpa = 1.0;
        }
        else if(gradeIn == "F"){
            gpa = 0;
        }
        return gpa;
    }

    public static String GPA2Letter(double gradePointIn){
        String letter = "";
        if(gradePointIn >= 4){
            letter = "A";
        }
        else if((gradePointIn >= 3) && (gradePointIn < 4)){
            letter = "B";
        }
        else if((gradePointIn >= 2) && (gradePointIn < 3)){
            letter = "C";
        }
        else if((gradePointIn >= 1) && (gradePointIn < 2)){
            letter = "D";
        }
        else if((gradePointIn >= 0) && (gradePointIn < 1)){
            letter = "F";
        }
    }

}

read more

Safe sending String argument to JavaScript function from Java

My Java project based on WebView component.
Now, I want to call some JS function with single String argument.
To do this, I’m using simple code:

webEngine.executeScript("myFunc('" + str + "');");

*str text is getting from the texarea.

This solution works, but not safe enough.
Some times we can get netscape.javascript.JSException: SyntaxError: Unexpected EOF

So, how to handle str to avoid Exception?

Safe sending String argument to JavaScript function from Java

Safe sending String argument to JavaScript function from Java

My Java project based on WebView component.
Now, I want to call some JS function with single String argument.
To do this, I’m using simple code:

webEngine.executeScript("myFunc('" + str + "');");

*str text is getting from the texarea.

This solution works, but not safe enough.
Some times we can get netscape.javascript.JSException: SyntaxError: Unexpected EOF

So, how to handle str to avoid Exception?

Safe sending String argument to JavaScript function from Java

RecyclerView notifyItemRangeChanged doesn’t show new data

I am running into an issue with the RecyclerView#Adapter regarding notifyItemRangeChanged. It seems that if the Adapter thinks it has a size of 0 from the last call to getItemCount, and then I call Adapter#notifyItemRangeChanged(0, 1), the Adapter will simply ignore the call (it doesn’t result in the new item being inserted, for example).

Is this the expected behavior?

RecyclerView notifyItemRangeChanged doesn’t show new data