Mutable List of Float, Immutable List of Float

This code fails to maintain the integrity of the default value.


package net.coderextreme;


import java.util.ArrayList;

import java.util.List;

import java.util.Arrays;

import java.util.Collections;

import java.lang.Float;


public class NoModFloatArray {

private static final ArrayList<Float> FLOAT_ARRAY_FIELDOFVIEW_DEFAULT_VALUE = new ArrayList<Float>(Arrays.asList(-1f,-1f,1f,1f));

public  static final List<Float> FIELDOFVIEW_DEFAULT_VALUE = Collections.unmodifiableList(FLOAT_ARRAY_FIELDOFVIEW_DEFAULT_VALUE);

public void print(String section, List<Float> MOD_FIELDOFVIEW) {

System.out.println();

System.out.println(section);

System.out.println("Immutable Array FIELDOFVIEW_DEFAULT_VALUE "+FIELDOFVIEW_DEFAULT_VALUE);

System.out.println("Mutable Array MOD_FIELDOFVIEW"+MOD_FIELDOFVIEW);

System.out.println("Compare mutable to immutable "+MOD_FIELDOFVIEW.equals(FIELDOFVIEW_DEFAULT_VALUE));

System.out.println();

}

public NoModFloatArray() {

List<Float> MOD_FIELDOFVIEW = new ArrayList<Float>(FLOAT_ARRAY_FIELDOFVIEW_DEFAULT_VALUE);

print("Modify Pre Immutable Change", MOD_FIELDOFVIEW);

try {

System.out.println("Setting immutable index 0 to 2.0");

FIELDOFVIEW_DEFAULT_VALUE.set(0, 2.0f);

} catch (UnsupportedOperationException ex) {

ex.printStackTrace();

}

print("Modify Post Immutable Change", MOD_FIELDOFVIEW);


System.out.println("setting base mutable index 1 to 3.0");

FLOAT_ARRAY_FIELDOFVIEW_DEFAULT_VALUE.set(1, 3.0f);

print("Post modify Changing base mutable", MOD_FIELDOFVIEW);


System.out.println("seting mutable index 2 to 4.0");

MOD_FIELDOFVIEW.set(2, 4.0f);

print("Post modify non-final", MOD_FIELDOFVIEW);

}

public static void main(String[] args) {

new NoModFloatArray();

System.out.println("---------------------------------------------");

new NoModFloatArray();

}

}

$ java net/coderextreme/NoModFloatArray 2>&1

Modify Pre Immutable Change
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, -1.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, -1.0, 1.0, 1.0]
Compare mutable to immutable true

Setting immutable index 0 to 2.0
java.lang.UnsupportedOperationException
        at java.base/java.util.Collections$UnmodifiableList.set(Collections.java:1504)
        at net.coderextreme.NoModFloatArray.<init>(NoModFloatArray.java:25)
        at net.coderextreme.NoModFloatArray.main(NoModFloatArray.java:40)

Modify Post Immutable Change
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, -1.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, -1.0, 1.0, 1.0]
Compare mutable to immutable true

setting base mutable index 1 to 3.0

Post modify Changing base mutable
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, 3.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, -1.0, 1.0, 1.0]
Compare mutable to immutable false

seting mutable index 2 to 4.0

Post modify non-final
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, 3.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, -1.0, 4.0, 1.0]
Compare mutable to immutable false

---------------------------------------------

Modify Pre Immutable Change
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, 3.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, 3.0, 1.0, 1.0]
Compare mutable to immutable true

Setting immutable index 0 to 2.0
java.lang.UnsupportedOperationException
        at java.base/java.util.Collections$UnmodifiableList.set(Collections.java:1504)
        at net.coderextreme.NoModFloatArray.<init>(NoModFloatArray.java:25)
        at net.coderextreme.NoModFloatArray.main(NoModFloatArray.java:42)

Modify Post Immutable Change
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, 3.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, 3.0, 1.0, 1.0]
Compare mutable to immutable true

setting base mutable index 1 to 3.0

Post modify Changing base mutable
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, 3.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, 3.0, 1.0, 1.0]
Compare mutable to immutable true

seting mutable index 2 to 4.0

Post modify non-final
Immutable Array FIELDOFVIEW_DEFAULT_VALUE [-1.0, 3.0, 1.0, 1.0]
Mutable Array MOD_FIELDOFVIEW[-1.0, 3.0, 4.0, 1.0]
Compare mutable to immutable false

Comments

Popular posts from this blog

Progress on JSON array for MFString

Mutable List of Float, Immutable List of Float, Revisited